import pytest
import importlib
from contextvars import copy_context
import dash
from dash._callback_context import context_value
from dash._utils import AttributeDict
code = importlib.import_module('tabs.forecast_callbacks')



# =======================START render forecast test ===========================
def test_render_forecast_tab_group_1():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "group-1-toggle.n_clicks"},{"prop_id": "group-2-toggle.n_clicks"},{"prop_id": "group-3-toggle.n_clicks"},{"prop_id": "variable-dropdown-forecast.value"},{"prop_id": "collapse-1.is_open"},{"prop_id": "collapse-2.is_open"},{"prop_id": "collapse-3.is_open"}]}))
        return code.render_forecast_tab(1, 0, 0, 'SCONC_DUST', True, False, False ) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output[0] == False
    assert output[1] == False
    assert output[2] == False
    assert output[3] == dash.no_update
    assert output[4] == dash.no_update
    assert output[5] == dash.no_update

def test_render_forecast_tab_group_2():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "group-1-toggle.n_clicks"},{"prop_id": "group-2-toggle.n_clicks"},{"prop_id": "group-3-toggle.n_clicks"},{"prop_id": "variable-dropdown-forecast.value"},{"prop_id": "collapse-1.is_open"},{"prop_id": "collapse-2.is_open"},{"prop_id": "collapse-3.is_open"}]}))
        return code.render_forecast_tab(0, 1, 0, 'SCONC_DUST', False, True, False ) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output[0] == False
    assert output[1] == True
    assert output[2] == False
    assert output[3] == False
    assert output[4] == False
    assert output[5] == dash.no_update

def test_render_forecast_tab_group_3():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "group-1-toggle.n_clicks"},{"prop_id": "group-2-toggle.n_clicks"},{"prop_id": "group-3-toggle.n_clicks"},{"prop_id": "variable-dropdown-forecast.value"},{"prop_id": "collapse-1.is_open"},{"prop_id": "collapse-2.is_open"},{"prop_id": "collapse-3.is_open"}]}))
        return code.render_forecast_tab(0,0,1, 'SCONC_DUST', False, False, True ) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output[0] == False
    assert output[1] == False
    assert output[2] == True
    assert output[3] == False
    assert output[4] == False
    assert output[5] == dash.no_update

def test_render_forecast_tab_SCONC_DUST():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "group-1-toggle.n_clicks"},{"prop_id": "group-2-toggle.n_clicks"},{"prop_id": "group-3-toggle.n_clicks"},{"prop_id": "variable-dropdown-forecast.value"},{"prop_id": "collapse-1.is_open"},{"prop_id": "collapse-2.is_open"},{"prop_id": "collapse-3.is_open"}]}))
        return code.render_forecast_tab(0,0,0, 'SCONC_DUST', False, False, True ) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output[0] == False
    assert output[1] == False
    assert output[2] == True
    assert output[3] == False
    assert output[4] == False
    assert output[5] == dash.no_update

def test_render_forecast_tab_OD550():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "group-1-toggle.n_clicks"},{"prop_id": "group-2-toggle.n_clicks"},{"prop_id": "group-3-toggle.n_clicks"},{"prop_id": "variable-dropdown-forecast.value"},{"prop_id": "collapse-1.is_open"},{"prop_id": "collapse-2.is_open"},{"prop_id": "collapse-3.is_open"}]}))
        return code.render_forecast_tab(0,0,0, 'OD550_DUST', False, False, True ) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output[0] == True
    assert output[1] == False
    assert output[2] == False
    assert output[3] == False
    assert output[4] == True
    assert output[5] == dash.no_update
# =======================END render forecast test ===========================


# =======================START update_prob_dropdown test ===========================
def test_update_prob_dropdown():
    assert str(code.update_prob_dropdown('OD550_DUST')) == "([{'label': '> 0.1 ', 'value': 'prob_0.1'}, {'label': '> 0.2 ', 'value': 'prob_0.2'}, {'label': '> 0.5 ', 'value': 'prob_0.5'}, {'label': '> 0.8 ', 'value': 'prob_0.8'}], 'prob_0.1')"

    assert str(code.update_prob_dropdown('SCONC_DUST')) == "([{'label': '> 50 µg/m³', 'value': 'prob_50'}, {'label': '> 100 µg/m³', 'value': 'prob_100'}, {'label': '> 200 µg/m³', 'value': 'prob_200'}, {'label': '> 500 µg/m³', 'value': 'prob_500'}], 'prob_50')"
# =======================END update_prob_dropdown test ===========================


