import csv
import os
from typing import Tuple

from .blackbox_log_viewer_csv_loader import BlackboxLogViewerCsvLoader

# string fragment for identifying the main fields header row in CSV file
CSV_HEADER_ROW_FRAGMENT = "loopIteration"


class BlackboxDecodeCsvLoader(BlackboxLogViewerCsvLoader):
    """Loads CSV data generated by blackbox_decode.
    For now, it is the same as for CSV generated by Blackbox Log Viewer.
    """

    # keycheck for 'usecols' only reads usefull traces, uncomment if needed
    CSV_FIELDS = ['time (us)',
                  'rcCommand[0]', 'rcCommand[1]', 'rcCommand[2]', 'rcCommand[3]',
                  'axisP[0]', 'axisP[1]', 'axisP[2]',
                  'axisI[0]', 'axisI[1]', 'axisI[2]',
                  'axisD[0]', 'axisD[1]', 'axisD[2]',
                  'gyroADC[0]', 'gyroADC[1]', 'gyroADC[2]',
                  'gyroData[0]', 'gyroData[1]', 'gyroData[2]',
                  'ugyroADC[0]', 'ugyroADC[1]', 'ugyroADC[2]',
                  'debug[0]', 'debug[1]', 'debug[2]', 'debug[3]',
                  ]

    TIME_FIELD = "time (us)"

    @staticmethod
    def is_applicable(path: str) -> bool:
        if ".csv" != os.path.splitext(path)[1].lower():
            return False
        with open(path) as f:
            for row in csv.reader(f):
                if row[0] == CSV_HEADER_ROW_FRAGMENT:
                    # CSV begins with main fields
                    return True
                return False
        return False

    def _read_headers(self, path: str) -> Tuple[dict]:
        # headers are read from the BBL file by BblLoader
        return tuple()

    def _read_data(self, path: str) -> Tuple[dict]:
        # delegate to super class to parse CSV the same way
        return super()._read_data(path)

import csv
import os
from typing import Tuple

from .blackbox_log_viewer_csv_loader import BlackboxLogViewerCsvLoader

# string fragment for identifying the main fields header row in CSV file
CSV_HEADER_ROW_FRAGMENT = "loopIteration"


class BlackboxDecodeCsvLoader(BlackboxLogViewerCsvLoader):
    """Loads CSV data generated by blackbox_decode.
    For now, it is the same as for CSV generated by Blackbox Log Viewer.
    """

    # keycheck for 'usecols' only reads usefull traces, uncomment if needed
    CSV_FIELDS = ['time (us)',
                  'rcCommand[0]', 'rcCommand[1]', 'rcCommand[2]', 'rcCommand[3]',
                  'axisP[0]', 'axisP[1]', 'axisP[2]',
                  'axisI[0]', 'axisI[1]', 'axisI[2]',
                  'axisD[0]', 'axisD[1]', 'axisD[2]',
                  'gyroADC[0]', 'gyroADC[1]', 'gyroADC[2]',
                  'gyroData[0]', 'gyroData[1]', 'gyroData[2]',
                  'ugyroADC[0]', 'ugyroADC[1]', 'ugyroADC[2]',
                  'debug[0]', 'debug[1]', 'debug[2]', 'debug[3]',
                  ]

    TIME_FIELD = "time (us)"

    @staticmethod
    def is_applicable(path: str) -> bool:
        if ".csv" != os.path.splitext(path)[1].lower():
            return False
        with open(path) as f:
            for row in csv.reader(f):
                if row[0] == CSV_HEADER_ROW_FRAGMENT:
                    # CSV begins with main fields
                    return True
                return False
        return False

    def _read_headers(self, path: str) -> Tuple[dict]:
        # headers are read from the BBL file by BblLoader
        return tuple()

    def _read_data(self, path: str) -> Tuple[dict]:
        # delegate to super class to parse CSV the same way
        return super()._read_data(path)

# string fragment for identifying the main fields header row in CSV file Loads CSV data generated by blackbox_decode.     For now, it is the same as for CSV generated by Blackbox Log Viewer. # keycheck for 'usecols' only reads usefull traces, uncomment if needed # CSV begins with main fields # headers are read from the BBL file by BblLoader # delegate to super class to parse CSV the same way