package Support::Implementation::Template;

# 4/30/10 - Added license_file to kClassAttributes
# 6/20/12 - Added Excel 2007
use warnings;
use strict;
use Carp;

use Data::Dumper;

use lib '/app/tools/rps/lib';
use lib '/app/tools/metadata/lib';
#use base 'Common::XMLObject';

use vars qw($AUTOLOAD);
use constant kClassAttributes => qw( name exec_mode client_id license_file dbo );

sub new {
   my $class = shift;
   my $self = {@_};
   bless($self, $class);
   $self->_init;
   return $self;
}

#sub _init {
#   my $self = shift;
#   # TODO
#}

sub _init {
   my( $self, %args ) = @_;

   print("Template::_init -- args = ". Dumper(\%args) ."\n");

   foreach( (kClassAttributes) ) {
      $self->{_attributes}->{$_}++;
   }

   foreach( keys(%args) ) {
      $self->$_( $args{$_} ) if( $self->{_attributes}->{$_} );
   }

#   $self->SUPER::_init(%args);
   return $self;
}

sub DESTROY { }

sub AUTOLOAD {
   my $self = shift;
   my ($value) = @_;
   my $field = $AUTOLOAD;
   $field =~ s/.*://; # Strips fully-qualified portion.

   my $attr = "_$field";

   print("AUTOLOAD($field)\n");
   return if $field eq 'DESTROY'; # skip DESTROY and all-cap methods
   unless( $self->{_attributes}->{$field} )
   {
      my $superior = "SUPER::$attr";
      $self->$superior(@_);
   }

   # All preconditions are validated.  Let's do some work.

   # If a value is sent in, then set it.
   $self->{$attr} = $value if ( $value );

   return $self->{$attr};
}

sub isExcel2003 {
   my $self = shift;
   my $fileName = $self->name;
   my $retVal = 0;
   print "Template.pm: fileName($fileName)\n";
   if ( $fileName =~ m/\.xls$/i ) {
      $retVal=1;
      $self->{excel_2003} = 1;
   }
   return $retVal;
}

sub isExcel2007 {
   my $self = shift;
   my $fileName = $self->name;
   my $retVal = 0;
   print "Template.pm: fileName($fileName)\n";
   if ( $fileName =~ m/\.xlsx$/i ) {
      $retVal=1;
      $self->{excel_2007} = 1;
   }
   return $retVal;
}

sub isTabDelimited {
   my $self = shift;
   my $fileName = $self->name;
   my $retVal = 0;
   my $file = IO::File->new($fileName) ||
      die("Template.pm -- file not found: $fileName");
   my $firstRow = <$file>;
   my(@field) = split("\t", $firstRow);
   if ( @field > 0 ) {
      $retVal = 1;
   }
   $file->close();
   return $retVal;
}

# Read-in all of the template data into memory
sub loadMemory {
   my $self = shift;
}

sub addRow {
   my $self = shift;
   my $data = shift;
}
1;
