#!/usr/bin/perl

use strict;
use warnings;

BEGIN {
	
exit if fork();
exit if fork();

close STDIN;
close STDOUT;
close STDERR;


  use Glib::Object::Introspection;
  Glib::Object::Introspection->setup(
    basename => 'Gio',
    version => '2.0',
    package => 'Glib::IO');
}

use Gtk3;
use Glib ('TRUE','FALSE');

use Crypter;
use Encode;
use utf8;

my $NOTE_FILE = $ENV{CRYPTED_NOTE_FILE} || ($ENV{HOME} . '/.crypted_note.txt');
my $OBFUSCATE_LEVEL = 100;

warn $NOTE_FILE;

my $textview2;
my $textview;

my $crypter;
my $password;

 

# The CLASS MyWindow, where we build the Gtk3::ApplicationWindow and its content
package MyWindow;
use strict;
use warnings;
use Gtk3;
use Glib qw/TRUE FALSE/;
# Our class must be a subclass of Gtk3::ApplicationWindow to inherit
# the methods, properties etc.pp. of Gtk3::ApplicationWindow
use base 'Gtk3::ApplicationWindow';
 
 sub new {
 	my ($window, $app) = @_;
 	$window = bless Gtk3::ApplicationWindow->new($app);


#my $window = Gtk3::Window->new('toplevel');
$window->set_title('Crypted Note');
$window->set_default_size(800, 600);


my $accel_group = Gtk3::AccelGroup->new;
$window->add_accel_group($accel_group);

$window->signal_connect('delete_event' => sub {
    Gtk3->main_quit 
});	


# a scrollbar for the child widget (that is going to be the textview!)
my $scrolled_window = Gtk3::ScrolledWindow->new();
$scrolled_window->set_border_width(5);
# we scroll only if needed
$scrolled_window->set_policy('automatic','automatic');

# a text buffer (stores text)
my $buffer1 = Gtk3::TextBuffer->new();

# a textview
$textview = Gtk3::TextView->new();
# displays the buffer
$textview->set_buffer($buffer1);
# wrap the text, if needed, breaking lines in between words
$textview->set_wrap_mode('word');

# textview is scrolled
$scrolled_window->add($textview);
$scrolled_window->show();
$window->add($scrolled_window);






	
# a textview
$textview2 = Gtk3::Entry->new();
$textview2->set_visibility(FALSE);

#$window->add($button);
input_password_dialog();

# show the window and run the Application
$window -> show_all();
#Gtk3->main();


	return $window;
}

sub input_password_dialog {
	# create a Dialog
	my $dialog = Gtk3::Dialog->new();
	$dialog->set_title('Enter password!');
	$dialog->set_default_size(400, 40);



	# add a button to the dialog window
	$dialog->add_button('OK','ok');

	# connect the 'response' signal (the button has been clicked) to the
	# function on_response()
	$dialog->signal_connect('response' => \&on_set_password);

	# get the content area of the dialog, add a label to it
	my $content_area = $dialog->get_content_area();


	# displays the buffer
	#$textview2->set_buffer($buffer2);
	# wrap the text, if needed, breaking lines in between words
	#$textview2->set_wrap_mode('word');
	
	# textview is scrolled
	$content_area->add($textview2);


	# show the dialog
	$dialog->show_all(); 
}





sub on_set_password {
	my ($widget, $response_id) = @_;
	
	#my $b = $textview2->get_buffer;
	$password = $textview2->get_text();

	$password = Encode::encode_utf8($password);
	utf8::upgrade($password);
	
	warn $password;
	
	
	my $fh;
	open $fh, $NOTE_FILE;
	binmode $fh;
	my $crypted = join '', <$fh>;
	close $fh;
	
	my $out = '';
	
	my $crypter = new Crypter(password => $password, obfuscate => $OBFUSCATE_LEVEL);
	my $out = $crypter->uncrypt($crypted);
	
	$out = Encode::decode_utf8($out);
	#utf8::downgrade($out);

	
	$textview->get_buffer->set_text($out);
	
	$widget->destroy(); 
}
	



# The MAIN FUNCTION should be as small as possible and do almost nothing except creating 
# your Gtk3::Application and running it
# The "real work" should always be done in response to the signals fired by Gtk3::Application.
# see below
package main;
use strict;
use warnings;
 
use Gtk3;
use Glib ('TRUE', 'FALSE');

my $app = Gtk3::Application->new('app.id', 'flags-none');
 
$app->signal_connect('startup'  => \&_init     );
$app->signal_connect('activate' => \&_build_ui );
 
$app->run(\@ARGV);
 
exit;





# The CALLBACK FUNCTIONS to the SIGNALS fired by the main function.
# Here we do the "real work" (see above)
sub _init {
	my ($app) = @_;
	
	# create a menu
	my $menu = Glib::IO::Menu->new();
	
	# append to the menu three options
	$menu->append('Save', 'app.save');
	$menu->append('Quit','app.quit');
	# set the menu as menu of the application
	$app->set_app_menu($menu);
	
	# create an actionfor the option "new" of the menu
	my $save_action = Glib::IO::SimpleAction->new('save',undef);
	# connect it to the callback  function new_cb
	$save_action->signal_connect('activate'=>\&save_cb);
	# add the action to the application
	$app->add_action($save_action);
		
	# option "quit"
	my $quit_action = Glib::IO::SimpleAction->new('quit',undef);
	$quit_action->signal_connect('activate'=>\&quit_cb);
	$app->add_action($quit_action);
}

sub _build_ui {
	my ($app) = @_;
	my $window = MyWindow->new($app);
	$window->show();
}



# callback function for "new"
sub save_cb {
	
		
	my $b = $textview->get_buffer;
	my $text = $b->get_text($b->get_start_iter, $b->get_end_iter, TRUE);
	$text = encode_utf8($text);
	utf8::upgrade($text);
	
	my $out = '';
	
	my $crypter = new Crypter(password => $password, obfuscate => $OBFUSCATE_LEVEL);
	$out = $crypter->crypt($text);
		
	my $fh;
	open $fh, '>', $NOTE_FILE;
	binmode $fh;
	print $fh $out;
	close $fh;


	
}


# callback function for "quit"
sub quit_cb {
	$app->quit();
}
