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

use Data::Dumper;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use Common::RSApp;

use lib '/app/tools/rps/lib';
use RPS::MapFace::Command::Main;
use RPS::MapFace::Form;
use RPS::DB::Item::MapFaceTempMapping;
use RPS::Command;
use RPS::CommandLog;
use base 'RPS::Command';

use constant kTemplate => '/app/tools/rps/templates/mapface/map.xsl';

sub cmd  { return "map"; }
sub area { return "mapface"; }

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

    # Give any inherited methods the first crack at this.
    # (This checks for login and basic access permissions).
    #
    my $response = $self->SUPER::execute();
    return $response if $response;

    my $fileID       = $self->getParam('file');
    my $mappingGroup = $self->getParam('mappingGroup');
    if ($mappingGroup) {
        my $status = RPS::DB::Item::MapFaceTempMapping->GetStatus( $fileID, $mappingGroup );
        if ( $status == RPS::DB::Item::MapFaceTempMapping::STATUS_AUTO_APPROVED ) {
            $mappingGroup = RPS::DB::Item::MapFaceTempMapping->GetNextViewableGroupNum( $fileID, $mappingGroup );
            if ($mappingGroup) {
                my %mapParams = (
                    file         => $fileID,
                    mappingGroup => $mappingGroup,
                );
                my $command = RPS::MapFace::Command::Map->new(%mapParams);
                return RSApache::Response::Redirect->new( $command->getRedirect() );
            }
        }
    } else {
        $mappingGroup = RPS::DB::Item::MapFaceTempMapping->GetFirstViewableGroupNum($fileID);

        # We may want to overwrite this with either the first mappable or first approvable group num.
        # If there is one, of course.
        if ( $self->getParam('map') ) {
            my $mappableGroup = RPS::DB::Item::MapFaceTempMapping->GetFirstMappableGroupNum($fileID);
            if ($mappableGroup) {
                $mappingGroup = $mappableGroup;
            }
        } elsif ( $self->getParam('approve') ) {
            my $approvableGroup = RPS::DB::Item::MapFaceTempMapping->GetFirstApprovableGroupNum($fileID);
            if ($approvableGroup) {
                $mappingGroup = $approvableGroup;
            }
        }
    }

    # We want to skip over temp mappings with a status of auto-approved.
    # So first, let's make sure we have some.
    if ( !$mappingGroup ) {
        my $command = RPS::MapFace::Command::Main->new();
        return RSApache::Response::Redirect->new( $command->getRedirect() );
    }

    my $form = RPS::MapFace::Form->new( fileID => $fileID, mappingGroup => $mappingGroup );

    if ( $self->getParam('formSubmit') ) {
        if ( $form->assignCGIParams( 'Form', $self->getParams() ) && $form->validate() ) {
            $form->save();
            RPS::CommandLog::Log( $self, $id );

            # We want to go to the next mapping group.  The form will redirect us back to the first group
            # if there isn't really a next one.
            #
            my $successMessage = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
            my $nextGroup = RPS::DB::Item::MapFaceTempMapping->GetNextViewableGroupNum( $fileID, $mappingGroup );
            if ( !$nextGroup ) {

                # Go back to the first page, I guess?
                $nextGroup = RPS::DB::Item::MapFaceTempMapping->GetFirstViewableGroupNum($fileID);
            }
            my %mapParams = (
                file         => $fileID,
                mappingGroup => $nextGroup,
                msg          => $successMessage->toString(),
            );
            my $command = RPS::MapFace::Command::Map->new(%mapParams);
            return RSApache::Response::Redirect->new( $command->getRedirect() );
        } else {
            $form->setError(Common::FormObject::kErrFormSubmit);
            $self->addMessageXML( type => "error", code => Common::FormObject::kErrFormSubmit );
        }
    }

    $self->{xml}{Form} = $form;

    $response = RSApache::Response::XSLT->new( $self->{xml}, kTemplate );

    return $response;
}

1;
