#!@@_perl_root_@@/bin/perl.exe
package Reference;

##########################################################
#                                                        #
# dictyBase Extension of Reference                       #
#                                                        #
##########################################################

use Reference_base;

BEGIN { %Reference:: = %Reference_base:: }


#
# will eventually properly put this as a globally configurable variable
#
my $schema = $ENV{'DBUSER'};

sub new {
   my ($type, %args) = @_;
   my $self;

   if ($args{'dictyBaseid'}) {
      my $dictyBaseid = dictyBaseid::->new( %args );

      if ( $dictyBaseid->tab_name eq 'REFERENCE' ) {
         $self = $type->SUPER::new( reference_no => $dictyBaseid->primary_key, dbh => $args{'dbh'} );
         $self->dictyBaseid( $dictyBaseid->dictyBaseid );
      }
      else {
         die "did not pass real reference dictyBaseid";
      }
   }
   else {
      $self = $type->SUPER::new( %args );
      return if !$self;
   }
   $self->reference_types( $self->get_reference_types );

   return $self;
}


#
#  returns an array of author names for a given reference
#
#
######################################################################
sub authorNameArrayRef {
######################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
       SELECT A.author_name
       FROM   $schema.author A, $schema.author_editor AE
       WHERE  AE.reference_no = ?
       AND    AE.author_no = A.author_no
       ORDER BY AE.author_order
    ");
    $sth->execute($self->reference_no);
    my @authorArray;
    while(my ($author, $authorOrder) = $sth->fetchrow()) {
       push @authorArray, $author;
    }
    $sth->finish;
    return \@authorArray;
}

sub reference_types {
   my ($self, $obj) = @_;
   if($obj) {
   $self->{reference_types} = $obj;
   }
   return $self->{reference_types};
}

#
#
# make pubmed a popup
#
####################################################################
sub _formatCitation {
####################################################################
    my ($self, $citation, $image, $isErratum) = @_;
    if ($citation =~ /^(.+)(http:.+)$/) {
        $citation = $1 . a({-href=>"$2", -target=>'infowin'}, $2);
    }
    else {
        my $journal = $self->journal;
        if ($journal) {
            $citation =~ s/$journal/<i>$journal<\/i>/;
        }
    }
    my $configPath = ConfigPathdictyBase->new;
    my $configUrl = ConfigURLdictyBase->new;
    my $dblink = $configUrl->dblink($self->dbh->{Name});
    if ($image) {
        $citation .= br.a({-href=>$configUrl->dictyBaseCGIRoot."$dblink/reference/reference.pl?refNo=".$self->reference_no,
                           -target=>'infowin'},
                          img({-src=>$configUrl->dictyBaseImages."$image",
                               -border=>'0',
                               -alt=>"dictyBase Papers Entry"}))."&nbsp;&nbsp;";
    }
    else { $citation .= br; }

    my $pubmed =$self->pubmed;
    if ($pubmed) {
        $citation .= a({-href=>$configUrl->reference_popup( $configUrl->pubmed_url($pubmed)  )},
                          img({-src=>$configUrl->dictyBaseImages."pubmedrefsml.gif",
                               -border=>'0',
                               -alt=>"Pubmed Entry"}))."&nbsp;&nbsp;";
    }
    my $urlArrayRef = $self->urlArrayRef;
#    my ($url) = $$urlArrayRef[0]->[0];
#    if ($url) {
    foreach my $rowRef (@$urlArrayRef) {
        my ($url, $type) = @$rowRef;
        my $journalGif = "full_text.gif";
#        if ($type =~ /LINKOUT/i) {
#            $journalGif = "full_text.gif";
#        }
#        else {
#            $journalGif = $self->journal;
#            $journalGif =~ s/ /\./g;
#            $journalGif .= ".gif";
#        }
#        if (! -e $configPath->gifDir."$journalGif") {
#            $journalGif = "Generic.gif";
#        }
        $citation .= a({-href=>$configUrl->reference_popup($url)},
                       img({-src=>$configUrl->dictyBaseImages."$journalGif",
                            -border=>'0',
                            -alt=>"$type"}))."&nbsp;&nbsp;";
    }
    my $webSuppUrl = $self->webSupplementUrl;
    if ($webSuppUrl) {
        my @suppUrl = split(/\t/, $webSuppUrl);
        foreach my $suppUrl (@suppUrl) {
            $citation .= a({-href=>$configUrl->reference_popup($suppUrl)},
                       img({-src=>$configUrl->dictyBaseImages."webSupplement.gif",
                            -border=>'0',
                            -alt=>"Web Supplement"}))."&nbsp;&nbsp;";
        }
    }
    if (!$isErratum) {
        my $relatedRefArrayRef = $self->relatedRefArrayRef;
        if ($$relatedRefArrayRef[0]) {
            $citation .= a({-href=>$configUrl->dictyBaseCGIRoot."$dblink/reference/reference.pl?refNo=".$self->reference_no."&type=other",
                            -target=>'infowin'},
                           img({-src=>$configUrl->dictyBaseImages."C&E.gif",
                               -border=>'0',
                               -alt=>"dictyBase Curated Comments & Errata"}));
        }
    }
    if ($self->dictyBaseidForCommunityAnnotatedRef) {
        $citation .= a({-href=>$configUrl->dictyBaseCGIRoot."$dblink/DisplayCommuAnno?refNo=".$self->reference_no,
                        -target=>'infowin'},
                       img({-src=>$configUrl->dictyBaseImages."star.gif",
                            -border=>0}));


    }
    return $citation;

}


