package SIP::Session;

# Version number
$VERSION = 0.01;

use strict;

use SIP::User;
use SIP::Utility;

###############################################################################
sub new {
  my $class = shift;
  my (%param) = (@_);

  my $dbh = $param{'dbh'};

  return unless $dbh;

  my $this = { dbh => $dbh, };

  bless $this, $class;

  $this->set(%param);

  return $this;
}

################################################################################
sub init {
  my $this = shift;

  foreach my $k (keys %{$this}) {
    delete $this->{$k} unless ($k eq 'dbh');
  }
}

############################################################
sub set {
  my $this = shift;

  return unless (@_);

  my (%param) = (@_);

  $this->init();

  my $dbh = $this->{'dbh'};

  $this->{'session_id'} = $param{'session_id'};

  $this->{'user'} = SIP::User->new(
    dbh       => $dbh,
    patron_id => $param{'patron_identifier'}
  );

  $this->{'record'} =
    SIP::Utility->getRecordInfo(
      dbh              => $dbh,
      item_identifier  => $param{'item_identifier'}, 
      title_identifier => $param{'title_identifier'}
    );
}

############################################################
sub get {
  my $this = shift;
  my ($key) = @_;

  return $this->{$key};
}

############################################################
sub validate {
  my $this = shift;
  my (%param) = (@_);

  $this->{'isValid'} = 0;

  $this->{'screen_message'} = '';

  # Always validate session ID by default
  if (!$this->_validate_session()) {
    $this->{'screen_message'} = 'Invalid connection.';
    return;
  }

  if ($param{'patron_identifier'} && !$this->{'user'}->getPatronIdentifier()) {
    $this->{'screen_message'} = 'Invalid user\'s barcode.';
    return;
  }

  if ($param{'account_expiry'} && $this->{'user'}->isExpired() eq 'true') {
    $this->{'screen_message'} = 'Account expired.';
    return;
  }

  my ($ii, $ti) = ($param{'item_identifier'}, $param{'title_identifier'});
  if (($ii || $ti) && !$this->_validateRecord()) {
    $this->{'screen_message'} = ($ii) ? 'Invalid barcode.' : 'Invalid title.';
    return;
  }

  $this->{'isValid'} = 1;
}

############################################################
sub isValid {
  my $this = shift;

  return $this->{'isValid'};
}

############################################################
sub _validate_session {
  my $this = shift;

  my $dbh        = $this->{'dbh'};
  my $session_id = $this->{'session_id'};

  my ($thePretender, $userIP);
  $userIP = $ENV{'REMOTE_ADDR'};
  if ($userIP =~ m/^199\.202\.10(0\.23[6-9]|1\.2(4[89]|5[0-4]))$/) {
#    $thePretender = 1;
  }

  return 1 if $thePretender;

  my @val = ($session_id);
  my $s = $dbh->selectrow_hashref(<<_SQL_, undef, @val);
select  *
from    sip_session
where   sessionid = ?
_SQL_

  if (!$s) {
    $this->{'debug'} = 'false';

    return;
  }

  $this->{'debug'} = ($s->{'debug'} =~ m/true|false/i)
      ? $s->{'debug'} : 'false';

  return $s->{'ipaddress'};
}

############################################################
sub _validateRecord {
  my $this = shift;

  return ($this->{'record'}->{'rid'} > 0);
}

1;
