#!/usr/bin/perl -w
use strict;
use Gtk2 -init;
use Data::Dumper;
use threads;
use Parser;
$|++;

do "conf.pl";


my $parser = new Parser;
$parser->set_data_dir("data");
$parser->set_feeds_dir("feeds");
$parser->set_config_file("config");
$parser->set_save_old(12);
$parser->set_browser("firefox");
$parser->set_defil_timer(100);
$parser->set_update_timer(10000);
$parser->start_parse("data");
$parser->generate_c_texte;

my $position  =0;
my $busy=1;

sub update_defil()
{
	$parser->update_parse;  
	$parser->update_file;  	
        $parser->generate_c_texte;  
}



		    





#window 
    my $window = Gtk2::Window->new ('toplevel');
    $window->signal_connect('delete_event', \&aurevoir);
    $window->signal_connect('destroy', \&destroy);
    $window->set_title('Gtkrss by tfe');
    $window->set_default_size(475,100);
    $window->set_position('mouse');
#box
    my $box = Gtk2::VBox->new(0,0);
    $box->set_border_width(15);

    #box_top
    my $title_label = Gtk2::Label->new('Gtkrss by tfe');
#    $box->pack_start($title_label,1,1,5);
#    $title_label->show();

	my $event_box = new Gtk2::EventBox();
	$box->pack_start($event_box,1,1,0);
	$event_box->show();
	$event_box->set_events('button_press_mask');
	$event_box->signal_connect('button_press_event','open_news');	
		
        my $box_label = Gtk2::HBox->new(0,0);
	$event_box->add($box_label);
        $box_label->show();
	my $etat_cache = 0;
	my $label  = Gtk2::Label->new('-'x55);
	$label->set_max_width_chars(80);
	$label->set_justify('center');
	
	my $cache_button = Gtk2::Button->new();
	$cache_button->signal_connect('clicked','hide_buttons');
	my $cache_arrow = Gtk2::Arrow->new('up','in');
	my $afficher_arrow = Gtk2::Arrow->new('down','in');	
	$cache_button->add($cache_arrow);
	
	$afficher_arrow->show();
	$cache_arrow->show();
	$box_label->pack_start($label,1,1,10);
	$box_label->pack_end($cache_button,0,1,10);		
	$label->show();
	$cache_button->show();







        #label
    #box_bottom
	my $box_b = Gtk2::HBox->new(0,0);
	#sub_box
	$box->pack_start($box_b,1,1,0);
	$box_b->show();

		my $button = Gtk2::Button->new('Contenu');
		$button->signal_connect('clicked',"conf",$parser);
		$box_b->pack_start($button,1,1,0);
		$button->show();

		my $config_box = Gtk2::VBox->new(0,0);
		$box_b->pack_start($config_box,1,1,0);
		$config_box->show();

		my $refresh = Gtk2::Button->new('Afficher tous');
	    	$refresh->signal_connect('clicked',sub { $parser->read_none; &update_defil; } );
		$config_box->pack_start($refresh,1,1,0);
		$refresh->show();
		
		my $conf_button = Gtk2::Button->new('Cacher deja lus');
	    	$conf_button->signal_connect('clicked',sub {  $parser->read_all ; &update_defil;});
		$config_box->pack_start($conf_button,1,1,0);
		$conf_button->show();
		
		

    $window->add($box);
    $box->show();
    $box_b->show();
    $window->show();
    
    
    
sub hide_buttons()
{
     if ($etat_cache ==0) 
     { 
       $cache_button->remove($cache_arrow);
       $cache_button->add($afficher_arrow);	
       $etat_cache=1;
       $box_b->hide();
     }
     else 
     {
       $cache_button->remove($afficher_arrow);
       $cache_button->add($cache_arrow);	
       $etat_cache=0;
       $box_b->show();       
    }
    $window->resize(450,10);
}


sub defilement()
{

    if (scalar (@{$parser->c_texte})<=55) 
	{ 
	    $label->set_markup(join "" ,@{$parser->c_texte}); $position=0;
	}
    else
    {
        $position++;
    if ($position >= scalar (@{$parser->c_texte}))  { $position = 0; }
    if ($position >= scalar(@{$parser->c_texte})- 55) 
    { 
#	print "$position a ".(scalar @{$parser->c_texte}-1)." et 0 a ".(55-(scalar @{$parser->c_texte}-$position-1))."\n";
        $label->set_markup(
	    (join "" ,@{$parser->c_texte}[$position..scalar @{$parser->c_texte}-1]).
	    (join "" ,@{$parser->c_texte}[0..55-(scalar @{$parser->c_texte}-$position-1)])
	    );
    }
    else
    {
    $label->set_markup(join "" ,@{$parser->c_texte}[$position..
              (scalar @{$parser->c_texte}> 54 ? $position+55 : scalar @{$parser->c_texte}-1)]);
    }
    
    }
    return 1;
}


sub open_news()
{
    if ($parser->get_news_title_ref)
    {

    print "\t>> Current position : $position -> ";
    my $current=0;
    foreach(@{$parser->get_news_title_ref})
    {
	if ($_ < $position) { $current = $_ ; }
	if ($_ > $position) { last; }
    }
    my %urls = %{$parser->get_news_url_ref};
    print " news\t$current ".${$urls{$current}}."\n";
    system($parser->get_browser." ".${$urls{$current}}." &");
    }
}






    
my $pid;
if ($pid = fork) 
{
	while(1)
	{
	print "UPDATE !\n";
#        &get_files($parser);
	select(undef,undef,undef,60*10);
	print "FIN DE MISE A JOUR !!\n\n\n";
	}
}
else
{
	&update_defil;
	my $up_defile_timer = Glib::Timeout->add($parser->get_update_timer, "update_defil" ) ;
	
	my $defil_timer = Glib::Timeout->add($parser->get_defil_timer, "defilement" ) ;
	
        Gtk2->main;
}


sub stop_def()  {   }
sub aurevoir(){ return 0;}
sub destroy(){Gtk2->main_quit;return 0;}