# =======================START update_models_dropdown test ===========================
def test_update_models_dropdown():
    assert str(code.update_models_dropdown('OD550_DUST', ['median', 'monarch'])) == "([{'label': 'MULTI-MODEL', 'value': 'median', 'disabled': False}, {'label': 'MONARCH', 'value': 'monarch', 'disabled': False}, {'label': 'CAMS-IFS', 'value': 'cams', 'disabled': False}, {'label': 'DREAM8-CAMS', 'value': 'dream8-macc', 'disabled': False}, {'label': 'NASA-GEOS', 'value': 'nasa-geos', 'disabled': False}, {'label': 'MetOffice-UM', 'value': 'metoffice', 'disabled': False}, {'label': 'NCEP-GEFS', 'value': 'ncep-gefs', 'disabled': False}, {'label': 'EMA-RegCM4', 'value': 'ema-regcm4', 'disabled': False}, {'label': 'SILAM', 'value': 'silam', 'disabled': False}, {'label': 'LOTOS-EUROS', 'value': 'lotos-euros', 'disabled': False}, {'label': 'ICON-ART', 'value': 'icon-art', 'disabled': False}, {'label': 'NOA-WRF-CHEM', 'value': 'noa', 'disabled': False}, {'label': 'WRF-NEMO', 'value': 'wrf-nemo', 'disabled': False}, {'label': 'ALADIN', 'value': 'aladin', 'disabled': False}, {'label': 'ZAMG-WRF-CHEM', 'value': 'zamg', 'disabled': False}, {'label': 'MOCAGE', 'value': 'mocage', 'disabled': False}], ['median', 'monarch'], {'display': 'none'})"

    assert str(code.update_models_dropdown('SCONC_DUST', ['cams', 'silam'])) == "([{'label': 'MULTI-MODEL', 'value': 'median', 'disabled': False}, {'label': 'MONARCH', 'value': 'monarch', 'disabled': False}, {'label': 'CAMS-IFS', 'value': 'cams', 'disabled': False}, {'label': 'DREAM8-CAMS', 'value': 'dream8-macc', 'disabled': False}, {'label': 'NASA-GEOS', 'value': 'nasa-geos', 'disabled': False}, {'label': 'MetOffice-UM', 'value': 'metoffice', 'disabled': False}, {'label': 'NCEP-GEFS', 'value': 'ncep-gefs', 'disabled': False}, {'label': 'EMA-RegCM4', 'value': 'ema-regcm4', 'disabled': False}, {'label': 'SILAM', 'value': 'silam', 'disabled': False}, {'label': 'LOTOS-EUROS', 'value': 'lotos-euros', 'disabled': False}, {'label': 'ICON-ART', 'value': 'icon-art', 'disabled': False}, {'label': 'NOA-WRF-CHEM', 'value': 'noa', 'disabled': False}, {'label': 'WRF-NEMO', 'value': 'wrf-nemo', 'disabled': False}, {'label': 'ALADIN', 'value': 'aladin', 'disabled': False}, {'label': 'ZAMG-WRF-CHEM', 'value': 'zamg', 'disabled': False}, {'label': 'MOCAGE', 'value': 'mocage', 'disabled': False}], ['cams', 'silam'], {'display': 'none'})"
# =======================END update_models_dropdown test ===========================


# =======================START CARET TESTS ===========================
def test_rotate_models_caret():
    assert code.rotate_models_caret(True) == None
    assert code.rotate_models_caret(False) == {'top': '.05rem', 'transform': 'rotate(180deg)', '-ms-transform': 'rotate(180deg)', '-webkit-transform': 'rotate(180deg)'}

def test_rotate_prob_caret():
    assert code.rotate_prob_caret(False) == None
    assert code.rotate_prob_caret(True) == {'transform': 'rotate(0deg)', '-ms-transform': 'rotate(0deg)', '-webkit-transform': 'rotate(0deg)'}

def test_rotate_was_caret():
    assert code.rotate_was_caret(False) == None
    assert code.rotate_was_caret(True) == {'transform': 'rotate(0deg)', '-ms-transform': 'rotate(0deg)', '-webkit-transform': 'rotate(0deg)'}
# =======================END CARET TESTS ===========================

# =======================START SIDEBAR_BOTTOM TESTS ===========================
def test_sidebar_bottom_info():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "info-button.n_clicks"},{"prop_id": "download-button.n_clicks"},{"prop_id": "info-collapse.is_open"},{"prop_id": "download_collapse.is_open"}]}))
        return code.sidebar_bottom(1,0,True,False) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output == (False, False)

