import pytest
from datetime import datetime
from datetime import timedelta
import importlib
code = importlib.import_module('tools')
from data_handler import END_DATE
from data_handler import FREQ

FMT_ISO = "%Y%m%d"
FMT_DASH = "%Y-%m-%d"
FMT_MON = "%d %b %Y"
FMT_MON_HR = f"%Hh {FMT_MON}"
FMT_FULLMON = "%d %B %Y"
FMT_FULLMON_HR = f"%Hh {FMT_FULLMON}"
FMT_HR = "%H:%M:%S"
FMT_DASH_HR = f"{FMT_DASH} {FMT_HR}"
EDATE_OBJ = datetime.strptime(END_DATE, FMT_ISO)
EDATE_PREV = (datetime.strptime(END_DATE, FMT_ISO) - timedelta(days=7)).strftime(FMT_ISO)

# def test_download_image_link():
#     assert code.download_image_link(['median', 'monarch'], 'OD550_DUST', END_DATE, 1, False) == f'assets/comparison/all/od550_dust/2022/08/20220808_all_01.png'
# 
#     assert code.download_image_link(['median'], 'DEPW_DUST', '20220808', 6, True) == 'assets/comparison/median/depw_dust/2022/08/20220808_median_loop.gif'

def test_get_timeseries():
    assert code.get_timeseries('median', END_DATE, 'OD550_DUST', 45, 45, forecast=False).layout.title.text == 'Dust Optical Depth @ lat = 45 and lon = 45'

    assert code.get_timeseries('monarch', END_DATE, 'SCONC_DUST', 5, 44, forecast=True).layout.title.text == 'Dust Surface Conc. (µg/m³) @ lat = 5 and lon = 44'

# def test_get_single_point():
#     assert float(code.get_single_point('median', '20220808', 1, 'SCONC_DUST', 5, 44)) == 6.047785205964828e-08
#     assert float(code.get_single_point('median', '20220808', 3, 'OD550_DUST', 25, 40)) == 0.47600093483924866
#     assert float(code.get_single_point('cams', '20220808', 12, 'SCONC_DUST', 15, 4)) == 1.8746418106729834e-07

def test_get_scores_figure():
    run1 = code.get_scores_figure('aeronet', 'monarch', 'bias', EDATE_OBJ.strftime("%Y%m"))
    assert str(run1.data[0]) == 'Scattermapbox()'
    assert str(run1.data[1].name) == 'MBE score'

    run2 = code.get_scores_figure('modis', 'median', 'bias', EDATE_OBJ.strftime("%Y%m"))
    assert str(run2.data[0]) == 'Scattermapbox()'
    assert list(run2.data[1].lat[1:5]) ==[0.25, 0.25, 0.25, 0.25]

def test_get_prob_figure():
    assert "Run: {edate} Valid: {edate}".format(edate=EDATE_OBJ.strftime(FMT_MON)) in str(code.get_prob_figure('OD550_DUST', prob=0.5, day=0, selected_date=END_DATE)[2].children)
    assert code.get_prob_figure('OD550_DUST', prob=0.8, day=1, selected_date=END_DATE)[1].classes ==[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def test_get_was_figure():
    result = "GeoJSON(hideout={'colorscale': ['green', 'gold', 'darkorange', 'red'], 'bounds': [0, 1, 2, 3], 'style': {'weight': 1, 'opacity': 1, 'color': 'white', 'dashArray': '3', 'fillOpacity': 0.7}, 'colorProp': 'value'}, hoverStyle={'arrow': {'weight': 2, 'color': 'white', 'dashArray': '', 'fillOpacity': 0.7}}, options={'style': {'variable': 'forecastTab.wasMaps.styleHandle'}}, url='/dashboard/assets/geojsons/was/burkinafaso/geojson/%(edate)s/%(edate)s_SCONC_DUST_0.geojson'" % { 'edate': END_DATE }
    assert result in str(code.get_was_figure(was='burkinafaso', day=0, selected_date=END_DATE))

    result2 = "GeoJSON(hideout={'colorscale': ['green', 'gold', 'darkorange', 'red'], 'bounds': [0, 1, 2, 3], 'style': {'weight': 1, 'opacity': 1, 'color': 'white', 'dashArray': '3', 'fillOpacity': 0.7}, 'colorProp': 'value'}, hoverStyle={'arrow': {'weight': 2, 'color': 'white', 'dashArray': '', 'fillOpacity': 0.7}}, options={'style': {'variable': 'forecastTab.wasMaps.styleHandle'}}, url='/dashboard/assets/geojsons/was/chad/geojson/%(edate)s/%(edate)s_SCONC_DUST_0.geojson')" % { 'edate': END_DATE }
    assert result2 in str(code.get_was_figure(was='chad', day=0, selected_date=END_DATE))

def test_get_vis_figure():
    result = "Div(children=['Visibility reduced by airborne dust', Br(None), '08 August 2022 00-06 UTC'], id='vis-info', className='info', style={'position': 'absolute', 'top': '10px', 'left': '10px', 'zIndex': '1000', 'fontFamily': "
    result2 = "Div(children=[Div(children=[Span(children='', className='vis-legend-point', style={'backgroundColor': '#714921'}), Span(children='<1 km', className='vis-legend-label')], style={'display': 'block'}), Div(children=[Span(children='', className='vis-legend-point', style={'backgroundColor': '#da7230'}), Span(children='1 - 2 km', className='vis-legend-label')],"
    run1 = str(code.get_vis_figure(tstep=0, selected_date='20220808')[1][1])
    run2 = str(code.get_vis_figure(tstep=0, selected_date='20220808')[1][2])
    assert result in run1
    assert result2 in run2

def test_get_models_figure():
    result = f"/dashboard/assets/geojsons/median/geojson/{END_DATE}/00_{END_DATE}_OD550_DUST.geojson"
    run1 = str(code.get_models_figure(model='median', var='OD550_DUST',
        selected_date=END_DATE, tstep=0).children[2].url)
    assert result in run1

    result2 = "{'tag': 'model-tile-layer', 'index': 'monarch'}"
    run2 = str(code.get_models_figure(model='monarch', var='SCONC_DUST', selected_date=END_DATE, tstep=1,
        hour=3, static=True, aspect=(1, 1), center=None,
        view='carto-positron', zoom=None, layer=None, tag='empty').children[0].id)
    assert result2 in run2
