import pytest
from datetime import datetime
from datetime import timedelta
import importlib
code = importlib.import_module('callback_tools')
from map_handler import END_DATE
from map_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():
    code_run = code.get_vis_figure(tstep=0, selected_date='20230321')
    assert code_run[1].children[0].children[0].className == 'vis-legend-point'
    assert code_run[2].children.children.children[2] == '21 Mar 2023 00-06 UTC'
    assert code_run[2].id == 'vis-info'

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

    if END_DATE == '20230430':
        monarch_date = '20230429'
    result2 = f"/dashboard/assets/geojsons/NMMB-BSC/geojson/{monarch_date}/00_{monarch_date}_SCONC_DUST.geojson"
    run2 = str(code.get_model_figure(var='SCONC_DUST', model='monarch', tstep=1, hour=3, 
        selected_date=END_DATE)[0].url)
    assert result2 in run2