def test_sidebar_bottom_info2():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "info-button.n_clicks"},{"prop_id": "download-button.n_clicks"},{"prop_id": "info-collapse.is_open"},{"prop_id": "download_collapse.is_open"}]}))
        return code.sidebar_bottom(1,0,True,False) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output == (False, False)

def test_sidebar_bottom_download_clicked():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "info-button.n_clicks"},{"prop_id": "download-button.n_clicks"},{"prop_id": "info-collapse.is_open"},{"prop_id": "download_collapse.is_open"}]}))
        return code.sidebar_bottom(0,1,True,False) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output == (False, False)

def test_sidebar_bottom_download_clicked2():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "info-button.n_clicks"},{"prop_id": "download-button.n_clicks"},{"prop_id": "info-collapse.is_open"},{"prop_id": "download_collapse.is_open"}]}))
        return code.sidebar_bottom(0,1,False,False) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output == (True, False)
# =======================END SIDEBAR_BOTTOM TESTS ===========================

    
# =======================START DOWNLOAD_ANIM_LINK TESTS===========================
def test_download_anim_link():
    assert code.download_anim_link(['monarch'], 'SCONC_DUST', '20220831', 3) ==  'assets/comparison/monarch/sconc_dust/2022/08/20220831_monarch_loop.gif'
    assert code.download_anim_link(['median','monarch'], 'SCONC_DUST', '20220831', 3) ==  'assets/comparison/all/sconc_dust/2022/08/20220831_all_loop.gif'
    assert code.download_anim_link(['monarch'], 'OD550_DUST', '20220831', 3) ==  'assets/comparison/monarch/od550_dust/2022/08/20220831_monarch_loop.gif'
    assert code.download_anim_link(['median','monarch'], 'OD550_DUST', '20220831', 3) ==  'assets/comparison/all/od550_dust/2022/08/20220831_all_loop.gif'
# =======================END DOWNLOAD_ANIM_LINK TESTS===========================

def test_zoom_country():
    assert code.zoom_country(1, ['monarch'], 2, 45, 35) == ([2.0], [[45.0, 35.0]])
    assert code.zoom_country(1, ['median', 'monarch'], 2, 45, 35) == ([2.0, 2.0], [[45.0, 35.0], [45.0, 35.0]])

# def test_zooms():
#     def run_callback():
#         context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "{'tag': 'model-map', 'index': 'median', 'n_clicks': 0}"},{"prop_id": "{'tag': 'model-map', 'index': 'median', 'n_clicks': 0}"}]}))
#         return code.zooms([None, None],[{'tag': 'model-map', 'index': 'median', 'n_clicks': 0}, {'tag': 'model-map', 'index': 'monarch', 'n_clicks': 1}]) 
#     ctx = copy_context()
#     output = ctx.run(run_callback)
#     assert output == (True, False)
#
#
# ORJSON ERROR
# def test_update_was_styles_button():
#     def run_callback():
#         context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "view-style.n_clicks"},{"prop_id": "was-tile-layer.url"},{"prop_id": "view-style.active"}]}))
#         return code.update_was_styles_button.uncached([None, None, 1, None], ['https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png'], [True, False, False, False]) 
#
#     ctx = copy_context()
#     output = ctx.run(run_callback)
#     assert output == (True, False)

# ORJSON ERROR
# def test_models_popup():
#     def run_callback():
#         context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "model-map.click_lat_lng"},{"prop_id": "model-map.id"},{"prop_id": "model-map.children"},{"prop_id": "model-date-picker.date"},{"prop_id": "slider-graph.value"},{"prop_id": "variable-dropdown-forecast.value"},{"prop_id": "model-clicked-coords.data"},{"prop_id": "current-popups-stored.data"}]}))
#         return code.models_popup([[56.739260373724775, 91.93359375]], [], [], '20230404', 0, 'OD550_DUST', None, None) 
#
#     ctx = copy_context()
#     output = ctx.run(run_callback)
#     assert output == (True, False)


# =======================Start start_stop_autoslider tests===========================
def test_start_stop_autoslider_pause():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "btn-play.n_clicks"},{"prop_id": "slider-interval.disabled"},{"prop_id": "slider-graph.value"}]}))
        return code.start_stop_autoslider(1, False, 3) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output == (True, 1, {'display': 'block'}, 'fa fa-play text-center')

def test_start_stop_autoslider_play():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "btn-play.n_clicks"},{"prop_id": "slider-interval.disabled"},{"prop_id": "slider-graph.value"}]}))
        return code.start_stop_autoslider(1, True, 9) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert output == (False, 3, {'display': 'none'}, 'fa fa-pause text-center')
