# -*- coding: utf-8 -*-
"""
Created on Tue Oct 12 12:26:08 2021

@author: jkp4
"""
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State

import json
import numpy as np
import os
import pandas as pd

from mcvqoe.hub.eval_app import app

import mcvqoe.hub.eval_shared as eval_shared



#-----------------------[Begin layout]---------------------------
# TODO: Say something about common thinning fctor if data can't be thined

measurement = 'm2e'
layout = eval_shared.layout_template(measurement)

    
def format_m2e_results(m2e_eval, digits=6):
    """
    Format results from mouth2ear.evaluate object to be in nice HTML.

    Parameters
    ----------
    m2e_eval : mcvqoe.mouth2ear.evaluate
        DESCRIPTION.

    Returns
    -------
    children : html.Div
        DESCRIPTION.

    """
    if np.isnan(m2e_eval.common_thinning):
        thin_msg = f'No common thinning factor identified, statistics displayed for unthinned data'
    else:
        thin_msg = f'{m2e_eval.common_thinning}'
    pretty_mean = eval_shared.pretty_numbers(m2e_eval.mean, digits)
    pretty_ci = eval_shared.pretty_numbers(m2e_eval.ci, digits)
    children = html.Div([
        html.H6('Mean mouth-to-ear latency'),
        html.Div(f'{pretty_mean} seconds'),
        html.H6('95% Confidence Interval'),
        html.Div(f'{pretty_ci} seconds'),
        html.H6('Thinning factor'),
        html.Div(thin_msg),
        ],
        style=eval_shared.style_results,
        # className='six columns',
        )
    return children

# --------------[Callback functions (order matters here!)]--------------------
@app.callback(
    Output(f'{measurement}-measurement-results', 'children'),
    Output(f'{measurement}-measurement-formatting', 'children'),
    Output(f'{measurement}-scatter', 'figure'),
    Output(f'{measurement}-hist', 'figure'),
    Output(f'{measurement}-talker-select', 'options'),
    Output(f'{measurement}-session-select', 'options'),
    Input(f'{measurement}-json-data', 'data'),
    Input(f'{measurement}-thin-select', 'value'),
    Input(f'{measurement}-talker-select', 'value'),
    Input(f'{measurement}-session-select', 'value'),
    Input(f'{measurement}-x-axis', 'value'),
    Input(f'{measurement}-measurement-digits', 'value'),
    )
def update_plots(jsonified_data, thin, talker_select, session_select, x, meas_digits):
    """
    Update all plots

    Parameters
    ----------
    jsonified_data : TYPE
        DESCRIPTION.
    thin : TYPE
        DESCRIPTION.
    talker_select : TYPE
        DESCRIPTION.
    session_select : TYPE
        DESCRIPTION.
    x : TYPE
        DESCRIPTION.

    Returns
    -------
    return_vals : TYPE
        DESCRIPTION.

    """
    
    if jsonified_data is not None:
        try:
            json_data = json.loads(jsonified_data)
            if 'error' in json_data:
                error_out = '. '.join(json_data['error'])
                raise RuntimeError(error_out)
            else:
                m2e_eval = eval_shared.load_json_data(jsonified_data, f'{measurement}')

            thinned = thin == 'True'
            if x == 'index':
                x = None
            if talker_select == []:
                talker_select = None
            if session_select == []:
                session_select = None
            
            fig_scatter = m2e_eval.plot(
                x=x,
                thinned=thinned,
                talkers=talker_select,
                test_name=session_select,
                color_palette=eval_shared.plotly_color_palette,
                )
            fig_histogram = m2e_eval.histogram(
                thinned=thinned,
                talkers=talker_select,
                test_name=session_select,
                color_palette=eval_shared.plotly_color_palette,
                )
            
            talkers = np.unique(m2e_eval.data['Filename'])
            talker_options = [{'label': i, 'value': i} for i in talkers]
            
            sessions = m2e_eval.test_names
            session_options = [{'label': i, 'value': i} for i in sessions]
            
            res = format_m2e_results(m2e_eval, meas_digits)
            res_formatting = eval_shared.measurement_digits('grid', meas_digits,
                                                            measurement=measurement)
            return_vals = (
                res,
                res_formatting,
                fig_scatter,
                fig_histogram,
                talker_options,
                session_options
                )
        except Exception as e:
            print(e)
            return_vals = eval_shared.failed_process(measurement, msg=e.args)
        
    else:
        return_vals = eval_shared.failed_process(measurement, )
        
    return return_vals
        


        