###################################################################

package KoLBot::KMail;
 
# Copyright (c) 2004 Alan W. Mahoney. All rights reserved. 
# This program is free software.  You may modify and/or 
# distribute it under the same terms as Perl itself.  
# This copyright notice must remain attached to the file.  
#

use warnings;
use strict;

use KoLBot;
use KoLBot::Items;

use Carp;

BEGIN 
{
  use Exporter ();
  our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $ID);

  $VERSION     = 0.5;
  $ID          = '$Id: KMail.pm 169 2006-09-26 20:37:44Z alan $';

  @ISA         = qw(Exporter);
  @EXPORT      = qw( );
  %EXPORT_TAGS = ( );

  @EXPORT_OK   = qw();
}

our @EXPORT_OK;

##########################################
## Overloads
##########################################

use overload q("") => \&as_string;

##########################################
## element access
##########################################

for my $field (qw( type
		   rawmsg
		   time
		   user
		   user_id
		   message
		   id
		   meat
		   items

		   insidenote
		   i_meat
		   i_items

		   )) {
# 		   item_id
# 		   item_name
# 		   number
# 		   i_item_id
# 		   i_item_name
# 		   i_number
		   # last six above are to to phase out
		   # item_db is phased out
  no strict "refs";
  *$field = sub {
    my $self = shift;
    $self->{$field} = shift if @_;
    return $self->{$field};
  }
}

##########################################
## Constructor:
##########################################

sub new {
  my $invocant = shift;
  my $class = ref($invocant) || $invocant;
  my $kmail = { type => 'kmail',
		@_ };
  if(exists($kmail->{item_id})) {
    if(exists($kmail->{items})) {
      die "Can't pass both {items} (new) and item_id (old) to KMail::new";
    }
    my $onumber = $kmail->{number} || [];
    my $oitem = $kmail->{item_id} || [];
    if(ref($onumber) ne 'ARRAY') {
      $onumber = [$onumber];
    }
    if(ref($oitem) ne 'ARRAY') {
      $oitem = [$oitem];
    }
    my @items = ();
    for my $i (0..$#{$onumber}) {
      # bare minimum item
      push @items,new KoLBot::Item(id => $oitem->[$i],
				   number => $onumber->[$i],);
    }
    $kmail->{items} = [@items];
#    die "KMail::new now takes \@items rather than ids and numbers.";
  }

  bless $kmail, $class;
  delete($kmail->{number});
  delete($kmail->{item_id});
  $kmail->{items} ||= [],
  return $kmail;
}

##########################################
## The rest
##########################################

sub as_string {
  my $kmail = shift;
  my $str = '';

#  my ($sec,$min,$hour,$mday,$mon,$year) = KoLBot::kmailtime($kmail->time);
  my ($sec,$min,$hour,$mday,$mon,$year) = gmtime($kmail->time);
  my @months = ('Jan','Feb','Mar','Apr','May','Jun','Jul',
		'Aug','Sep','Oct','Nov','Dec');

  $str .= "From: " . $kmail->user . " (" . $kmail->user_id . ")\n";
  $str .= "Id: " . $kmail->id . "\n";
  $str .= "Date: " . " $mday " . $months[$mon] . " " . ($year+1900)
    . " $hour:" . (($min<10) ? '0': '') . "$min\n";
  $str .= "Meat: " . $kmail->meat . "\n";

  $str .= " Items:\n";

  for (@{$kmail->items}) {
    $str .= $_->{number} . "@" . $_->{id} . ' ' . ($_->{name} || '') . "\n";
  }
  $str .= "---------\n";

  $str .= $kmail->message;
  $str .= "\n";

  if(defined($kmail->{insidenote})) {
    $str .= "----inside package----\n";
    $str .= "Meat: " . $kmail->i_meat . "\n";
    for (@{$kmail->i_items}) {
      $str .= $_->{number} . "@" . $_->{id} . ' ' . ($_->{name} || ''). "\n";
    }
    $str .= "---------\n";
    $str .= $kmail->insidenote;
    $str .= "\n";
  }

  return $str;
}

END {} ;

1;
