package RSApache::RSWebApp;

use strict;
use warnings;
use CGI;
use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Permission;
use RSApache::DBSession;
use Common::RSApp;
use base 'Common::RSApp';

use constant kCommandToken => 'c';

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

    assert( $args{apache} );

    # Call the inherited method first.
    #
    $self->SUPER::_init(%args);

    # JPK - Presumably, any additional static methods
    # this class will provide will have something to do
    # with apache...
    #
    $self->{apache}  = $args{apache};
    $self->{appName} = $self->{apache}->dir_config('app');

    return $self;
}

#
# - Static methods
#
sub GetHttpHost {
    return $ENV{HTTP_HOST};
}

sub GetApache {
    return Common::RSApp::Instance()->{apache};
}

sub GetAppName {
    return Common::RSApp::Instance()->{appName};
}

sub IsSessionValid {

    # It seems like a bad idea to cache the session object here..
    #
    my $session = new RSApache::DBSession();

    # if client id is 0 then even if the user has a valid session
    # we need the user to select a client site to use
    #
    return $session->IsValid() && Common::RSApp::GetClientID() > 0;
}

sub GetSession {
    return new RSApache::DBSession();
}

# JPK - This became generic enough for me to justify moving
# this code into the base class.
#
# Derived classes need to override the _getCommandMap() method to
# return a hashref that maps the command parameter to a package name.
#
sub CreateCommand {
    my ( $pkg, $cgi ) = @_;

    assert( defined $cgi );

    # Figure out which screen/area we want.
    # ex: http://www.royaltyshare.com/rps/workbench?cmd=show
    #                                 ^-app  ^       ^
    #                                        |- area |
    #                                                |-command
    #
    # We also now allow the command to be part of the path:
    # ex: http://www.royaltyshare.com/rps/workbench/show
    #                                 ^-app  ^       ^
    #                                        |- area |
    #                                                |-command
    my $url  = $ENV{REQUEST_URI};
    my $area = $pkg->_parseArea($url);
    my $cmd  = $pkg->_parseCommand($url);
    if ( !$cmd ) {
        $cmd = $cgi->param(kCommandToken);
    }

    # Temp fix.  Should be handled in the derived classes
    $cmd = 'DEFAULT' unless $cmd;

    my $cmdPkg = $pkg->GetCommandPkg( $area, $cmd );
    die "No command found for $area / $cmd" if !$cmdPkg;

    eval "require $cmdPkg";
    die $@ if $@;

    my $commandObj = $cmdPkg->new( cgi => $cgi );

    return $commandObj;
}

sub GetCommandPkg {
    my ( $pkg, $area, $cmd ) = @_;

    my $self = Common::RSApp::Instance();
    my $map  = $self->_getCommandMap();

    my $cmdPkg = $map->{$area}{$cmd};

    return $cmdPkg;
}

sub _parseArea {
    my ( $pkg, $url ) = @_;

    my ( $path, $args ) = split( '\?', $url );
    my @bits = split( '\/', $path );
    return $bits[2];

    #	return $bits[(scalar @bits) - 1];
}

sub _parseCommand {
    my ( $pkg, $url ) = @_;

    my ( $path, $args ) = split( '\?', $url );
    my @bits = split( '\/', $path );
    return $bits[3];

    #	return $bits[(scalar @bits) - 1];
}

sub _parseApp {
    my ($url) = @_;

    my ( $path, $args ) = split( '\?', $url );
    my @bits = split( '\/', $path );
    return $bits[1];
}

###
1;    # Play nicely.
###
