#!/usr/bin/perl

use Mojo::SQLite;
use Data::Dumper;
use Mojo::JSON qw(decode_json encode_json);
require './subroutines.pl';
use File::Slurp;
my $msg = shift;
my $database = shift;
die unless -e $database;
my $config_file = read_file('./config.json');
my $config = decode_json $config_file;
our $logfile = &subs::home($config->{'logfile'});
our $log = Mojo::Log->new(path => $logfile);
my $suds = &subs::suds_grabber();
my $tally  = eval { return decode_json &subs::note_decrypter($suds,read_file(&subs::home('~/.president/pen'))) } || {};
my $sql = Mojo::SQLite->new('sqlite:' . $database, sqlite_use_immediate_transaction => 0);


if ( scalar @{$tally->{'__statements'}} == 0 ) {

	my $statements = &subs::db_query('select * from mailbox order by server_time desc limit 10')->hashes;


	foreach my $st ( @{$statements} ) {
		my $old_message = &subs::note_decrypter($gb::suds, $st->{'body'});
		$tally = &statement_preparer($old_message,$tally);
		push @{$tally->{'__statements'}}, $old_message;
	}
}

$tally = &statement_preparer($msg, $tally);

sub statement_preparer() {
	my $msg = shift;
	my $tally = shift;
	my @msg = split ' ', $msg;
	my @lexicon = @{$tally->{'__lexicon'}};
	for (my $w = 0; $w <= scalar @msg; $w++) {
		my $ms = $msg[$w];
		$tally = &word_reader($ms, $tally);

		for (my $n = 0; $n <= scalar @msg; $n++ ) {
			my $word;
			foreach my $wor ( $n .. scalar @msg ) {
				$word .= '_' . $msg[$wor];
				unless ( grep { $_ eq $word } @lexicon ) {
					push @lexicon, $word;
					&word_reader($word, $tally);
				}
			}
		}
	}
	@{$tally->{'__lexicon'}} = @lexicon;
	return $tally;
}

sub word_reader() {
	my $m = shift;
	my $tally = shift;
	$m =~ s/^_//gi;
	$m =~ s/_$//gi;
#	$log->info('Word: ' . $m);
	$m = &subs::unformat_name($m);
	my $ms = &subs::settings_grabber({ app => $m, benign => 1 });
	if (scalar keys %{$ms} > 0 ) { #$log->info('settings for ' . $m . ' ' . scalar keys %{$ms} ); 
}
	$tally->{$ms->{'app'}}->{'setting'} = $ms if scalar keys %{$ms} > 2;
	foreach my $mk ( keys %{$ms} ) {
		if ($mk =~ /^sc_/gi) {
			my $mak = $mk;
			$mak =~ s/^sc_//gi;
			$tally->{$ms->{'app'}}->{'sc'}->{$mak} = decode_json $ms->{$mk};
		}
	}
	foreach my $mo ( qw/model option option_category subcategory/ ) {
	#	my $mos = &subs::db_select($mo, undef, { name => $mo })->hashes;
	#	$tally->{$ms->{'app'}}->{$mo} = $mos if scalar @{$mos} > 0;
	}
	return $tally;
}

push @{$tally->{'__statements'}}, $data->{'msg'};
#$log->info(Dumper $tally->{'__lexicon'});

my $jtally = encode_json $tally;
$log->info($jtally);
write_file(&subs::home('~/.president/pen'),&subs::note_encrypter($suds,$jtally));


my $return_msg = join ' ', grep { $_ !~ /^__/gi } keys %{$tally};
my $msg_data = {
	uuid => &subs::random_string_creator(25),
	timestamp => &subs::rightNow(),
	server_time => &subs::rightNow(),
	body => $return_msg,
	manager_file => &subs::manager_file_maker('pen'),
	status => 'public',
	contact => 'pen'
};
&subs::db_insert('mailbox', $msg_data);
my $returner = encode_json $msg_data;
print $returner;




