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

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

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::RSApp;

use lib '/app/tools/common/lib';
use Common::DB::Item::Permission;
use Common::DB::Item::AccessLevel;

use lib '/app/tools/rps/lib';
use Common::FormObject;

use base 'Common::FormObject';

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

    $self->SUPER::_init(%args);

    my %properties;
    if ( $args{_dbItem} ) {
        $self->_propertiesFromDBItem( \%properties, $args{_dbItem} );
    } elsif ( $args{application} && $args{area} && $args{command} && $args{accessLevelID} ) {
        $self->_propertiesFromDB( \%properties, $args{application}, $args{area}, $args{command}, $args{accessLevelID} );
    } else {
        $self->_propertiesFromDefaults( \%properties, $args{app}, $args{area}, $args{command}, $args{accessLevelID} );
    }

    $self->_initProperties( \%properties );
    $self->_initSubs( \%properties );

    return $self;
}

sub _propertiesFromDB {
    my ( $self, $hrProperties, $app, $area, $cmd, $accessLevel ) = @_;

    my $dbItem = Common::DB::Item::Permission->Lookup(
        application     => $app,
        area            => $area,
        command         => $cmd,
        access_level_id => $accessLevel
    );
    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

sub _propertiesFromDBItem {
    my ( $self, $hrProperties, $dbItem ) = @_;

    $hrProperties->{application}     = $dbItem->application;
    $hrProperties->{area}            = $dbItem->area;
    $hrProperties->{command}         = $dbItem->command;
    $hrProperties->{access_level_id} = $dbItem->access_level_id;
    $hrProperties->{enabled}         = $dbItem->enabled;

    $self->{_inDB} = 1;
}

sub _initProperties {
    my ( $self, $props ) = @_;

    $self->{Application} = Common::FormObject::Scalar::String->new( value => $props->{application}, maxlength => 32, readOnly => 1 );
    $self->{Area}        = Common::FormObject::Scalar::String->new( value => $props->{area},        maxlength => 32, readOnly => 1 );
    $self->{Command}     = Common::FormObject::Scalar::String->new( value => $props->{command},     maxlength => 32, readOnly => 1 );
    $self->{AccessLevelID} = Common::FormObject::Scalar->new( value => $props->{access_level_id}, readOnly => 1 );
    $self->{Enabled} = Common::FormObject::Scalar::Boolean->new( value => $props->{enabled} );
}

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

    # !!! Here's an issue - these command names have '_' in them, but we use '_' when parsing CGI keys.
    # !!! In retrospect, '_' is a lousy choice for a delimeter...
    # !!! I think I can re-map it, though.
    #
    my $key = $self->Application() . '.' . $self->Area() . '.' . $self->Command() . '.' . $self->AccessLevelID();

    $key =~ s/_/-/g;
    return $key;
}

sub _initSubs {
    my ( $self, $props ) = @_;

}

sub _propertiesFromDefaults {
    my ( $self, $hrProperties, $app, $area, $cmd, $accessLevelID ) = @_;

    assert($app);
    assert($area);
    assert($cmd);
    assert($accessLevelID);

    $hrProperties->{application}     = $app;
    $hrProperties->{area}            = $area;
    $hrProperties->{command}         = $cmd;
    $hrProperties->{access_level_id} = $accessLevelID;

    # This property is used to tell the template that this access level is enabled.
    #
    $hrProperties->{enabled} = 0;
}

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

    return 1;
}

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

    my $dbItem;
    if ( $self->{_inDB} ) {
        $dbItem = Common::DB::Item::Permission->Lookup(
            application     => $self->Application(),
            area            => $self->Area(),
            command         => $self->Command(),
            access_level_id => $self->AccessLevelID()
        );

    } else {
        $dbItem = Common::DB::Item::Permission->Create(
            application     => $self->Application(),
            area            => $self->Area(),
            command         => $self->Command(),
            access_level_id => $self->AccessLevelID()
        );
    }

    if ( $dbItem->access_level_id == 1 ) {
        $dbItem->enabled(1);
    } else {
        $dbItem->enabled( $self->Enabled() );
    }
    $dbItem->save();
    $self->{_inDB} = 1;
}

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