#!/usr/bin/perl
use strict;
use warnings;
use Gtk2 -init;
use constant TRUE => 1;
use constant FALSE => 0;

sub h_fader_255 {
  my $range = Gtk2::VScale->new_with_range(0, 255, 1);
  $range->set_value_pos('bottom');
  $range->set_inverted(1);
  $range->set_increments(5, 50);
  return $range;
}

sub dialog_audio_config {
  my $dialog = Gtk2::Dialog->new();
  my $notebook = Gtk2::Notebook->new();

  my $basics_frame = Gtk2::Frame->new("Basics");
  $notebook->append_page($basics_frame, Gtk2::Label->new("Basics"));
  $dialog->add($notebook);
  return $dialog;
}

sub audio_vu {
  my $left_vu = Gtk2::ProgressBar->new;
  $left_vu->set_orientation('bottom-to-top');
  return $left_vu;
}

sub audio_control {
  my $name = shift;
  my $hbox = Gtk2::HBox->new();
  $hbox->add(audio_vu());
  $hbox->add(audio_vu());
  $hbox->add(h_fader_255());

  my $frame = Gtk2::Frame->new($name);
  $frame->add($hbox);

  my $hbox2 = Gtk2::HBox->new();
  $hbox2->pack_start($frame, FALSE, FALSE, 0);
  return $hbox2;
}

sub video_transitions {
  my $frame = Gtk2::Frame->new("Transition");
  my $hbox = Gtk2::HBox->new();

  $hbox->add(h_fader_255);

  my $vbox = Gtk2::VBox->new();
  my $list = Gtk2::ScrolledWindow->new();
  $list->set_size_request(300,60);

  #my $text = Gtk2::TextView->new();
  
  #$list->append_text("Crossfade");
  #$list->append_text("Wipe");
  $vbox->add($list);

  $hbox->add($vbox);
  $frame->add($hbox);
  return $frame;
}

sub video_image {
  my $vbox = Gtk2::VBox->new();
  my $aspect_frame = Gtk2::AspectFrame->new("Video", 1, 0, 4/3, FALSE);
  my $drawing_area = Gtk2::DrawingArea->new();
  $drawing_area->size(64, 48);
  $aspect_frame->add($drawing_area);
  $vbox->add($aspect_frame);
  return $vbox
}

sub video_control {
  my $name = shift;
  my $frame = Gtk2::Frame->new($name);
  my $hbox = Gtk2::HBox->new();
  $hbox->add(audio_control("Audio"));
  $hbox->add(video_image());
  $frame->add($hbox);
  return $frame;
}

sub audio_only_inputs {
  my $frame_live_audio = Gtk2::Frame->new("Live Audio Sources");
  my $hbox = Gtk2::HBox->new();

  $frame_live_audio->add($hbox);
  $hbox->pack_start(audio_control("Mic 1"), FALSE, FALSE, 0);
  $hbox->pack_start(audio_control("Mic 2"), FALSE, FALSE, 0);
  $hbox->pack_start(audio_control("Mic 3"), FALSE, FALSE, 0);
  $hbox->pack_start(audio_control("Mic 4"), FALSE, FALSE, 0);

  my $hbox_audio = Gtk2::HBox->new();
  $hbox_audio->add($frame_live_audio);
  return $hbox_audio;
}

sub video_only_inputs {
  my $frame = Gtk2::Frame->new("Live Video Sources");
  my $hbox = Gtk2::HBox->new();
  $hbox->add(video_control("Cam 1"));
  $hbox->add(video_control("Cam 2"));
  $hbox->add(video_control("Cam 3"));
  $hbox->add(video_control("TwinPact 1"));
  $frame->add($hbox);
  return $frame;
}

sub video_overlays {
  my $frame = Gtk2::Frame->new("Video Overlays");
  my $hbox = Gtk2::HBox->new();
  $hbox->add(video_image("Compose 1"));
  $hbox->add(video_image("Compose 2"));
  $hbox->add(video_image("Compose 3"));
  $hbox->add(video_image("Compose 4"));
  $frame->add($hbox);
  return $frame;
}

sub master_mix_box {
  my $frame = Gtk2::Frame->new("Master Output");
  my $hbox = Gtk2::HBox->new();
  $hbox->add(audio_control("Audio"));
  $hbox->add(video_image());
  $frame->add($hbox);
  return $frame;
}


sub config_window {
  my $window = Gtk2::Window->new();
  $window->set_title('TVStudio');
  my $vbox = Gtk2::VBox->new();

  $vbox->add(audio_only_inputs());
  $vbox->add(video_only_inputs());
  $vbox->add(video_overlays());

  my $hbox = Gtk2::HBox->new;
  $hbox->add(master_mix_box());
  $hbox->add(video_transitions());
  $vbox->add($hbox);

  my $status = Gtk2::Statusbar->new;
  $vbox->add($status);
  my $ctx = $status->get_context_id("TVStudio");
  $status->push($ctx, "Ready");
  $window->add($vbox);
  $window->show_all;
  $window->signal_connect('delete_event' => sub { Gtk2->main_quit; }) ;
  return $window;
}

config_window();
Gtk2->main;
