#!/usr/bin/perl
use utf8;
use Encode 'decode';
use warnings;
use CGI;
use File::Basename;
use DBI;
use DBD::SQLite;
use Data::Dumper;
use YAML::XS;
use URI::Escape;
use HTML::Template::Pro;

#use KCatch;

#   Licensed under the Apache License,
#
#   Libre10
#
#   Copyright 2013 yukikaze
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
$cgi = new CGI;
my $confdata = YAML::XS::LoadFile(
	-e 'libre10.conf' ? 'libre10.conf' : '/etc/libre10.conf' );

$mode        = decode( 'utf-8', $cgi->param('mode') );
$pagenum     = int( $cgi->param('pagenum') );
$title_row   = $cgi->param('title');
$title_row   = uri_unescape($title_row);
$title_group = decode( 'utf-8', $title_row );
$title_uri   = uri_escape($title_row);
$html_mode   = $cgi->param('htmlmode');

$height = $cgi->param("height");
$width  = $cgi->param("width");
$dbpath = $confdata->{dburl};

$dbi =
  DBI->connect( "dbi:SQLite:dbname=$dbpath", "", "",
	{ RaiseError => 1, AutoCommit => 1 } );
$multi_maxpage = 0;

$sql = "SELECT id, title, part, startpage, endpage, showpagediff, indexpage
	FROM pdffile 
	WHERE title_id = '$title_group' 
	ORDER BY part DESC LIMIT 1";
$dbs = $dbi->prepare($sql);
$dbs->execute();
$dbh           = $dbs->fetchrow_hashref;
$multi_maxpage = $dbh->{'endpage'};
$indexpage = $dbh->{'indexpage'};
$showpagediff = $dbh->{'showpagediff'};
if ( !( $showpagediff > 0 ) ) {
	$showpagediff = "0";
}
$sql2 = "SELECT id ,startpage 
	FROM pdffile 
	WHERE title_id = '$title_group' AND startpage < $pagenum AND endpage > $pagenum";
$dbs2 = $dbi->prepare($sql2);
$dbs2->execute();
$dbh2           = $dbs2->fetchrow_hashref;
$single_pagenum = $pagenum - $dbh2->{'startpage'};
$pdfpath        = $dbh2->{'id'};
$jumplink =
"htmlserver.pl?title=$title_uri&mode=$mode&width=$width&height=$height&pagenum=";
$searchlink =
  "pdfsearch.pl?mode=$mode&width=$width&height=$height&search_text=";
$bpage = $pagenum - 1;
$blink =
"htmlserver.pl?title=$title_uri&mode=$mode&width=$width&height=$height&pagenum=$bpage";
$npage = $pagenum + 1;
$nlink =
"htmlserver.pl?title=$title_uri&mode=$mode&width=$width&height=$height&pagenum=$npage";
$b10page = $pagenum - 10;
$b10link =
"htmlserver.pl?title=$title_uri&mode=$mode&width=$width&height=$height&pagenum=$b10page";
$n10page = $pagenum + 10;
$n10link =
"htmlserver.pl?title=$title_uri&mode=$mode&width=$width&height=$height&pagenum=$n10page";
$homelink = "pdfsearch.pl?mode=$mode&width=$width&height=$height";
$dbi->disconnect;
$height = $cgi->param("height");
$width  = $cgi->param("width");

if ( $mode eq 'mobile' ) {
	$template =
	  HTML::Template::Pro->new(
		filename => './template/mobile_viewjpeg.tmp.html' );
	$jpeglink =
"jpegserver.pl?pdfpath=\"$pdfpath\"&pagenum=$single_pagenum&mode=$mode&height=$height&width=$width";
	$template->param( blink      => $blink );
	$template->param( nlink      => $nlink );
	$template->param( jpegpath   => $jpeglink );
	$template->param( jumplink   => $jumplink );
	$template->param( searchlink => $searchlink );
	$template->param( nowpage    => $pagenum );
	$template->param( maxpage    => $multi_maxpage );
	$template->param( homelink   => $homelink );
	$template->param( nowspd     => $showpagediff );
	#$template->param( title_id => $title_group );
}
else {
	$bpage = $pagenum - 2;
	$blink =
"htmlserver.pl?title=$title_uri&mode=$mode&width=$width&height=$height&pagenum=$bpage";
	$slidelink =
"htmlserver.pl?title=$title_uri&mode=$mode&width=$width&height=$height&pagenum=$npage";
	$npage = $pagenum + 2;
	$nlink =
"htmlserver.pl?title=$title_uri&mode=$mode&width=$width&height=$height&pagenum=$npage";
	$blink   = "$blink";
	$nlink   = "$nlink";
	$b10link = "$b10link";
	$n10link = "$n10link";
	$width   = $width / 2;
	$jpeglink =
"jpegserver.pl?pdfpath=\"$pdfpath\"&pagenum=$single_pagenum&mode=$mode&height=$height&width=$width";
	$single_pagenum2 = $single_pagenum + 1;
	$jpeglink2 =
"jpegserver.pl?pdfpath=\"$pdfpath\"&pagenum=$single_pagenum2&mode=$mode&height=$height&width=$width";
	$blink_jpegnum = $single_pagenum - 2;
	$blink_jpeg =
"jpegserver.pl?pdfpath=\"$pdfpath\"&pagenum=$blink_jpegnum&mode=$mode&height=$height&width=$width";
	$blink_jpegnum = $single_pagenum - 1;
	$blink_jpeg2 =
"jpegserver.pl?pdfpath=\"$pdfpath\"&pagenum=$blink_jpegnum&mode=$mode&height=$height&width=$width";
	$nlink_jpegnum = $single_pagenum + 2;
	$nlink_jpeg =
"jpegserver.pl?pdfpath=\"$pdfpath\"&pagenum=$nlink_jpegnum&mode=$mode&height=$height&width=$width";
	$nlink_jpegnum = $single_pagenum + 3;
	$nlink_jpeg2 =
"jpegserver.pl?pdfpath=\"$pdfpath\"&pagenum=$nlink_jpegnum&mode=$mode&height=$height&width=$width";
	$template =
	  HTML::Template::Pro->new(
		filename => './template/desktop_viewjpeg.tmp.html' );
	$template->param( blink       => $blink );
	$template->param( blink_jpeg  => $blink_jpeg );
	$template->param( blink_jpeg2 => $blink_jpeg2 );
	$template->param( nlink       => $nlink );
	$template->param( nlink_jpeg  => $nlink_jpeg );
	$template->param( nlink_jpeg2 => $nlink_jpeg2 );
	$template->param( slidelink   => $slidelink );
	$template->param( b10link     => $b10link );
	$template->param( n10link     => $n10link );

	#$template->param( cache_blink_data => $cache_blink_data);
	$template->param( jpegpath   => $jpeglink );
	$template->param( jpegpath2  => $jpeglink2 );
	$template->param( jumplink   => $jumplink );
	$template->param( searchlink => $searchlink );
	$template->param( nowpage    => $pagenum );
	$template->param( maxpage    => $multi_maxpage );
	$template->param( homelink   => $homelink );
	$template->param( title_id   => $title_group );
	$template->param( nowspd     => $showpagediff );
	$template->param( indexpage     => $indexpage );
}
print $cgi->header( -charset => 'utf-8' ), $template->output;
