package XXV::OUTPUT::Ajax;

use strict;

#use Template;
use vars qw($AUTOLOAD);
use Locale::gettext;
use Tools;
use XML::Simple;

$SIG{CHLD} = 'IGNORE';

# ------------------
sub AUTOLOAD {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $data = shift || {};
    my $params = shift || 0;

    my $name = (split('::', $AUTOLOAD))[-1];
    return  if($name eq 'DESTROY');

    $obj->{nopack} = 1;
    $obj->out( $data, $params, $name );

    $obj->{call} = '';
}

# ------------------
sub new {
# ------------------
	my($class, %attr) = @_;
	my $self = {};
	bless($self, $class);

    $self->{handle} = $attr{'-handle'}
        || return error('No handle defined!');

    $self->{cgi} = $attr{'-cgi'}
        || return error('No TemplateDir given!');

    $self->{browser} = $attr{'-browser'}
        || return error('No Mimehash given!');

    $self->{xml} = XML::Simple->new()
        || return error('XML failed!');

    $self->{outtype} = $attr{'-output'}
        || return error('No output type given!');

    $self->{TYP} = 'AJAX';

    $self->{CMDSTAT} = undef;

	return $self;
}

# ------------------
sub entities
# ------------------
{
    my $s = shift;

    $s =~ s/&/&amp;/g;
    $s =~ s/>/&gt;/g;
    $s =~ s/</&lt;/g;
    $s =~ s/\"/&quot;/g;
    $s =~ s/([^a-zA-Z0-9&%;:,\.\!\?\(\)\' ])/sprintf("&#x%02x;",ord($1))/eg;

    return $s;
}

# ------------------
sub out {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $data = shift || 'no Text for Output';
    my $para = shift || 0;
    my $name = shift || 'noName';
    my $type = shift || ($obj->{outtype} eq 'xml' ? 'application/xml' : 'text/plain');
    my %args = @_;

    unless(defined $obj->{header}) {
        # HTTP Header
        $obj->{output_header} = $obj->header($type, \%args);
    }
    $obj->{sendbytes}+= length($data);

    $obj->{output}->{DATA} = $data;
    $obj->{output}->{$name}->{data} = $data;
    $obj->{output}->{$name}->{params} = $para
        if($para);
}

# ------------------
sub printout {
# ------------------
    my $obj = shift  || return error ('No Object!' );
    my $nopack = shift || $obj->{nopack} || 0;

    $obj->{output}->{cmdstat} = $obj->{CMDSTAT};
    my $content = ($obj->{outtype} eq 'xml' ? $obj->{xml}->XMLout($obj->{output}) : $obj->{output}->{DATA});

    $content = Compress::Zlib::memGzip($content)
        if(! $nopack and $obj->{Zlib} and $obj->{browser}->{accept_gzip});

    $obj->{handle}->print($obj->{output_header}, $content);
    undef $obj->{output};
    undef $obj->{output_header};
    undef $obj->{nopack};
}


# ------------------
sub header {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $typ = shift || return error ('No Type!' );
    my $arg = shift || {};

    $arg->{'Content-encoding'} = 'gzip'
        if($obj->{browser}->{accept_gzip} && ((!defined $obj->{nopack}) || $obj->{nopack} == 0) );

    $obj->{header} = 1;
    return $obj->{cgi}->header(
        -type   =>  $typ,
        -status  => "200 OK",
        %{$arg},
    );
}

# ------------------
sub headerNoAuth {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $typ = shift || 'text/html';

    $obj->{header} = 1;
    return $obj->{cgi}->header(
        -type    => $typ,
        -status  => "401 Authorization Required\nWWW-Authenticate: Basic realm=\"xxvd\""
    );
}

# ------------------
sub typ {
# ------------------
    my $obj = shift || return error ('No Object!' );
    return $obj->{TYP};
}

# ------------------
sub setCall {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $name = shift || return error ('No Name!' );

    $obj->{call} = $name;
    return $obj->{call};
}

# ------------------
sub browser {
# ------------------
    my $obj = shift || return error ('No Object!' );
    return $obj->{browser};
}

1;
