#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);


########### config start ################
my $file_path = "/home/superkuh/app_installs/idlerpg/questinfo.txt"; # in the idlerpg dir
my $basemapfilepath = '/home/superkuh/www/newmap.png'; # base "empty" map file
my $questmapfilepath = '/home/superkuh/www/questmap.png'; # end result map with nicks from this script

my $debugon = 0;
########### config end ################

my $questtext;
my $stage;
my $goal1x;
my $goal1y;
my $goal2x;
my $goal2y;
my $player1nick;
my $player1x;
my $player1y;
my $player2nick;
my $player2x;
my $player2y;
my $player3nick;
my $player3x;
my $player3y;
my $player4nick;
my $player4x;
my $player4y;
my $questype = 2; # only 2 has x,y coords for animating
my $type1quest_s_time;
# Why not %players? Because lazy.

#T setup a trade route through the mountains to the neighboring land of Qwok and arrange correspondence with their leader, Cuincey-Love Vikk'l
#Y 2
#S 2
#P 235 125 430 60
#P1 Kurenai 372 60
#P2 Superkuh 385 60
#P3 123DMWM 387 60
#P4 MrSlimeDiamond 360 60

#T locate the centuries-lost tomes of the grim prophet Haplashak Mhadhu
#Y 1
#S 1717848651
#P1 Evil_H4x
#P2 Gauldoth
#P3 scumola
#P4 mernisse

open my $fh, '<', $file_path or die "Cannot open $file_path: $!";
while (my $line = <$fh>) {
		if ($line =~ /T\s+(.+)/) {
			$questtext = $1;
		} elsif ($line =~ /^Y (\d)/) {
			# don't care
			# okay, looks like I actually have to care
			$questype = $1;
			print "quest type raw value: $questype\n" if $debugon;
		} elsif ($line =~ /^S (\d+)/) {
			my $sthing = $1;
			print "S raw value: $sthing\n" if $debugon;
			if ($questype == 2) {
				$stage = $sthing;
			} elsif ($questype == 1) {
				#$type1quest_s_time = $sthing;
				$type1quest_s_time = strftime("%Y-%m-%d %H:%M:%S", localtime($sthing));
				#$type1quest_s_time = duration($sthing);
			}	
		} elsif ($line =~ /^P (\d+) (\d+) (\d+) (\d+)/) {
        	$goal1x = $1;
        	$goal1y = $2;
        	$goal2x = $3;
        	$goal2y = $4;
		} elsif ($line =~ /P1 (\w+) (\d+) (\d+)/) {
        	$player1nick = $1;
        	$player1x = $2;
        	$player1y = $3;
		} elsif ($line =~ /P2 (\w+) (\d+) (\d+)/) {
        	$player2nick = $1;
        	$player2x = $2;
        	$player2y = $3;
		} elsif ($line =~ /P3 (\w+) (\d+) (\d+)/) {
        	$player3nick = $1;
        	$player3x = $2;
        	$player3y = $3;
		} elsif ($line =~ /P4 (\w+) (\d+) (\d+)/) {
        	$player4nick = $1;
        	$player4x = $2;
        	$player4y = $3;
		} elsif ($line =~ /P1 (\w+)/) {
        	$player1nick = $1;
		} elsif ($line =~ /P2 (\w+)/) {
        	$player2nick = $1;
		} elsif ($line =~ /P3 (\w+)/) {
        	$player3nick = $1;
		} elsif ($line =~ /P4 (\w+)/) {
        	$player4nick = $1;
		}   	
		       
}      
close $fh;   

print "quest: $questtext\n" if $debugon;
print "stage: $stage\n" if $debugon;
print "quest type: $questype\n" if $debugon;
print "goal1: $goal1x,$goal1y\n" if $debugon;
print "goal1: $goal2x,$goal2y\n" if $debugon;
print "P1: $player1nick ($player1x,$player1y)\n" if $debugon;    
print "P2: $player2nick ($player2x,$player2y)\n" if $debugon;    
print "P3: $player3nick ($player3x,$player3y)\n" if $debugon;    
print "P4: $player4nick ($player4x,$player4y)\n" if $debugon;  
        
my $questers = "Questers: $player1nick, $player2nick, $player3nick, $player4nick";        
    
if (!defined $questtext) {
	print "No current quests. Exiting...\n"  if $debugon;
	# do something to append the text "QUEST OVER" like,
	`convert $questmapfilepath -gravity NorthWest -pointsize 50 -fill red -stroke black -draw "text 100,200 'QUEST OVER'" $questmapfilepath`; 	
	exit;
} else {
	$questtext =~ s/'/\\\'/g; # escape apostrophes for imagemagick
	$questtext =~ s/\b(.{1,46}\s)\b(?=\w.{9})/$1\n/g; # line breaks, but not on words and only long ones simplified
	$questtext =~ s/\n(?!.*\n)//g;
	$questtext = ucfirst($questtext);

	if ($questype == 2) { # quests with player coordinates
		# good default with Questers text in black at bottom with nicks
		`convert $basemapfilepath -stroke black -pointsize 30 -gravity NorthWest -draw "text $goal1x,$goal1y 'Quest 1'" -draw "text $goal2x,$goal2y 'Quest 2'" -pointsize 18 -fill red -stroke red -draw "text $player1x,$player1y '$player1nick'" -fill blue -stroke blue -draw "text $player2x,$player2y '$player2nick'" -fill green -stroke green -draw "text $player3x,$player3y '$player3nick'" -fill purple -stroke purple -draw "text $player4x,$player4y '$player4nick'" -stroke black -pointsize 10 -draw "text 10,50 '$questtext'" -stroke black -pointsize 10 -draw "text 5,480 '$questers'" $questmapfilepath`;	
	} elsif ($questype == 1) { # quests without player coordinates
		print "qt: $questtext\n" if $debugon;
		`convert $basemapfilepath -gravity NorthWest -stroke black -pointsize 20 -draw "text 20,250 '$questtext'" -stroke black -pointsize 15 -draw "text 5,470 '$questers'" -pointsize 30 -fill red -stroke black -draw "text 20,200 'Quest Ends: $type1quest_s_time'" $questmapfilepath`;
		exit; # don't make movies of this type.	
	}
}    
