use 5.34.0;
use Feature::Compat::Class;

#==============================================================================
class GeoStream;
#==============================================================================

use JSON::PP;
use IO::Handle;
use URI::Escape;
use DBIx::Simple;
use Data::Printer;
use LWP::UserAgent;
use Math::Trig qw/deg2rad/;
use DateTime::Format::Strptime;
use Term::ANSIColor qw/RESET :constants :constants256 color/;

field $sim_start_time :reader; # writer = set_sim_start_time(), getter =  sim_start_time()
field $deduct_time    :reader :param = 0; # time in seconds to deduct from log timestamps
field $simulation     :reader :param = 0; # if set, will simulate real-time delays based on log timestamps
field $test_output    :reader :param = 0; # if set will output all lines of log.txt
field $sql_trace      :reader :param = 0; # if set will output queries

field $dbix = DBIx::Simple->connect( 'dbi:SQLite:dbname=locations.db' );
field $t0; # only need this if simulating real-time delays

ADJUST { # say $sql_trace;
	$dbix->dbh->trace('SQL|'.$sql_trace);
	# say "simulation: $simulation, deduct_time: $deduct_time";
    $t0 = time - $deduct_time; # say "Start time: $start";
}
my $json = JSON::PP->new->utf8;
my $ua   = LWP::UserAgent->new( timeout => 10 );

my $osm_url = 'https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=%s&lon=%s';
my @offsets = ([0.5,0.5],[0.25,0.25],[0.75,0.25],[0.25,0.75],[0.75,0.75]); # offsets to try if no data found

my %locations_cache; # cache of lat/lon to location strings

method decode_line ($line) { say $line if $test_output;
	# must have simulation start time set before 1st I/SCN line:
	$sim_start_time || $self->set_sim_start_time($line);

    if ( my $data = $self->parse_log_line($line) ) { # aref [ $timestamp, $ms, $dir_name, $lat, $lon, $trisn ]
		# p $data;
		my ($timestamp, $ms, $dir_name, $lat, $lon, $tris) = @$data; # p $data;

		my ($h,$m,$s) = split /:/, $timestamp;
		my $secs = $h * 3600 + $m * 60 + $s; # say "$timestamp: $secs";

        if ($simulation) { # only need this if simulating real-time delays
           # determine how long to sleep, to match log file timestamps
            my $target_time = $t0 + $secs; # warn $target;
            while (time < $target_time) {
                my $remaining = $target_time - time;
                say STDERR FAINT YELLOW, "  waiting $remaining seconds ...",
                    RESET;
                sleep 1;
            }
        }
		{
			my $load_time = $ms > 1_100_000
				? sprintf '%.1f sec', $ms / 1_000_000
				: sprintf '%s ms', int $ms / 1_000;
			my $str = sprintf "%s [%s, %d triangles]",
				$dir_name, $load_time, $tris;
			say STDERR MAGENTA $str, RESET;
		}

		# formatted lat-lon string (e.g. N70 E18):
		my $lat_lon_str = $self->format_lat_lon($lat, $lon); # say "Lat/Lon: $lat_lon_str";
		# calculate adjusted time string (start time + seconds from log line)
		my $hms = $sim_start_time->clone->add(seconds => $secs)
			->strftime('%H:%M:%S'); # warn "time: $hms"; 

		my %res = ( message => sprintf '%s (+%s)', $hms, $timestamp );

		# unique id for this lat/lon (no spaces)
		my $id = $lat_lon_str =~ s/\s+//r; # say $id; # e.g. N70E18
		# need to cache so not duplicating openstreetmap lookups
		if ( $locations_cache{$id} ) { # p $locations_cache{$id}; # next;
			$res{message} .= ' loading from cache ';
			$res{colour}  = 'ITALIC FAINT YELLOW';
			# say STDERR ITALIC FAINT YELLOW 
			#	"[$timestamp] loading from cache for $lat / $lon", RESET;
		}
		elsif ( my $location = $self->db_lookup($lat, $lon) ) {
			$locations_cache{$id}{data} = $location;
			$res{message} .= ' loading from database ';
			$res{colour}  = 'ITALIC GREEN';
			# say STDERR ITALIC GREEN
			#      "[$timestamp] loading from database for $lat / $lon", RESET;
		}
		else {
			$res{message} .= ' fetching new location ';
			$res{colour}  = 'ITALIC BLUE';
			# say STDERR ITALIC FAINT BLUE
			#      "[$timestamp] fetching new location for $lat / $lon", RESET;
			OFFSET: # try up to 5 times to get data, offset by 0.25-degree increments
			for my $o (@offsets) {
				my $offset_lat = $lat + $o->[0];
    			my $offset_lon = $lon + $o->[1];

				my $url = sprintf( $osm_url, $offset_lat, $offset_lon );
				my $res = $ua->get($url, 'User-Agent' => 'GeoStream/1.0');
				if ($res->is_success) {
					my $data = eval { $json->decode($res->content) }; # p $data; next;
					# warn "no data for $offset_lat / $offset_lon, trying next" and
					next OFFSET if $data->{error};

					# say $dir_name;
					if ( my $addr = $data->{address} ) {
						my $location = $self->format_address($addr);
                        $locations_cache{$id}{data} = $location;
						last OFFSET;
					} 
				}
				else {
					warn "HTTP error: " . $res->status_line . "\n";
				}
			}
			$locations_cache{$id}{data} ||= 'No data'; # couldn't find anything in osm resoiurce
		}
        my $dimensions = $self->tile_dimensions($lat);
        $res{message} .= sprintf '%s :: Tile area: %s [H-%s x W-%s] km^2 ',
			$lat_lon_str, map int $_, @{$dimensions}{qw/area height width/};
		say color($res{colour}), $res{message}, RESET, 
			' :: ',  CYAN, $locations_cache{$id}{data}, RESET;
		# log message if it contains 'unknown' or 'no data':
		if ( $locations_cache{$id}{data} =~ /no data/i ) {
			open my $log_fh, '>>', 'geo_stream_warnings.log';
			say $log_fh join(' :: ', $lat, $lon, $locations_cache{$id}{data} );
			close $log_fh;
		}
    }
}

