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

from data_handler import START_DATE, END_DATE
from data_handler import FREQ

#============ TEST render_evaluation_tab=================================
def test_render_observations_tab():
    # CASE 1, RGB 1 CLICK, VISIBILITY NO CLICK
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "rgb.n_clicks"}, {"prop_id": "visibility.n_clicks"}]}))
        return code.render_observations_tab(1, 0) 

    ctx = copy_context()
    output = ctx.run(run_callback)
    print("*********************")
    print(output[0])
    print("---------------------")
    print(output[1:])
    print("*********************")
    assert f"Button(children='HEMISPHERIC', id='btn-fulldisc', active=True), Button(children='MIDDLE EAST', id='btn-middleeast', active=False)], id='rgb-buttons'), Div(children=[Img(id='rgb-image', alt='EUMETSAT RGB - NOT AVAILABLE', src='./assets/eumetsat/FullDiscHD/archive/{END_DATE}/FRAME_OIS_RGB-dust-all_{END_DATE}0000.gif'), Div(children=NavbarSimple(children=[Div(children=[Span(children=[DatePickerSingle(date='{END_DATE}'" in str(output[0]) 
    assert output[1:] == ({ 'font-weight': 'bold' }, { 'font-weight': 'normal' }, 'rgb')


def test_render_observations_tab_visibility():
    # CASE 2, RGB NO CLICK, VISIBILITY 1 CLICK
    def run_callback():
        context_value.set(AttributeDict(**{"triggered_inputs": [{"prop_id": "visibility.n_clicks"}]}))
        return code.render_observations_tab(0, 1) 

    from utils import get_vis_edate
    from datetime import datetime
    hour = datetime.now().hour
    _, default_tstep = get_vis_edate(END_DATE, hour=hour)
    ctx = copy_context()
    output = ctx.run(run_callback)
    assert "Slider(min=0, max=18, step=6, marks={0: {'label': '00-06'}, 6: {'label': '06-12'}, 12: {'label': '12-18'}, 18: {'label': '18-24', 'style': {'left': '', 'right': '-32px'}}}, value=%(default_tstep)s, id='obs-vis-slider-graph'), className='timesliderline')], className='timeslider')], id='rgb-navbar', className='fixed-bottom navbar-timebar', dark=True, expand='lg', fixed='bottom', fluid=True), Br(None), Br(None), Div(children=[Span(children=P('Dust data ©2023 WMO Barcelona Dust Regional Center.'), id='forecast-disclaimer')], className='disclaimer')], className='layout-dropdown rgb-layout-dropdown')], id='observations-tab', className='horizontal-menu', label='Observations', value='observations-tab')" % { 'default_tstep': default_tstep } in str(output[0]) 
    assert output[1:] == ({ 'font-weight': 'normal' }, { 'font-weight': 'bold' }, 'visibility')

# def update_image_src(btn_fulldisc, btn_middleeast, date, tstep, btn_fulldisc_active, btn_middleeast_active):
# def start_stop_obs_autoslider(n_play, disabled, value):
# def update_obs_slider(n):

def test_update_vis_figure():
    run = code.update_vis_figure.uncached(END_DATE, 1, [5], [45,45])
    assert "Map(children=[TileLayer(id={'tag': 'obs-vis-tile-layer', 'index': 'None'}" in str(run)

    assert "id='vis-info', className='info', style={'position': 'absolute', 'top': '10px', 'left': '10px', 'zIndex': '1000', 'fontFamily'" in str(run)
                                                    
def test_update_vis_figure_date_none():
    run = code.update_vis_figure.uncached(None, 1, [5], [45,45])
    assert "id={'tag': 'obs-vis-map', 'index': 'None'}, animate=False, center=45, inertia=True, minZoom=2, preferCanvas=True, wheelDebounceTime=80, wheelPxPerZoomLevel=120, zoom=5, zoomSnap=0.1)" in str(run)


                                                
