#!/usr/bin/env perl

use 5.012;
use strict;
use warnings;

BEGIN {
	# KATEB_DEV=1 carton run bin/kateb ...
	# KATEB_DEV=1 bin/kateb ... "$FindBin::Bin/../local/lib/perl5" is for this case
	if ($ENV{KATEB_DEV})
	{
		say 'DEV_MODE';
		use FindBin;
		unshift @INC, "$FindBin::Bin/../lib", "$FindBin::Bin/../local/lib/perl5";
	}

	if ($^O eq 'MSWin32')
	{
		say 'sorry, kateb is not currently usable in Windows';
		exit 1;
	}
}

use kateb;

my $version = $kateb::VERSION;

use Term::ANSIColor qw(:constants);

#------------------- ansi color constants -------------------#
my %c =
(
	byellow => (BOLD YELLOW),
	bpurple => (BOLD MAGENTA),
	bblue   => (BOLD BLUE),
	bgreen  => (BOLD GREEN),
	bold    => (BOLD),
	bred    => (BOLD RED),
	bcyan   => (BOLD CYAN),
	reset   => (RESET),
);

#------------------- prepare -------------------#
my $local_data = kateb::LocalData->new;

print "\n";


#----------------- Check ARGVs -----------------#
usage() unless @ARGV;

usage() if (@ARGV && $ARGV[0] !~ m/^install$|^reinstall$|^update$|^list$|^fonts$|^info$|^self-upgrade$|^version$|\-v/);

if ($ARGV[0] =~ m/^version$|^\-v$/)
{
	print "version: $c{bgreen}". $version ."$c{reset}\n";
	exit 0;
}

if ($ARGV[0] =~ m/^list$/)
{
	my $num;
	say "installed fonts:\n";
	foreach my $font_name ( sort {lc $a cmp lc $b} keys %{$local_data->{installedVersions}})
	{
		if ($local_data->{installedVersions}->{$font_name})
		{
			printf("%20s : %s\n", $font_name, $local_data->{installedVersions}->{$font_name});
			++$num;
		}
	}
	say "No fonts have been installed by kateb yet\n" unless $num;
	print "\n";
	exit 0;
}

if ($ARGV[0] =~ m/^fonts$/)
{
	require kateb::FontInfo;
	my $info = kateb::FontInfo->new;
	my @fonts_list = sort {lc $a cmp lc $b} keys %{$info};
	say "List of fonts supported by kateb\n";
	say "\t$_" foreach @fonts_list;
	print "\n";
}

if ($ARGV[0] =~ m/^info$/)
{
	shift @ARGV;

	my $install = kateb::Install->new(@ARGV);
	error_and_exit($install) if ($install->{error});

	require kateb::FontInfo;
	my $info = kateb::FontInfo->new;

	my @fonts_list;
	if ($ARGV[0] =~ /^all$|^-a$/)
	{
		@fonts_list = keys %{$info};
	} else
	{
		@fonts_list = @ARGV;
	}
	@fonts_list = sort {lc $a cmp lc $b} @fonts_list;
	foreach my $font_name (@fonts_list)
	{
		say $c{bgreen} . $info->{$font_name}->{name} . $c{reset};
		say "Publisher : " . $info->{$font_name}->{publisher_name};
		say "Repository: " . $info->{$font_name}->{repo};
		print "\n";
	}

	print "\n";
}

if ($ARGV[0] =~ m/^install$/)
{
	shift @ARGV;
	my $install = kateb::Install->new(@ARGV);
	error_and_exit($install) if ($install->{error});

	$install->install($local_data);

	exit 0;
}

if ($ARGV[0] =~ m/^update$/)
{
	shift @ARGV;
	my $install = kateb::Install->new(@ARGV);
	error_and_exit($install) if ($install->{error});

	$install->update($local_data);

	exit 0;
}

if ($ARGV[0] =~ m/^reinstall$/)
{
	shift @ARGV;
	my $install = kateb::Install->new(@ARGV);
	error_and_exit($install) if ($install->{error});

	$install->reinstall($local_data);

	exit 0;
}

if ($ARGV[0] =~ m/^self-upgrade$/) {
	use HTTP::Tinyish;
	my $http = HTTP::Tinyish->new
		(
			agent => "kateb/$kateb::VERSION",
			verify_SSL => 1
		);
	my $response = $http->get('https://fastapi.metacpan.org/v1/module/kateb?fields=version');
	unless ($response->{success})
	{
		say "Error!!! - failed to connect to metacpan.org, plz try again later";
		exit 3;
	}

	use JSON::PP;
	my $latest_version = decode_json( $response->{content} )->{version};
	unless (version->parse($latest_version)->numify > version->parse($version)->numify)
	{
		print "kateb is up to date: " .
			$c{bgreen} . version->parse($version)->normal . $c{reset} ."\n";
		exit 0;
	}

	say "installed version: $c{byellow}" . version->parse($version)->normal . $c{reset};
	say "available version: $c{bblue}" . version->parse($latest_version)->normal . $c{reset};
	say '';
	say "you can upgrade to new version by type this command:\n";
	say "	sudo cpan -T kateb\n	or\n	sudo cpan kateb";
	# }
	# if ($> == 0) {
	# 	use CPAN;
	# 	CPAN::Shell->install("kateb");
	## 	say qx{sudo cpan -T kateb};
	# } else {
	## 	say qx{cpan -T kateb};
	# }



	exit 0;
}


sub error_and_exit {
	my $install = shift;
	say $install->{message};
	say "- $_" foreach @{$install->{errlist}};
	print "\n";
	say "To see the list of fonts supported by kateb, try:\n\n\tkateb fonts\n";
	exit $install->{error};
}

sub usage {
	print <<END_USAGE;
kateb <command> [option]

commands:
    install          install new font
    update           update available font
    reinstall        reinstall, installed font
    list             list of installed fonts versions
    fonts            show all farsi free fonts supported
    info             short info about font publisher
    version | -v     kateb version
    self-upgrade     kateb, update itelf

options:
    -a | all         install or update all fonts

sample:
    kateb install all
END_USAGE
exit 1;
}
