#!/usr/bin/perl

# This script is not finished.

#use utf8;
use strict;
use CGI;
use Encode;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Date::Calc::Object qw(
    :all
);
use Opals::Search qw(
    srch_searchRecord
);

use Opals::Marc21 qw(
    mc21_infoGeneral
);
use Opals::CurriculumStd qw(
        getLevelList_ny
        getCategoryList_ny
        getSbjStdList_ny
        getSubjectList_ny
        getStandardList_ny
        getBenchMarkList_ny
        get_nyById
        get_nyByIdList
        getDewey_1dec
        getDewey_2dec
);


use Time::localtime;

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

my $cgi = CGI->new;
my $input = $cgi->Vars();
my $op = $input->{'op'};
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'util/bibsrc/stdBibliography.tmpl',
            reqPermission   => (($op eq 'email')? 'email':'none'),
        }
);
my $tm = localtime;
my $todayStr = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);
my $pqf;

my $bibList = $cgi->cookie('nyStdRsList');
 #format bibList is   stdId +":" + rid +",";
$bibList =~ s/(^\n|\n$)//g;
my $zid = $input->{'zid'};
($zid && $zid >= 0) || ($zid = 0);
my @bibliography;
my $odd_row=1;
#-----------------------------
my ($summary,$callnumber);  


my $stdRS;
my $oddEven=0;

foreach my $stdId_rid (split (",", $bibList)){
    my @a = split(':',$stdId_rid);
    push @{$stdRS->{$a[0]}}, $a[1];
}

foreach my $stdCode (sort keys %{$stdRS}) {
    #loop all standard codes
    my $stdResources ;

    $stdResources = get_nyById($dbh,$stdCode );
    my @stdBib; 
    foreach my $rid (sort @{$stdRS->{$stdCode}}){         
    #loop all records are in the standard code    
        $summary ="";
        $pqf = "\@attr 1=12  $rid ";
            
        my ($resultSize, $marcxml) = srch_searchRecord($dbh, $zid, 'f', $pqf, $ENV{'Z_INDEX_BASE'}, 1);
        my %info = mc21_infoGeneral($marcxml->[0]);
        foreach my $sum (@{$info{'listSummary'}}) {
            $summary .=  $sum->{'val'};
        }

        $summary =  decode('utf8',$summary );
        my $bibInfo;
        $bibInfo->{'rid'}           = $rid;
        $bibInfo->{'callnumber'}    = $info{'itemList'}->[0]->{'callnumber'};
        $bibInfo->{'callnumber'}    =~ s/[\.\,]+$//g;
        $bibInfo->{'title'}         = $info{'title'};
        $bibInfo->{'title'}         =~ s/[\.\,]+$//g;
        $bibInfo->{'title_sub'}     = $info{'title_sub'};
        $bibInfo->{'title_sub'}     =~ s/[\.\,]+$//g;
        $bibInfo->{'author'}        = $info{'author'};
        $bibInfo->{'author'}        =~ s/[\.\,]+$//g;
        $bibInfo->{'pubPlace'}      =   $info{'place'};
        $bibInfo->{'pubPlace'}      =~ s/[\.\,]+$//g;
        $bibInfo->{'pubName'}       =   $info{'publisher'};
        $bibInfo->{'pubName'}       =~ s/[\.\,]+$//g;
        $bibInfo->{'pubDate'}       =   $info{'copyright'};
        $bibInfo->{'pubDate'}       =~ s/[\.\,]+$//g;
        $bibInfo->{'summary'}       = $summary;
        $bibInfo->{'summary'}       =~ s/[\.\,]+$//g;
        $bibInfo->{'note'}          = encode('utf8',$stdCode);
        
        $oddEven=$oddEven == 1?0:1;
        $bibInfo->{'oddEven'}       = $oddEven;
        
        
        push @stdBib, $bibInfo;

        }
        $stdResources->{'stdBib'} = \@stdBib;    
        push @bibliography, $stdResources;
 }

 
#-----------------------------
my $pref = tmpl_preference($dbh);

my $bibformat = $cgi->cookie('stdbibformat');
($bibformat && $bibformat =~ m/^(apa|mla|shelf)$/i) || ($bibformat = $pref->{'bibformat'});
($bibformat && $bibformat =~ m/^(apa|mla|shelf)$/i) || ($bibformat = 'mla');

#if($op eq 'ppreview' || $op eq 'email'){
#    my $sumformat = $input->{'sumformat'};
#    $template->param( sumformat   => $sumformat,);
#}
#else{
#    $template->param( sumformat   => 1,);
#}

if ( $bibformat eq 'shelf' )
    { @bibliography = sort {$a->{'callnumber'} cmp $b->{'callnumber'}} @bibliography; }
else { @bibliography = sort {$a->{'author'} cmp $b->{'author'}} @bibliography; }

foreach my $bibInfo (@bibliography){
    $bibInfo->{'odd'}=$odd_row;
    $odd_row =$odd_row?0:1;
}
$template->param(
    todayStr => $todayStr,
    bibliography    => \@bibliography,
    bibformatList   => [
        {
            val => 'mla',
            opt => 'MLA',
            sel => ($bibformat =~ m/^mla$/i) ? 1:0,
        },
        {
            val => 'apa',
            opt => 'APA',
            sel => ($bibformat =~ m/^apa$/i) ? 1:0,
        },
        {
            val => 'shelf',
            opt => 'Shelf',
            sel => ($bibformat =~ m/^shelf$/i) ? 1:0,
        },
    ],
    op_ppreview     => (($op eq 'ppreview') ? 1:0),
    op_email        => (($op eq 'email') ? 1:0),
    $bibformat      => 1,
    viewBib         => 1,
);

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