package PS::Verbose;
use base qw( PS::Base );

require 5.006_000;

use strict;
use util;
use Data::Dumper;

our $VERSION 	= 1.10;

sub init {
  my ($self, $args) = @_;
  print "DEBUG >>	Initializing PS:Verbose ...\n" if $self->DEBUG;

  $args->{DATA} = {} unless defined $args->{DATA};
  $self->{data} = (ref $args->{DATA} eq 'HASH') ? $args->{DATA} : { $args->{DATA} };
  $self->{data}{elapsedtime} = compacttime(0);

  $self->verbose( (exists $args->{VERBOSE}) ? $args->{VERBOSE} : 1 );

  $self->{_lastoutput} = time();
  $self->{_starttime} = time();

  return $self;
}

# add a list of variables onto the main data hash
sub add {
  my $self = shift;
  if (ref $_[0] ne 'HASH') {
    while (@_) {
      my ($var, $val) = (shift, shift);
      $self->{data}{$var} = $val;
    }
  } else {
    while (my ($var,$val) = each %{$_[0]}) {
      $self->{data}{$var} = $val;
    }
  }
}

# deletes a list of variables from the main data hash
sub del {
  my $self = shift;
  delete $self->{data}{$_} foreach @_;
}

# prints a string to the screen, interpolating variable tokens within the string
# $data is an optional hash of extra values to use for interpolating
sub print {
  my ($self, $str, $data, $force) = @_;
  $str = "*** Undefined language string used in " . (caller)[1] . " at or near line # " . (caller)[2] . " ***\n" unless defined $str;
  return if (!$force && !$self->verbose) or !defined $str or $str eq '';
  $data = {} unless defined $data;
  $self->{data}{elapsedtime} = compacttime( time() - $self->{_starttime} );
  $str =~ s/\\n/\n/g;							# replace literal "\n" strings with phyiscal newlines
  $str =~ s/\\r/\r/g;							# replace literal "\r" strings with phyiscal CR's
  $str =~ s/\\t/\t/g;							# replace literal "\t" strings with phyiscal tabs
  $str = simple_interpolate($str, { %{$self->{data}}, %$data });	# interpolate simple variable $tokens

  print $str;
}

# toggle verbose mode on/off (1/0), or just return the current value if no paramaters are given
sub verbose {
  my $self = shift;
  return (@_) ? $self->{_verbose} = shift : $self->{_verbose};
}



1;

