use strict;
use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';
use lib '/app/tools/report/lib';

use Common::RSApp;
use RPS::Report::Dynamic::Factory;
use Data::Dumper;
use Common::WriteXML;

use Report::DB::Item::ReportParameter;

my $app = Common::RSApp->new( clientID => 60 );

# Here's how you get the Group and Type XML to build
# the two menus on the 'choose report type' page.
#
my $xml = RPS::Report::Dynamic::Factory->GetGroupTypeXML(1);
print Common::WriteXML::GetXMLString($xml) . "\n";

# Here we fetch the Class, given a Group and Type.
# So this is how the ConfigureReport command gets started,
# since it will receive those as cgi params.
#
my $class = RPS::Report::Dynamic::Factory->ClassFromGroupType( 'Test Group', 'Test Type One' );
print "class: $class\n";

# Because perl is magic, we can invoke Class method on the string holding the class name.
# So here we get a blank ReportParameters object from the Class.
# This is a FormObject ready to go and receive parameters from
# a 'assignCGIParams' call, or can live in another Form.
#
my $params = $class->NewParameters();
print Common::WriteXML::GetXMLString($params) . "\n";

# Here is another way to directly assign values into the Parameter fields.
# FormObject provides an AUTOLOAD accessor (can't chain these, though, which is
# why I have to dereference OptionalData instead of writing $params->OptionalData()->IceCreamFlavor() ).
#
$params->{OptionalData}->IceCreamFlavor("Strawberry");
print Common::WriteXML::GetXMLString($params) . "\n";

# These accessors are the same that the assignCGIParams call eventually invokes.
# Being a form object parameters can be validated.
#
print( $params->validate() ? 'good' : 'bad', "\n" );    # should be 'good'

$params->{OptionalData}->IceCreamFlavor("Rocky Road");
print( $params->validate() ? 'good' : 'bad', "\n" );    # should be 'bad'

# Printing out the XML again you will notice an error message ('e' attribute).
#
print Common::WriteXML::GetXMLString($params) . "\n";

# This saves the param state to the database.
# Notice that I am not validating here, so you can write crap.
# Calling validate within save() might not be a bad plan...
#
$params->save(101);

# That's all the UI related interfaces.

# I want to test Report before I build the Queue method.
# But QueueReport is the only way to normally create a Report.
# For testing, though, I'll just create a bogus Report object and
# run with it.

my $newParams = $class->NewParameters();
$newParams->{OptionalData}->IceCreamFlavor("Strawberry");

# !!! did I mention that you will normally not do this ?
# !!! Use QueueReport() instead.
my $report = $class->QueueReport( parameters => $newParams );
print Common::WriteXML::GetXMLString($report) . "\n";

# Now, pretend we're the Job script and create the report.
#
my $newReport = RPS::Report::Dynamic::Factory->Fetch( $report->reportID() );
$newReport->createReport();

print "flavor?: " . $newReport->parameters()->{OptionalData}->IceCreamFlavor() . "\n";
