#
# Copyright (c) 2004-2007 - Consultas, PKG.fr
#
# This file is part of A2P.
#
# A2P is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# A2P is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with A2P; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# $Id: PTX.pm 3 2007-10-18 16:20:19Z g $
#
# Class to export Presentation Text Data (PTCA flux) members
#
# cf ref #2, p. 309
#

package AFPDS::MODCA::PTX ;

use strict ;
use A2P::Globals ;
use A2P::Syslog ;
use AFPDS::PTCA ;
use AFPDS::MODCA::Common ;

BEGIN {
    our $VERSION = sprintf "%s", q$Rev: 1007 $ =~ /([0-9.]+)\s+/ ;
}
our $VERSION ;
our @ISA = ("AFPDS::MODCA");
our $IDENTS ;

sub _ID { 0xD3EE9B }

sub new {
    my $class = shift ;
    &Debug("new " . __PACKAGE__ . " v$VERSION object");

    my $self = {
        FLUX     => 0,        # Associated flux
        IDENT    => _ID,      # MO:DCA identity
        FLAG     => 0,        # MO:DCA flags
        RESERVED => 0,        # MO:DCA reserved
        LONG     => 0,        # MO:DCA buffer length
        BUFFER   => ''        # MO:DCA content buffer
    };

    return bless $self , $class ;
}

sub validate {
    my $self = shift ;

    &UPSTAT('GETPTCA');

    my $ptx = new AFPDS::PTCA( $self->{BUFFER} );

    return ( 251, "Can't convert invalid PTCA flux" )
        unless ( $ptx->isValid );

    # Check FLUX is a Flux object
    return ( 251, "IPS needs to be used with Flux object" )
        unless ( ref($self->{FLUX}) =~ /^AFPDS::Flux$/ );

    # Store PTX only if used
    $self->{PTX} = $ptx->isValid > 0 ? $ptx : 0 ;

    return () ;
}

sub create {
    my $self = shift ;

    # Still return if not used
    my $ptx  = $self->{PTX} or return () ;

    my $flux = $self->{FLUX} ;

    if ( $flux->getlineindex > 0 ) {
        # Directly insert if any line exists
        $flux->addline( $ptx->getTex() ) ;

    } else {
        # Otherwise this means page is not initialized, so we can add
        # it to logo def. This case arises when adding line with logos
        $flux->newlogo( $ptx->getTex() ) ;
    }

    if ( my $font = $ptx->getFont() ){
        # PTCA has selected a font, we should handle it
        $flux->updateFlux( { DefaultTRC => $font } );
    }

    return () ;
}

&Debug("Module " . __PACKAGE__ . " v$VERSION loaded");

( $IDENTS->{&_ID} ) = __PACKAGE__ =~ /(\w+)$/ ;

1 ;
