# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;
use File::Basename;
use lib dirname(__FILE__);
use Data::Dumper;

use Bref;

my $brefbot = Bref->new();

$brefbot->download_all_episodes();

my $iconv="/opt/bin/iconv";
my $TABLEAU="tableau.html";

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year+=1900;
my $date = "$year$mon$mday$hour$min$sec";

rename "$TABLEAU", "$TABLEAU.$date";

open TABLEAU, ">$TABLEAU" or die "Can't open $TABLEAU";
my $list = $brefbot->get_episodes();
my $max = scalar keys %$list;
foreach my $episode_id (sort { $b <=> $a } keys %$list) {
 my $episode = $list->{$episode_id};
	print TABLEAU generate_html($episode,--$max);
}
close TABLEAU;


sub generate_html {
	my $ep = shift;
 my $id = shift;
	my $html  = "<tr>\n";
	$html .= " <td>\n";
	$html .= "  <img src=\"".$ep->{image_small}."\"  width=\"145\" height=\"109\"/>\n";
	$html .= " </td>\n";
	$html .= " <td>\n";
	$html .= "  <b>".$ep->{titre}."</b> (".$ep->{vues}." vues ; Note: ".$ep->{note_moyenne}."/5 )<br/>\n";
	$html .= "Episode du ".$ep->{date}."<br/>\n";
	$html .= "[<a href=\"http://service.canal-plus.com/video/rest/getVideosLiees/cplus/".$ep->{id}."\">XML</a> | <a href=\"".$ep->{url}."\">Flash</a>]</a><br/><br/>\n";
	$html .= "vlc \"".$ep->{video_hd}."\" \n";
	$html .= " </td>\n";
	$html .= "</tr>\n\n";
	return $html;
}

