#!/usr/bin/perl

use strict;
use warnings;
use lib "/home/dabreegster/lab/hexedui";
use include;

use HexedUI;
use Queue;
use Storable;
use Date::Format;

my $table = forge_template(
  ["<date>", "<type>", "<descr>", "<change>", '$<total_save>', '$<total_check>'],
  date => {
		Format =>"mm/dd/yy"
	},
  type         => {
		Format => [qw(Deposit_Savings Deposit_Checking Transfer Withdrawal)]
	},
  descr => {
		Format => "-"
	},
  change => {
		Format => "+1024.00",
		Filter => sub {
			my $obj = shift;
      sprintf('%1$+.2f', $obj->{change});
		}
	},
  total_save => {
		Format => '1024.00',
		Filter => sub {
			my $obj = shift;
		  sprintf('%1$.2f', $obj->{total_save});
		}
	},
  total_check => {
		Format => '1024.00',
		Filter => sub {
			my $obj = shift;
		  sprintf('%1$.2f', $obj->{total_check});
		}
	}
);

# Set up the queue.
my $book;
if (-f ".book") {
  $book = retrieve(".book");
  $book->set($_, MenuFilter => $table) foreach 0 .. $#{ $book->{Queue} };
} else {
  $book = new Queue;
  $book->{Checking} = 0;
  $book->{Savings} = 0;
}

# Set up the interface.
my $ui = cast HexedUI Ramen => {
  Title => {
    Type   => "Fixed",
    At     => [0, 0],
    Size   => [3, "100%"],
    Border => 1
  },
  Main => {
    Type   => "Menu",
    At	   => [3, 0],
    Size   => ["100% - 3", "100%"],
    Border => 1,
    Pad    => 1,
    Queue  => $book,
    KeyHandler => sub {
      my (undef, $key) = @_;
      add_entry() if $key eq "n";
    },
    ShiftKeys => 0,
    DeleteKey => 0
  },
  Shutdown => sub {
    delete @{$book}{qw(Heap Hooks)};
    store($book, ".book");
  }
};
calc_title();
$ui->{Ramen}{Main}->draw;

$ui->start;
exit;

sub add_entry {
  my $type = $ui->choose("What type of transaction?" =>
    "Deposit to Savings",
    "Deposit to Checking",
    "Transfer from Savings to Checking",
    "Withdrawal from Checking"
  );
  $type = [
    "Deposit_Savings",
    "Deposit_Checking",
    "Transfer",
    "Withdrawal"
  ]->[$type];
  my $desc = $ui->askfor("Please describe the transaction.", limit => 50);
  my $qty = $ui->askfor("Please enter the amount.", limit => 6);
  my $date = $ui->askfor("Please enter the date (blank for today).", limit => 8);
  $date ||= time2str("%D", time);

  # Processing
  if ($type eq "Deposit_Savings") {
    $book->{Savings} += $qty;
  } elsif ($type eq "Deposit_Checking") {
    $book->{Checking} += $qty;
  } elsif ($type eq "Transfer") {
    $book->{Savings} -= $qty;
    $book->{Checking} += $qty;
  } elsif ($type eq "Withdrawal") {
    $book->{Checking} -= $qty;
    $qty *= -1;
  }
  $book->preadd("transaction_" . $book->all,
    MenuFilter  => $table,
    date        => $date,
    type        => $type,
    descr       => $desc,
		change      => $qty,
		total_check => $book->{Checking},
		total_save  => $book->{Savings}
  );
  calc_title();
}

sub calc_title {
  my $title = sprintf('Dinero... Checking: $%1$.2f  Savings: $%2$.2f',
    $book->{Checking}, $book->{Savings});
  $ui->{Ramen}{Title}->title(0, $title);
  $ui->{Ramen}{Title}->draw;
}
