from dqc_wrapper.utils.utils_bokeh import BokehPlot, TemporalPlot
from dqc_wrapper.unit_tests.conftest import dirs
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
import xarray as xr
from os.path import abspath


def prepare():
    return BokehPlot('test title', 'test-valid-ranges.html', dirs.stats_csv, dirs.hist_csv,
                     'tas', '', 0, 50, {'total': {'vmin_nmax': 0, 'vmax_nmax': 50}},
                     {'varname': 'tas', 'unit': 'degC'}, 'degC', '', '')


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


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_image():
    bokeh_plot = prepare()
    res = bokeh_plot.plot_image(dirs.test_image)
    assert type(res) == Figure


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


def test_add_valid_minmax():
    bokeh_plot = prepare()
    fig = figure(plot_width=100, plot_height=100, x_range=(0, 1.5),
               y_range=(0, 1),
               tools="")
    bokeh_plot.add_valid_minmax('total', fig, np.array([range(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.iloc[-1] = ['total'] + list(np.random.rand(7))
    df = df.rename(columns={'File_dt': 'File'})
    df = df.drop(['dataType'], axis=1)
    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()


def test_generate_figure():
    ds = xr.open_dataset(dirs.nc_file)
    df = pd.read_csv(dirs.stats_csv)
    test = TemporalPlot('test', ds)
    res = test.generate_figure('test title', df, [2019, 2020], 0, 0)
    ds.close()
    assert type(res) == Figure


def test_plot_completeness():
    ds = xr.open_dataset(dirs.nc_file)
    test = TemporalPlot('test', ds)
    start = datetime(2019, 1, 1)
    end = datetime(2019, 4, 1)
    test.plot_completeness([start], [end])
    assert Path(abspath('test')).exists()
    Path(abspath('test')).unlink()


def test_plot_consistency():
    ds = xr.open_dataset(dirs.nc_file)
    test = TemporalPlot('test', ds)
    start = datetime(2019, 1, 1)
    end = datetime(2019, 4, 1)
    test.plot_consistency([start, end])
    assert Path(abspath('test')).exists()
    Path(abspath('test')).unlink()