import pandas as pd
import os
import re
from pathlib import Path
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import sys

tstamp_s = '%d.%m.%Y %H:%M:%S.%f'
ox_dtime_format = '%d.%m.%Y %H:%M'

path_itog_brd_data = '../data/brd_data/'


class PathFileNotFound(Exception):
    pass

# recursive search files by pattern in specified directory
def find_required_files(root_dir, pattern):
    result = []
    for dirpath, _, filenames in os.walk(root_dir):
        for filename in filenames:
            match = re.match(pattern, filename)
            if match:
                result.append(dirpath + '/' + filename)

    if len(result) == 0:
        raise PathFileNotFound(
            f'error: check that the path is correct ({root_dir}) or files pattern is correct ({pattern})')

    return sorted(result)

# read all finded files into pandas dataFrame
def read_files_into_df(fname_list, column_list, dtype_columns={}):
    data_itog = pd.DataFrame()
    epoch_start = pd.Timestamp('2000-01-01')

    for fname in fname_list:
        data = pd.read_csv(fname, sep=r'\s+', dtype=str)
        data = data.dropna()
        data = data[column_list]

        if 'TIME' in column_list:
            # convert TIME value to human-readable timestamp (sinse epoch 01.01.2000)
            time = data['TIME'].astype(float)
            tstamp = epoch_start + pd.to_timedelta(time, unit='s')
            timestamp = tstamp.dt.strftime(tstamp_s)
            data['timestamp'] = timestamp

            # clear dataframe rows where time value == 0
            data['time'] = time
            data_clear = data.query('time != 0.0')

        data_itog = pd.concat([data_itog, data_clear], ignore_index=True)

    return data_itog


def collect_tm_brd_files(root_dir_tm_data, column_list, column_list_itog):
    patterns_tm = [r'mvn_tm_brd01_(.*)', r'mvn_tm_brd02_(.*)', r'mvn_tm_brd03_(.*)',
                   r'mvn_tm_brd04_(.*)']

    for pattern in patterns_tm:
        fname = path_itog_brd_data + pattern[:12] + '.csv'
        try:
            found_files = find_required_files(root_dir_tm_data, pattern)
            data = read_files_into_df(found_files, column_list, dtype_columns={11: float})
        except KeyError as e:
            print(
                f'error in collect_tm_brd_files: the specified column name was not found in the data file (path: {root_dir_tm_data}) ({e})')
            break
        except Exception as e:
            print(f'error in collect_tm_brd_files: {e}')
            break

        data.to_csv(fname, index=False, sep=';', columns=column_list_itog, encoding='utf-8-sig')
        print('data saved: ' + fname)


if __name__ == "__main__":

    if len(sys.argv) != 2:
        print("Usage: python tm_brd_parser.py /path/to/tm_brd_data/")
    else:
        root_dir_tm_data = sys.argv[1]

        print('collect raw brd tm data into one file for each brd')

        column_list = ['TIME', 'PER_1Hz', 'ST_HV']
        column_list_itog = ['TIME', 'timestamp', 'PER_1Hz', 'ST_HV']

        # collect_tm_brd_files(root_dir_tm_data, column_list, column_list_itog)

        ## plot 'evolution' 1 Hz from tm brd data
        print('plot evolution 1 Hz from tm brd data')

        fname = path_itog_brd_data + 'mvn_tm_brd01.csv'
        df = pd.read_csv(fname, sep=';', parse_dates=['timestamp'], date_format="%d.%m.%Y %H:%M:%S.%f")

        # print(df)
        plt.plot(df['timestamp'], df['PER_1Hz'], '.')
        plt.grid()
        plt.show()
