#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Command;

#
# This class is intended to be a base class for all the commands in RPS.
#

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Cookie::PersistentCookie;
use RSApache::Command::SessionedCommand;
use base 'RSApache::Command::SessionedCommand';

use lib '/app/tools/rps/lib';
use RPS::Message;

sub app { return 'rps'; }

sub _maxPageSize       { 1_000_000_000 }
sub _stickyParamPrefix { }
sub _stickyParams      { {} }

sub _preprocessStickyParams {
    my $self = shift;
    my $stickyPrefix  = shift // '';
    my $hStickyParams = shift || {};

    my $oCookie = Common::Cookie::PersistentCookie->new();
    my $hParams = $self->getParams();

    foreach my $key ( keys %$hStickyParams ) {
        my $cgiValue    = $hParams->{$key};
        my $cookieValue = $oCookie->get( "$stickyPrefix$key" );

        # update cookie value
        if ( defined $cgiValue && defined $cookieValue ) {
            next if $cgiValue eq $cookieValue;

            $hParams->{$key} = $cgiValue;
            $oCookie->set( "$stickyPrefix$key", $cgiValue );
        }
        # set cgi value
        elsif ( defined $cgiValue && !defined $cookieValue ) {
            $oCookie->set( "$stickyPrefix$key", $cgiValue );
        }
        # set cookie value
        elsif ( !defined $cgiValue && defined $cookieValue ) {
            $hParams->{$key} = $cookieValue;
        }
        # set default
        elsif ( !defined $cgiValue && !defined $cookieValue ) {
            $hParams->{$key} = $hStickyParams->{$key};
            $oCookie->set( "$stickyPrefix$key", $hStickyParams->{$key} );
        }
    }

    $oCookie->store() if %$hStickyParams;

    return $hParams;
}

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

    my $response = $self->SUPER::execute();
    return $response if $response;

    my $userID = Common::RSApp::GetActiveUserID();
    my $user   = AppUser::DB::Item::User->Lookup( user_id => $userID );
    if ( $user->must_change ) {
        # The must_change flag means that only the change password page (/rps/login?c=changepw)
        # is valid.
        # The flag will be cleared once the user successfully updates their password.
        # If the user tries to access any other area or command then kill the session and force
        # the user back to the login page.
        #
        my $cmd = $self->getParam('c') || $self->cmd();
        if ( $self->area() ne 'login' || ($self->cmd() ne 'changepw' && $cmd ne 'changepw' ) ) {

            my $session = RSApache::RSWebApp::GetSession();
            if ( $session->IsValid() ) {
                if ( $session->End() ) {
                    print STDERR "RPS::Command -- user_id $userID session ended\n";
                } else {
                    print STDERR "RPS::Command -- user_id $userID session end failed: ".$session->errstr."\n";
                }
            }
            $response = RSApache::Response::Redirect->new('/');
            return $response;
        }
    }
    return undef;
}

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

