import unittest
import numpy as np
import helperFunctions as hF
import matplotlib.pyplot as plt
from CellData import icelldata_of_dir
from Baseline import BaselineCellData
import os


class HelperFunctionsTester(unittest.TestCase):
    reference_base_spikes = {'2012-12-21-an-invivo-1': 39, '2012-07-12-ag-invivo-1': 21, '2012-12-13-ac-invivo-1': 19,
                             '2012-12-20-ac-invivo-1': 43, '2013-01-08-ad-invivo-1': 29, '2012-06-27-an-invivo-1': 19,
                             '2012-12-13-ah-invivo-1': 35, '2012-12-13-ag-invivo-1': 26, '2012-12-13-an-invivo-1': 29,
                             '2012-12-21-ai-invivo-1': 55, '2012-12-20-aa-invivo-1': 24, '2012-12-21-am-invivo-1': 26,
                             '2012-06-27-ah-invivo-1': 27, '2012-07-12-ap-invivo-1': 35, '2012-12-21-ak-invivo-1': 30}

    noise_levels = [0, 0.05, 0.1, 0.2]
    frequencies = [0, 1, 5, 30, 100, 500, 750, 1000]

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test__vector_strength__is_1(self):
        length = 2000
        rel_spike_times = np.full(length, 0.3)
        eod_durations = np.full(length, 0.14)

        self.assertEqual(1, round(hF.__vector_strength__(rel_spike_times,eod_durations), 2))

    def test__vector_strength__is_0(self):
        length = 2000
        period = 0.14
        rel_spike_times = np.arange(0, period, period/length)
        eod_durations = np.full(length, period)

        self.assertEqual(0, round(hF.__vector_strength__(rel_spike_times, eod_durations), 5))

    def test_detect_spiketimes(self):
        count = 0
        for cell_data in icelldata_of_dir("../data/"):
            if os.path.basename(cell_data.get_data_path()) not in self.reference_base_spikes:
                continue

            print(cell_data.get_data_path())
            # if "21-ai" not in cell_data.get_data_path() and "20-ac" not in cell_data.get_data_path():
            #     continue

            spikes = np.array(cell_data.get_base_spikes()[0])

            time_length = 0.2
            time = cell_data.get_base_traces(cell_data.TIME)[0]
            length_data_points = int(time_length / cell_data.get_sampling_interval())

            start_idx = 0
            end_idx = length_data_points + 1
            end_idx = end_idx if end_idx <= len(time) else len(time)

            bot_lim_spikes = spikes[spikes > time[start_idx]]
            top_lim_spikes = bot_lim_spikes[bot_lim_spikes < time[end_idx]]

            expected = self.reference_base_spikes[os.path.basename(cell_data.get_data_path())]
            if len(top_lim_spikes) == expected:
                print("yay")
            else:

                print("detected: {:}, reference: {:}".format(len(top_lim_spikes), expected))
                print("nay")
                baseline = BaselineCellData(cell_data)
                baseline.plot_baseline(position=0, time_length=0.2)

            count += 1

    def test_automatic_splitting(self):
        for cell_data in icelldata_of_dir("../data/"):
            print(cell_data.get_data_path())
            v1 = cell_data.get_base_traces(cell_data.V1)[0]

            hF.detect_spike_indices_automatic_split(v1)

    # todo
    #  search_eod_start_and_end_times ? (not used anymore ?)
    #  eods_around_spikes
    #  calculate_phases

    # def test(self):
    #    test_distribution()