#!/usr/local/bin/perl5.6.1 -w
# $Id: LabelerDataM.pm,v 1.1 2003/10/21 15:42:03 jx1 Exp $

package LabelerDataM;

use strict;
use warnings;
use Carp qw(croak cluck carp);
use Class::Struct;

use sql_engine;
use Checkbarcode;
#use BarcodeRevised_V2_0;
require "/nfs/team71/psg/dn1/badger-bin/BarcodeRevised_V2_0.pm";

use constant DBCONNECT => '2dbarcode';
use constant SQLLIB => 'DnaBoxLabel.sql_lib';
use constant LABEL_PREFIX => 'DB';

####    simple object tester    ####
unless( caller )
{
    use LabelerDataM;

    print "check constant: ", DBCONNECT, "\n";

    my $datamgr = LabelerDataM->new()->init();
    print "Data manager ref: $datamgr\n";
    print "sqllib: ", SQLLIB, "::all\n";

    #$datamgr->printBarcode();
    $datamgr->findDefaultBcPrinter();
    undef $datamgr;
    
}


####    Object definition    ####
struct LabelerDataM =>
{
    QueryHandlr => '$',
    BarcodeLs   => '$', # ref to list
    VLeader     => '$',
    UserInfo    => '$',
};


sub init
{
    my $self = shift;

    # init user information
    my $vleader = $self->VLeader();
    if( defined $vleader )
    {
	$self -> UserInfo( $vleader->DataSource()->User() ); 
    }   
#    if( $^W )
#    {
#	my $user = $self -> UserInfo();
#	
#	if( defined $user )
#	{
#	    print "User information: \n";
#	    print "UserId: ", $user->PersonId(), "\n";
#	    print "Team:   ", $user->Team(), "\n";
#	    print "RoleId: ", $user->RoleId(), "\n";
#	}
#    }

    # database connection
    my $dbqh = statements->new(DBCONNECT);
    $dbqh->{debug} = 1 if( $^W );
#    $dbqh->default_sql_lib_path('./');
    $dbqh->load_sql_lib([SQLLIB.'::all']);

    $self-> QueryHandlr( $dbqh );

    # setup barcode label handler
    $self->BarcodeLs( Barcode->new( $dbqh->{DBH} ) );

    my $labelhdlr = $self->BarcodeLs();
    $labelhdlr->Type( LABEL_PREFIX );
    $labelhdlr->Printer( $self->findDefaultBcPrinter() );

    return $self;
}


sub DESTROY
{
    my $self = shift;
    
    my $qryh = $self->QueryHandlr();
    if( defined $qryh )
    {
	$qryh->{DBH}->rollback();
	$qryh -> end();
    }
    
    return;
}


sub findDefaultBcPrinter
{
    my $self = shift;
    my $dbqh = $self->QueryHandlr();
    my $user = $self->UserInfo();

    my $default_printer;

    my $qrslt = $dbqh->get( SQLLIB.'::default_barcode_printer', [LABEL_PREFIX, $user->Email()] );

    if( scalar $qrslt )
    {
	$default_printer = $qrslt->[0]->[0];
    }
    else
    {
	$default_printer = '';
    }
    
    print "Default printer found for ", $user->Email(), " is -$default_printer-\n";
    return $default_printer;
}


sub selectBcPrinter
{
    my $self = shift;
    my $vleader = $self->VLeader();

    my $labelhdlr = $self->BarcodeLs();
    if( defined $labelhdlr )
    {
	# reset barcode printer
	my $printer = $labelhdlr->Printer();
	print "resetting printer ...$printer\n";

	#my $printer = 'none';
	#$labelhdlr->Printer('none');
	#print "barcode printer now is ", $labelhdlr->Printer(), "\n";

	$labelhdlr->Viewprinters();
	
#	my $i = 1;
#	if( $labelhdlr->Printer() eq $printer )
#	{
#	    print "start pause $i\n";
#	    $i++;
#	    sleep( 3 );
#	}
    } #end - if( defined $labelhdlr )

    return $labelhdlr->Printer();
}


sub getCurrentBcPrinter
{
    my $self = shift;

    return $self->BarcodeLs()->Printer();
}


sub printBarcodeLs
{
    my $self = shift;
    my $vleader = $self->VLeader();
    #print "In 'printBarcode' \n";

    my $labelhdlr = $self->BarcodeLs();
    my $labels = $labelhdlr->{Label};
    my $num_lbs = scalar @{$labels};
    print "Number of labels in this batch: $num_lbs\n";
#    print "Chosen printer: ", $labelhdlr->Printer(), "\n";
#    return;

    if( $num_lbs )
    {
	my $printer = $labelhdlr->Printer();
	if( $printer ne '' )
	{
	    my $pstatus = `lpq -P$printer`;
	    print "$pstatus\n";

	    if( $pstatus !~ /Printer OK/ )
	    {
		if( $pstatus =~ /cannot open connection/ )
		{
		    $vleader->updateWarning( "Printer $printer may not be switched on." );
		}
		elsif( $pstatus =~ /no.+printer/ )
		{
		    croak "Printer $printer may not exist.\n";
		}
	    }
	    else
	    {
		print "Chosen printer: ", $labelhdlr->Printer(), "\n";
		#return;
		eval
		{
		    $labelhdlr->Printlabel();
		};
		if( $@ )
		{
		    $vleader->updateWarning( "Problems printing labels!", $@ );
		}
		else
		{
		    $vleader->updateMessage( "$num_lbs Labels printed." );
		    
		    # clear all labels
		    delete @{ $labelhdlr->{Label} }[0, $num_lbs-1];
		    print "after cleaning: ", scalar @{ $labelhdlr->{Label} };
		}
	    }
	}
	else
	{
	    croak "No printer has been set.\n";
	    #$vleader->updateWarning( "No printer has been set." );
	}
    }
    else
    {
	# there must be nothing to print
	$vleader->updateWarning( "Nothing to print." );
    }

    return;
}


sub createBarcodeLs
{
    my $self = shift;
    my $num = shift;
    my $user = $self->UserInfo();
    my $dbqh = $self->QueryHandlr();

    my $labelhdlr = $self->BarcodeLs();
    my $labels = [];
    #my $labels = $labelhdlr->{Label};
    my $boxid;
    my $bcbits;

	
    for( my $i=0; $i<$num; $i++ )
    {
	$boxid = ( $dbqh->get( SQLLIB.'::new_dnabox_id' ) )->[0]->[0];
	$bcbits = makeID( LABEL_PREFIX.$boxid );
	
	push @{$labels}, $bcbits;
	#print "itr $i, number of labels: ", scalar @{$labels}, "\n";
	
	# setup labels for printing
	$labelhdlr->Barcode( $boxid );
	#$labelhdlr->Name(  );
	$labelhdlr->Owner( $user->Email() );
	$labelhdlr->Project( 'DNA Storage Box' );
	$labelhdlr->Labellist();
    }

    return $labels;
}

1; # keep perl happy
