#!/usr/bin/perl
use warnings;
use strict;
use autodie;
use Data::Dump qw(dump);

my $filename = 'Intervali_Openface_novi_rad_Boris_export.txt';

my $debug = $ENV{DEBUG} || 0;

sub col2nr {
	my @c = @_;
	my $i = 0;
	my $out;
	$out->{$_} = $i++ foreach @c;
	#warn "## col2nr ", dump( $out );
	return $out;
}

open(my $fh, '<', $filename);

my $h = <$fh>; chomp $h;
my @cols = split(/\t/, $h);
my $col2nr = col2nr @cols;

my $file2;
my $fh2;
my $last_id;

my @h2;
my $h2nr;
my $tell2;


while(<$fh>) {
	chomp;
	next if m/^\s*$/;	# FIXME corrput data
	my @v = split(/\t/, $_);
	warn "#[ $_ ]\n" if $debug;

	if ( ! $last_id || $last_id ne $v[0] ) {
		my $patt = 'originals_2023-07-07/' . $v[0] . '*.csv';
		$file2 = glob $patt;
		$file2 ||= glob 'originals_2023-07-07/' . $v[0] . '*.csv';

		if ( ! defined $file2 ) {
			warn "SKIP ERROR: $patt glob didn't find anything for v[0]=$v[0] [$_]";
			next;
		}

		if ( ! -r $file2 ) {
			warn "ERROR: $patt glob file $file2 not readable: $! SKIPPING [$_]";
			next;
		}

		warn "# file2 $file2 [$v[0]]";
		open($fh2, '<', $file2);
		$last_id = $v[0];
		@h2 = ();
	}

	sub col_v {
		my $name = shift;
		my $i = $col2nr->{$name};
		die "can't find $name" unless defined $i;
		my $v = $v[$i];
		die "can't find $i : $name in ",dump( \@v ) unless defined $v;
		warn "## col_v $name -> $i -> $v",dump( \@v ) if $debug > 1;
		return $v;
	}

	#my $start = $v[ $col2nr->{'Start (s)'} ];
	#my $stop  = $v[ $col2nr->{'Stop (s)'} ];

	#my $start = col_v 'Start (s)';
	#my $stop  = col_v 'Stop (s)';

	my $start = $v[10];
	my $stop  = $v[11];
	warn "# start: $start - stop: $stop\n" if $debug;

	my @sum;
	my $count = 0;

	while(<$fh2>) {
		chomp; s/\r+$//;
		warn "## fh2 [ $_ ]\n" if $debug;
		#my @v2 = map { s/^\s+//; $_ } split(/\t/,$_);
		my @v2 = map { s/^\s+//; $_ } split(/[;,]\s*/,$_);
		if ( ! @h2 ) {
			@h2 = @v2;
			$h2nr = col2nr @h2;
			$tell2 = tell($fh2);
			warn "# h2nr ",dump( $h2nr ) if $debug;
			next;
		}

		sub col_v2 {
			my $name = shift;
			my $i = $h2nr->{$name};
			die "can't find $name in ",dump( $h2nr ) unless defined $i;
			my $v = $v2[$i];
			die "can't find $i : $name in ",dump( \@v2 ) unless defined $v;
			return "### col_v2 $name $i = $v";
			return $v;
		}


		#my $timestamp = $v2[ $h2nr->{'timestamp'} ] || die;
		#my $timestamp = col_v2 'timestamp';
		my $timestamp = $v2[2];

		#$timestamp /= 1000 if $timestamp =~ m/^\d+$/;	# FIXME fix corrputed input data
		#warn "XXX filter $start - $stop from ",dump( $timestamp ); #$h2nr, \@v2 );

		# Argument "1.324.400" isn't numeric
		$timestamp =~ s/^(\d+)\.(\d\d\d)\.(\d\d\d)$/$1$2.$3/;

		if ( $timestamp !~ m/^\d+(\.\d+)$/ ) {
			warn "timestamp [$timestamp] from $file2 not numeric [$_]";
		}

		if ( $timestamp < $start ) {
			#warn "## $start > @v2";
		} elsif ( $timestamp < $stop ) {
			$tell2 = tell($fh2);
			foreach my $n ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_c'} ) {
				$sum[$n] += $v2[$n];
				#warn "sum $n $sum[$n] $v2[$n]\n";
			}
			$count++;
			#warn "XXX $count sum $h2nr->{'AU01_r'} .. $h2nr->{'AU45_c'} = ",dump( \@sum );
		} else {
			warn "# seek $tell2" if $debug;
			seek $fh2, $tell2, 0;

			die "count is 0" if $count == 0;

			#warn "XXX $file2 count=$count XXX ", $h2nr->{'AU01_r'},' - ', $h2nr->{'AU45_r'} , " YYY ", $h2nr->{'AU01_c'}, ' - ', $h2nr->{'AU45_c'}, ' sum=', dump( \@sum );
			my @add_cols;
			push @add_cols, map { $sum[$_] / $count } ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_r'} );
			push @add_cols,	map { $sum[$_] }          ( $h2nr->{'AU01_c'} .. $h2nr->{'AU45_c'} );
			warn "# add_cols = ",dump( \@add_cols ) if $debug;
			print join("\t", @v, @add_cols ), "\n";

			@sum = ();
			$count = 0;

			warn "# last" if $debug;
			last;
		}
	}
}

__END__


my $col2nr;

# prema filenamenu iz $v[0]
# Start (s)       Stop (s)

my $lookup_file = '

# col timestamp
# 