method format_lat_lon ($lat, $lon) {
	my $lat_dir = $lat >= 0 ? 'N' : 'S';
	my $lon_dir = $lon >= 0 ? 'E' : 'W';
	my $formatted = sprintf "%s%s %s%s", 
		abs($lat), $lat_dir, abs($lon), $lon_dir;
	return $formatted;  # N70 E18
}

# not currently used
method format_time ($t) {
	my ($h, $m, $s) = split /:/, $t;

	my @parts;
	push @parts, "$h hour"  . ($h == 1 ? '' : 's') if $h;
	push @parts, "$m min"   . ($m == 1 ? '' : 's') if $m;
	push @parts, "$s sec"   . ($s == 1 ? '' : 's') if $s;
	return join ' ', @parts;
}

# used by geo_stream_live.pl to get simulation start time
method get_sim_start_time { $sim_start_time }

method set_sim_start_time ($line) {
 	if ( my ($timestr) = $line =~ m/X-Plane Started on (.*)/i ) {
		my $strp = DateTime::Format::Strptime->new(
			pattern   => '%a %b %d %H:%M:%S %Y',
			time_zone => 'local',
		);
		$sim_start_time = $strp->parse_datetime($timestr);
		say 'Simulation startup time: ' . $sim_start_time->strftime('%H:%M:%S');
	}
}

method format_address ($addr) {
	# Step 1: Pick first meaningful locality (city, town, village, municipality)
	my @first_candidates = qw(city town village municipality city_district);
=begin
	my ($first) = grep { defined $_ && $_ ne '' && $_ !~ /^unknown$/i }
        map { $addr->{$_} } @first_candidates;
=cut
	my $first;
	for (@first_candidates) {
		$first = $self->clean_address($addr->{$_});
		last if $first;
	}
	# Step 2: Pick the most useful middle field
	my @middle_candidates = qw(state county region state_district province);
	my $middle;
	for (@middle_candidates) {
		$middle = $self->clean_address($addr->{$_});
		last if $middle && $middle ne $first; # avoid duplicate
	}

	# Step 3: Always pick country
	my $country = $self->clean_address($addr->{country});
=begin
	my ($city) = grep { defined $_ } # get first one of:
        @$addr{ qw/city city_district town village/ };
		# || next OFFSET;
	my $region  = $addr->{county} || $addr->{state};
	my $country = $addr->{country};
	$locations_cache{$id}{data} = join ', ', map { $_ || 'Unknown' }
		$city, $region, $country;
=cut
	# Build final parts
	my @parts = grep { defined $_ && $_ ne '' } ($first, $middle, $country);

	# Join non-empty parts with comma
	my $formatted = join ', ', @parts;
	return $formatted;
}

method clean_address ($place) {
	defined $place && $place !~ /^unknown$/i ? $place : ();
}

method parse_log_line {
	#0:57:32.110 I/SCN: DSF load time: 5710065 for file D:\XP Map Enhacement\Base packages\XPME_Europe/Earth nav data/+50+000/+53+007.dsf (608040 tris, 134 skipped for -23.8 m^2)
	my $line = shift;

	if ( $line =~ m{
        (\d+:\d+:\d+)        # [1] timestamp (e.g. 0:57:32)
        \.\d+                # fractional seconds (ignored)
        \s+I\/SCN:\s+        # literal " I/SCN: " with spacing
        DSF\ load\ time:\s+  # literal "DSF load time:"
        (\d+).*              # [2] load time in microseconds
        [/\\]([^/\\]+)       # [3] dir_name (after last / or \)
        /Earth\ nav\ data/   # folder
        [+-]\d+[+-]\d+/      # folder with lat/lon
        ([+-]\d+)            # [4] latitude
        ([+-]\d+)            # [5] longitude
        \.dsf                # file extension
        \s+\(                # opening parenthesis
        (\d+)                # [6] triangle count
        \s+tris,\s+\d+\s+skipped\s+for\s+
        (-?[\d\.]+)\s+m\^2\) # [7] area value of skipped triangles (can be negative)        
    }x ) {
		my ($time, $ms, $dir_name, $lat, $lon, $tris) = ($1, $2, $3, $4, $5, $6);
		# say join ' :: ', $time, $ms, $dir_name, $lat, $lon, $tris;
		return [ $time, $ms, $dir_name, $lat, $lon, $tris ];
	}
	return undef;
}

method tile_dimensions {
    my $lat = shift;
    my $height = 111.32;   # km per degree latitude
    my $width  = 111.32 * cos(deg2rad($lat));
    my $area   = $width * $height;
    return { width => $width, height => $height, area => $area };
}

method db_lookup ($lat, $lon) {
	my %h = ( lat => int $lat, lon => int $lon ); # convert to integers eg +53
	$dbix->select('locations', 'name', \%h )->into(my $location);
	return $location;
}

method get_sql_trace_level { $sql_trace }

1;