package PathInfo;
use Exporter;
@ISA = qw(Exporter);
use strict;

use CGI;
use CGI::Carp qw( fatalsToBrowser);

use MainIndex;        # for subjectGroup verification 

# Structure of PathInfo strings:
# ...../program.pl/informationType/argument0/argument1/argument2/......
# where informationType = 
#      printablePage, mainIndex, MostRecentSideIndex, WorkInProgressSideIndex
#      payPalSale
# and the arg values depend on the informationType
#      printablePage, MostRecentSideIndex and WorkInProgressSideIndex take no arguments.
#      mainIndex  : argument0 = subjectGroup
#      payPalSale : argument0 = sold object or service identifier
# Usage:
# i.   Create the object, determine the type of information being transmitted, validate arguments
#  my $pathInfoObj    = PathInfo->new($cgiObj);
#
# ii.  In cases where a subjectGroup value should have been passed but has not then
#      this may be set to a specific (default) value (and remove the error flag if set)
#           $pathInfoObj->setSubjectGroupAndResetError(subjectGroup);
#
# iii. test for informationType
#            if ( $pathInfoObj->printablePage() ) { .....
#            if ( $pathInfoObj->mainIndex() ) { .....
#            if ( $pathInfoObj->mostRecentSideIndex() ) { .....
#            if ( $pathInfoObj->workInProgressSideIndex() ) { .....
#            if ( $pathInfoObj->payPalSale() ) { .....
#
# iv.  get value of argument0 etc
#              my $pathInfo            = $pathInfoObj->pathInfo();
#              my $subjectGroup        = $pathInfoObj->subjectGroup();
#              my $payPalTheObjectSold = $pathInfoObj->payPalTheObjectSold();
#


sub new {
# calling sequence
# my $pathInfoObj    = PathInfo->new($cgiObj)

    my $invocant = shift @_;
    my $className = ref($invocant) || $invocant;
    my $self = {};
    bless ($self, $className);
    
    # and our cgi object
    my $cgiObj    = shift;
    
    $self->{CGI_OBJ}                        = $cgiObj;
    
    $self->{NO_PATH_INFO}                   = undef;
    $self->{ERROR}                          = undef;
    
    $self->{PATH_INFO}                      = undef;
    $self->{PRINTABLE_PAGE}                 = undef;
    $self->{MAIN_INDEX}                     = undef;
    $self->{MOST_RECENT_SIDE_INDEX}         = undef;
    $self->{WORK_IN_PROGRESS_SIDE_INDEX}    = undef;
    $self->{PAY_PAL_SALE}                   = undef;
    $self->{SUBJECT_GROUP}                  = undef;
    $self->{PAY_PAL_THE_OBJECT_SOLD}        = undef;
    
# get the path-info
    my $pathInfo                = $cgiObj->path_info;
    my ($dummy,$informationType,$argument0) =  split "/", $pathInfo;
    
    if (! $pathInfo) {
        $self->{NO_PATH_INFO}    = 1;
        return $self;
    } # end if        
    
    $self->{PATH_INFO}          = $pathInfo;
    
    if ($informationType eq "printablePage") {
        $self->{PRINTABLE_PAGE}                    = 1;
        return $self;
    } # end if
    
    if ($informationType eq "MostRecentSideIndex") {
        $self->{MOST_RECENT_SIDE_INDEX}            = 1;
        return $self;
    } # end if
    
    if ($informationType eq "WorkInProgressSideIndex") {
        $self->{WORK_IN_PROGRESS_SIDE_INDEX}      = 1;
        return $self;
    } # end if
    
    if ($informationType eq "mainIndex") {
    # main index can be called from a page having a subjectGroup side index
    # or one of the arbitrary indexes: MostRecentSideIndex and WorkInProgressSideIndex
        $self->{MAIN_INDEX}            = 1;
        if ( ! $argument0) {
            $self->{ERROR}            = "missing subjectGroup";
            return $self;
        } # end if
        
        if ( MainIndex::subjectGroupExists($argument0) ) {
            $self->{SUBJECT_GROUP}    = $argument0;
            return $self;
        } # end if
        
        if ( $argument0 eq "MostRecentSideIndex" ) {
            $self->{SUBJECT_GROUP}    = "MostRecentSideIndex";
            return $self;
        } # end if
        
        if ( $argument0 eq "WorkInProgressSideIndex" ) {
            $self->{SUBJECT_GROUP}    = "WorkInProgressSideIndex";
            return $self;
        } # end if
        
        $self->{ERROR}    = "invalid subjectGroup";
        return $self;
        
    } # end if
    
    if ($informationType eq "payPalSale") {
    # Note that we validate the contents of argument0 when 
        $self->{PAY_PAL_SALE}    = 1;
        if ( ! $argument0) {
            $self->{ERROR}    = "missing sold object id";
            return $self;
        } # end if
        $self->{PAY_PAL_THE_OBJECT_SOLD}    = $argument0;
        
    } # end if
    
    $self->{ERROR}                            = "unknown informationType";
    return $self;


} # end new


sub pathInfo {
my $self    = shift;
    return $self->{PATH_INFO};
} # end noPathInfo

sub noPathInfo {
my $self    = shift;
    return $self->{NO_PATH_INFO};
} # end noPathInfo

sub error {
my $self    = shift;
    return $self->{ERROR};
} # end error



sub setSubjectGroupAndResetError {
my $self    = shift;
    $self->{SUBJECT_GROUP}    = shift;
    $self->{ERROR}            = undef;
} # end setSubjectGroupAndResetError



sub printablePage {
my $self    = shift;
    return $self->{PRINTABLE_PAGE};
} # end printablePage

sub mainIndex {
my $self    = shift;
    return $self->{MAIN_INDEX};
} # end mainIndex

sub mostRecentSideIndex {
my $self    = shift;
    return $self->{MOST_RECENT_SIDE_INDEX};
} # end mostRecentSideIndex

sub workInProgressSideIndex {
my $self    = shift;
    return $self->{WORK_IN_PROGRESS_SIDE_INDEX};
} # end workInProgressSideIndex

sub payPalSale {
my $self    = shift;
    return $self->{PAY_PAL_SALE};
} # end payPalSale


sub subjectGroup {
my $self    = shift;
    return $self->{SUBJECT_GROUP};
} # end groupId

sub payPalTheObjectSold {
my $self    = shift;
    return $self->{PAY_PAL_THE_OBJECT_SOLD};
} # end payPalTheObjectSold










1;