from dqc_wrapper.utils.utils_bokeh import BokehPlot
from dqc_wrapper.unit_tests.conftest import dirs
from dqc_wrapper.test06_valid_ranges import ValidRangesTest
from bokeh.plotting import figure
from bokeh.plotting.figure import Figure
import pandas as pd
from datetime import datetime
import numpy as np
from pathlib import Path


def prepare():
    vr = ValidRangesTest(dirs.log_dir, 'testname.ini', [dirs.nc_file],
                           'tas', '', '',  0, '',  dirs.test_dir, 'tas')
    vr.check_valid_ranges()
    return BokehPlot('test-valid-ranges.html', dirs.stats_csv, dirs.hist_csv,
                     'tas', '', 0, 50, {'varname': 'tas', 'unit': 'degC'})


def test_plot_boxplot_lines():
    bokeh_plot = prepare()
    bokeh_plot.plot_boxplot_lines()


def test_draw_citations():
    bokeh_plot = prepare()
    p = figure(plot_width=100, plot_height=100, x_range=(0, 1.5),
               y_range=(0, 1),
               tools="")
    res = bokeh_plot.draw_citations(p, 'cit', 0, 0, 'Test')
    assert type(res) == Figure


def test_plot_instructions():
    bokeh_plot = prepare()
    df = pd.read_csv(dirs.stats_csv)
    df['date'] = datetime(2020, 1, 22)
    bokeh_plot.plot_instructions(df)


def test_add_valid_minmax():
    bokeh_plot = prepare()
    p = figure(plot_width=100, plot_height=100, x_range=(0, 1.5),
               y_range=(0, 1),
               tools="")
    bokeh_plot.add_valid_minmax(p, np.array([[100]]), np.array(range(100)), 0)


def test_plot_histogram_figure():
    bokeh_plot = prepare()
    df = pd.read_csv(dirs.stats_csv)

    df = df.rename(columns={'Min': -60, 'Max': 50, 'Mean': 0, 'Std': 0,
                            'Med': 0, 'q1': 0, 'q3': 0, 'IQR': 0, 'skew': 0})
    df = pd.concat([df]*2, ignore_index=True)
    df['File'] = ['', 'total']
    res = bokeh_plot.plot_histogram_figure(df)
    assert type(res) == Figure


def test_plot_histograms():
    bokeh_plot = prepare()
    bokeh_plot.plot_histograms()
    Path('test-valid-ranges.html').unlink()