import os
import numpy as np
import pandas as pd
from CHECLabPy.plotting.setup import Plotter
from CHECLabPy.spectrum_fitters.gentile import pe_signal
from CHECLabPy.waveform_reducers.cross_correlation import CrossCorrelation
from target_calib import CameraConfiguration
from IPython import embed


class SPEHist(Plotter):
    def plot(self, hist, edges, between, x, y, label):
        self.ax.hist(between, bins=edges, weights=hist, alpha=0.2)
        self.ax.plot(x, y, label=label)

    def finish(self):
        self.ax.set_xlabel("Amplitude (mV)")
        self.ax.set_ylabel("N")
        self.add_legend()


def main():
    input_path = "/Volumes/gct-jason/thesis_data/checs/lab/dynrange/tf/tf_poly/spe.h5"
    file_dir = os.path.dirname(os.path.abspath(__file__))
    output_dir = os.path.join(file_dir, "outputs")

    dead = [677, 293, 27, 1925]

    store = pd.HDFStore(input_path)
    df = store['coeff_pixel']
    df_array = store['array_camera']
    df = df.loc[~df['pixel'].isin(dead)]
    df_mean = df.mean().to_dict()

    norm = (df_mean['norm0'] + df_mean['norm1'] + df_mean['norm2'])/3
    spe = df_mean['spe']

    config = CameraConfiguration("1.1.0")
    ref_path = config.GetReferencePulsePath()
    cc = CrossCorrelation(1, 96, reference_pulse_path=ref_path)

    d = dict(
        norm=1,
        eped=cc.get_pulse_height(df_mean['eped']),
        eped_sigma=cc.get_pulse_height(df_mean['eped_sigma']),
        spe=cc.get_pulse_height(df_mean['spe']),
        spe_sigma=cc.get_pulse_height(df_mean['spe_sigma']),
        lambda_=1,
        opct=df_mean['opct'],
        pap=df_mean['pap'],
        dap=df_mean['dap']
    )

    hist = df_array.loc[0, 'hist'] / (norm * 800)
    edges = cc.get_pulse_height(df_array.loc[0, 'edges'])
    between = cc.get_pulse_height(df_array.loc[0, 'between'])

    x = np.linspace(-5, 15, 1000)
    y = pe_signal(1, x, **d)

    p_hist = SPEHist()
    label = "fadc_amplitude = spe = {:.3f}".format(d['spe'])
    p_hist.plot(hist, edges, between, x, y, label)
    output_path = os.path.join(output_dir, "checs_fadc_amplitude.pdf")
    p_hist.save(output_path)


if __name__ == '__main__':
    main()
