#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;
use Opals::Constant;
use Date::Calc::Object qw(
    :all
);
use Time::localtime;

use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::User qw(
    user_currentUser
    user_permission
    user_permission_1
    user_StrPermission
    user_StrPermission_1

);
use Opals::Circulation qw(
    eCirc_isOnLoanTo
);
use Opals::WebServiceEbookClient qw( 
    wsc_getEbookPreview
);

################################################################################
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();
my $bid=$input->{'bid'};
my $rdrStyle=$input->{'style'};
if(!$rdrStyle){
    $rdrStyle='simple';
}
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'ebook/preview.tmpl',
        }
);


#if($loginuid){
    my $params={bid=> $bid};
    my $ebook = wsc_getEbookPreview($params);

    my $toc = $ebook->{'TOC'};
    my $title = $ebook->{'metadata'}->{'title'};
    my $author= $ebook->{'metadata'}->{'author'};
    my $spine=$ebook->{'spine'};

    my $lastspineFid=@$spine[-1]->{'spineItem'};
    my $i=0;
    foreach my $t(@$toc){
        $i++;
        last if($t->{'uri'} =~ m/$lastspineFid/)   ;
    }
    while($i< scalar(@$toc)){
        @$toc[$i++]->{"uri"}=EBOOK_PREVIEW_EOB;
    }
    #push @$spine,{spineItem =>EBOOK_PREVIEW_EOB};
    $template->param(bid                =>$bid,
                     title              =>$title,
                     author             =>$author,
                     spine              =>$spine,
                     toc                =>$toc,
                     EBOOK_EOB         =>EBOOK_PREVIEW_EOB,
                     rdrStyle           => $rdrStyle);
#}

tmpl_write($dbh, $cgi, $cookie, $template);

################################################################################

sub getBook{
    my ($dbh,$bid)=@_;
    my $sth=$dbh->prepare("select title,author from eb_record where bid=?");
    $sth->execute($bid);
    if(my ($title,$author)=$sth->fetchrow_array){
        return ($title,$author);
    }
    return (undef,undef);
    
}
################################################################################

sub getBookToc{
    my ($dbh,$bid)=@_;
    my $sth=$dbh->prepare("select * from eb_toc where bid=? order by playOrder");
    $sth->execute($bid);
    my $toc=[];
    while(my $rec=$sth->fetchrow_hashref){
        push @$toc,$rec;
    }
    return $toc;
    
}
################################################################################
sub getBookSpine{
    my ($dbh,$bid,$prvPercentage)=@_;
    my $spine=[];
    my $sth=$dbh->prepare("select sum(length(content)) from eb_spine s inner join eb_file f on s.fid=f.fid and s.bid=f.bid   where s.bid=?");
    my $result = $sth->execute($bid);
    return $spine if($result eq '0E0');
    my($size) =$sth->fetchrow_array;
    my $maxPrvSize=$size*$prvPercentage;
    my $prvSize =0;
    $sth=$dbh->prepare("select s.fid,length(content) from eb_spine s inner join eb_file f on s.fid=f.fid and s.bid=f.bid where s.bid=? order by s.playOrder");
    $sth->execute($bid);
    while(my ($fid,$contSize)=$sth->fetchrow_array){
        $prvSize += $contSize;
        push @$spine,{spineItem=>"/ebook/$bid/$fid"};
        last if($prvSize>$maxPrvSize);
    }
    return $spine;
    
}
################################################################################

