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_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():
    # use import pdb; pdb.set_trace() to open interactive debugger to examine what is returned
    #import pdb; pdb.set_trace()
    code_run = code.get_vis_figure(tstep=0, selected_date='20220808')
    assert code_run[1][1].children.children.children[2] == '08 Aug 2022 00-06 UTC'
    assert code_run[1][1].id == 'vis-info'
    assert code_run[1][2].children[0].children[0].className == 'vis-legend-point'

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