sub dictyBaseid {
   my ($self, $obj) = @_;

  #
  # fetches dictyBaseid from database (get_dictyBaseid) if dictyBaseid is not yet defined
  #
   defined $self->{dictyBaseid} || defined $obj || $self->get_dictyBaseid();

   if($obj) {
      $self->{dictyBaseid} = $obj;
   }
   return $self->{dictyBaseid};
}

sub get_dictyBaseid{
   my ($self) = @_;

   my $featno = $self->reference_no;

   my $sth = $self->dbh->prepare("
        SELECT DICTYBASEID
          FROM $schema.DICTYBASEID
         WHERE TAB_NAME         = 'REFERENCE'
           AND DICTYBASEID_TYPE = 'Primary'
           AND PRIMARY_KEY      = ?
   ");

   $sth->execute( $self->reference_no );
   my $records = $sth->fetchall_arrayref();

   my $getdictyBaseid = [ map { $_->[0] } @{ $records } ];

   $self->dictyBaseid($getdictyBaseid->[0]);
   $sth->finish();

}

sub get_reference_types{
   my ($self, $type) = @_;

   my $sth = $self->dbh->prepare("
        SELECT PUB_TYPE
          FROM $schema.PUBLICATION_TYPE
         WHERE REFERENCE_NO = ?
   ");

   $sth->execute( $self->reference_no );
   my $records = $sth->fetchall_arrayref();

   my $getTypes = [ map { $_->[0] } @{ $records } ];
   $sth->finish();
   return $getTypes;

}
####################################################################
sub formatedCitation4notYetTopic {
####################################################################
    my ($self) = @_;
    my $citation = $self->citation;
    $citation =~ s/^([^0-9]+\([0-9]{4}\))(.+)$/<b>$1<\/b>$2/;
#    $citation =~ s/^([^0-9]+\([0-9]{4}\))$/<b>$1<\/b> personal communication/;
    $citation =~ s/et al\./<i>et al\.<\/i>/;
    my $citation = $self->_formatCitation($citation, "papericon3.png");
    return $citation;
}

####################################################################
sub formatedCitation {
####################################################################
    my ($self) = @_;
    my $citation = $self->citation;
    $citation =~ s/^([^0-9]+\([0-9]{4}\))(.+)$/<b>$1<\/b>$2/;
#    $citation =~ s/^([^0-9]+\([0-9]{4}\))$/<b>$1<\/b> personal communication/;
    $citation =~ s/et al\./<i>et al\.<\/i>/;
    my $citation = $self->_formatCitation($citation, "dictyBaserefsml.gif");
    return $citation;
}