import logging

from hdf5.hdf5 import HDF5

logger = logging.getLogger(__name__)

class HDF5_fdi(HDF5):

    prefix = 'fdi'

    def __init__(self, log_folder):
        super().__init__(log_folder)


    def _create_logfile(self, prefix=prefix):
        super()._create_logfile(prefix)


    def new_log(self):
        grp = 'raw motor data'
        self._create_logfile()
        # add raw motor data group
        self._create_group(grp)
        self._create_dataset(grp, 'time', 'seconds')
        self._create_dataset(grp, 'voltage', 'Volts', daq='9239')
        self._create_dataset(grp, 'current', 'Amps', daq='9239')
        self._create_dataset(grp, 'str pot', 'Volts', daq='9239')
        self._create_dataset(grp, 'encoder', 'counts', daq='9361')
        # add processed motor data group
        grp = 'motor data'
        self._create_group(grp)
        self._create_dataset(grp, 'time', 'seconds')
        self._create_dataset(grp, 'voltage', 'Volts')
        self._create_dataset(grp, 'current', 'Amps')
        self._create_dataset(grp, 'str pot', 'mm')
        self._create_dataset(grp, 'encoder', 'radians')
        self._create_dataset(grp, 'x_motor_previous', 'radians')
        # add ahrs data group                                                                                              
        self._create_group('ahrs data')
        self._create_dataset('ahrs data', 'time', 'seconds')
        self._create_dataset('ahrs data', 'pitch', 'radians')
        # add residuals group                                                                                              
        self._create_group('detection residuals')
        self._create_dataset('detection residuals', 'time', 'seconds')
        self._create_dataset('detection residuals', 'r0')
        self._create_dataset('detection residuals', 'r1')
        self._create_dataset('detection residuals', 'r2')
        self._create_dataset('detection residuals', 'r3')


    def append_motor_data_generic(self, time, data, grp):
        self._append_scalar_to_dataset_in_group(time, 'time', grp)
        self._append_scalar_to_dataset_in_group(data[0], 'voltage', grp)
        self._append_scalar_to_dataset_in_group(data[1], 'current', grp)
        self._append_scalar_to_dataset_in_group(data[2], 'str pot', grp)
        self._append_scalar_to_dataset_in_group(data[3], 'encoder', grp)
        

    def append_raw_motor_data(self, time, data):
        grp = 'raw motor data'
        self.append_motor_data_generic(time, data, grp)


    def append_motor_data(self, time, data):
        group = 'motor data'
        self.append_motor_data_generic(time, data, group)
        self._append_scalar_to_dataset_in_group(data[4], 'x_motor_previous', group)


    def append_pitch(self, time, pitch):
        self._append_scalar_to_dataset_in_group(time, 'time', 'ahrs data')
        self._append_scalar_to_dataset_in_group(pitch, 'pitch', 'ahrs data')


    def append_detection_residuals(self, time, r):
        self._append_scalar_to_dataset_in_group(time, 'time', 'detection residuals')
        self._append_scalar_to_dataset_in_group(r[0], 'r0', 'detection residuals')
        self._append_scalar_to_dataset_in_group(r[1], 'r1', 'detection residuals')
        self._append_scalar_to_dataset_in_group(r[2], 'r2', 'detection residuals')
        self._append_scalar_to_dataset_in_group(r[3], 'r3', 'detection residuals')
