import argparse
import subprocess
import os
import io
import sys
import pandas as pd


def main():
    parser = argparse.ArgumentParser(
        description='Process long runs of T04 data, where mutiple (livesky) '
        'T04 files need to be combined, and then differenced. Attempt to '
        'only run the necessary processes to create any data files '
        'missing in the data pipeline.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
        )
    parser.add_argument(
        "--folder",
        default=".",
        help="Where to scan for T04 files."
        )
    parser.add_argument('-v', '--Verbose', action='count', default=0)
    parser.add_argument(
        "--viewdat",
        default='viewdat',
        help="Executable name for t0x2t0x."
        )
    parser.add_argument(
        '-n', "--dryrun",
        action="store_true",
        help="Don't actually run t0x2t0x."
        )
    parser.add_argument(
        "--time_stamp_csv",
        default="time_stamps.csv",
        help="Where to store results."
        )
    parser.add_argument(
        "--start_stop_csv",
        default="time_start_stop.csv",
        help="Where to store results."
        )
    parser.add_argument(
        "--force",
        action="store_true",
        help="force calling viewdat",
        )

    args = parser.parse_args()

    #os.makedirs(args.log_dir, exist_ok=True)

    if not os.path.isfile(args.time_stamp_csv) or args.force:
        records = []
        n=0
        for root, dirs, files in os.walk(args.folder):
            for f in files:
                if not f.endswith('T04') and not f.endswith('T02'):
                    continue
                #print(f'{f}')

                #n += 1
                #if n > 3:
                #    break

                cmd_list = [
                    args.viewdat,
                    '-i',
                    os.path.normpath(os.path.join(root, f)),
                ]
                print(' '.join(cmd_list))
                if not args.dryrun:
                    p = subprocess.Popen(cmd_list,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.STDOUT,
                                         )
                    p.wait()

                    if p.returncode != 0:
                        print(f'WARNING: {args.viewdat} on {f} return code {p.returncode}')
                        continue

                    for line in io.TextIOWrapper(p.stdout, encoding="utf-8"):
                        if not 'First time tag' in line:
                            continue
                        print(line.strip())
                        ls = line.strip().split()
                        records.append({
                            'root': root,
                            'f': f,
                            'start_week': float(ls[4]),
                            'start_sec': float(ls[5]),
                            'stop_week': float(ls[10]),
                            'stop_sec': float(ls[11]),
                            })
                        break
        df = pd.DataFrame(records)
        print(f'Saving {args.time_stamp_csv}')
        df.to_csv(args.time_stamp_csv, index=False)
    else:
        df = pd.read_csv(args.time_stamp_csv)
    print(df)

    min_max = None
    for index, row in df.iterrows():
        #print(f'{row.root=}')
        #root_f = os.path.join(row.root, row.f)
        if min_max is None:
            min_max = {
                row.root: {
                    'start_week': row.start_week,
                    'start_sec': row.start_sec,
                    'stop_week': row.stop_week,
                    'stop_sec': row.stop_sec,
                    }
                }
        elif row.root in min_max.keys():
            min_max_row = min_max[row.root]
            if row.start_week < min_max_row['start_week']:
                min_max_row['start_week'] = row.start_week
            if row.start_sec < min_max_row['start_sec']:
                min_max_row['start_sec'] = row.start_sec
            if row.stop_week > min_max_row['stop_week']:
                min_max_row['stop_week'] = row.stop_week
            if row.stop_sec > min_max_row['stop_sec']:
                min_max_row['stop_sec'] = row.stop_sec
            #print(min_max_row)
        else:
            d = {
                'start_week': row.start_week,
                'start_sec': row.start_sec,
                'stop_week': row.stop_week,
                'stop_sec': row.stop_sec,
                }
            min_max[row.root] = d
            #print(row.root, min_max[row.root])

    print(min_max.keys())
    #print(min_max)
    d_min_max = []

    for k, v in min_max.items():
        print(d['start_week'])
        d_min_max.append({
            'root': k, 
            'start_week': v['start_week'],
            'start_sec': v['start_sec'],
            'stop_week': v['stop_week'],
            'stop_sec': v['stop_sec'],
            })


    df_start_stop = pd.DataFrame(d_min_max)
    df_start_stop.to_csv(args.start_stop_csv, index=False)


if __name__ == '__main__':
    main()