# =======================END start_stop_autoslider tests===========================

# =======================START update_slider tests===========================
def test_update_slider():
    assert code.update_slider.uncached(1) == 3
    assert code.update_slider.uncached(2) == 6
    assert code.update_slider.uncached(3) == 9
    assert code.update_slider.uncached(4) == 12
    assert code.update_slider.uncached(24) == 0
    assert code.update_slider.uncached(33) == 27
    assert code.update_slider.uncached(72) == 0
# =======================END update_slider tests===========================

# =======================START UPDATE WAS FIGURE TESTS===========================
def test_update_was_figure():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "was-apply.n_clicks"},{"prop_id": "was-date-picker.date"},{"prop_id": "was-slider-graph.value"},{"prop_id": "was-dropdown.value"},{"prop_id": "variable-dropdown-forecast.value"},{"prop_id": "was-previous.data"},{"prop_id": "view-style.active"},{"prop_id": "was-map.zoom"},{"prop_id": "was-map.center"}]}))
        return code.update_was_figure(1, '20230404', 1, 'burkinafaso', None, [True, False, False, False], [True], [], []) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert "url='https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png'), FullscreenControl(position='topright'), None, None, None, 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/20230404/20230404_SCONC_DUST_1.geojson'), Div(children=[Div(children=[Span(children='', className='was-legend-point', style={'backgroundColor': 'green'}), Span(children='Normal', className='was-legend-label')], style={'display': 'block'}), Div(children=[Span(children='', className='was-legend-point', style={'backgroundColor': 'gold'}), Span(children='High', className='was-legend-label')], style={'display': 'block'}), Div(children=[Span(children='', className='was-legend-point', style={'backgroundColor': 'darkorange'}), Span(children='Very High', className='was-legend-label')], style={'display': 'block'}), Div(children=[Span(children='', className='was-legend-point', style={'backgroundColor': 'red'}), Span(children='Extremely High', className='was-legend-label')], style={'display': 'block'})], className='was-legend'), Div(children=P(B(['Barcelona Dust Regional Center - Burkina Faso WAS.', Br(None), 'Expected concentration of airborne dust.', Br(None), 'Issued: 04 Apr 2023. Valid: 05 Apr 2023'])), id='was-info', className='info', style={'position': 'absolute', 'top': '10px', 'left': '10px', 'zIndex': '1000', 'fontFamily': " in str(output)

def test_update_was_figure_zooms():
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "was-apply.n_clicks"},{"prop_id": "was-date-picker.date"},{"prop_id": "was-slider-graph.value"},{"prop_id": "was-dropdown.value"},{"prop_id": "variable-dropdown-forecast.value"},{"prop_id": "was-previous.data"},{"prop_id": "view-style.active"},{"prop_id": "was-map.zoom"},{"prop_id": "was-map.center"}]}))
        return code.update_was_figure(2, '20230404', 1, 'cabo_verde', 'burkinafaso', [True, False, False, False],[True], [8], [[11.982397942974229, -2.6312255859375004]]) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    assert "url='https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png'), FullscreenControl(position='topright'), None, None, None, 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/cabo_verde/geojson/20230404/20230404_SCONC_DUST_1.geojson'), Div(children=[Div(children=[Span(children='', className='was-legend-point', style={'backgroundColor': 'green'}), Span(children='Normal', className='was-legend-label')], style={'display': 'block'}), Div(children=[Span(children='', className='was-legend-point', style={'backgroundColor': 'gold'}), Span(children='High', className='was-legend-label')], style={'display': 'block'}), Div(children=[Span(children='', className='was-legend-point', style={'backgroundColor': 'darkorange'}), Span(children='Very High', className='was-legend-label')], style={'display': 'block'}), Div(children=[Span(children='', className='was-legend-point', style={'backgroundColor': 'red'}), Span(children='Extremely High', className='was-legend-label')], style={'display': 'block'})], className='was-legend'), Div(children=P(B(['Barcelona Dust Regional Center - Cape Verde WAS.', Br(None), 'Expected concentration of airborne dust.', Br(None), 'Issued: 04 Apr 2023. Valid: 05 Apr 2023'])), id='was-info', className='info', style={'position': 'absolute', 'top': '10px', 'left': '10px', 'zIndex': '1000', 'fontFamily':" in str(output)
# =======================END UPDATE WAS FIGURE TESTS===========================
























#some comment
