import pytest
import pandas as pd
from datetime import datetime
from datetime import timedelta
import importlib
import numpy as np
from contextvars import copy_context
import dash
from dash._callback_context import context_value
from dash._utils import AttributeDict
code = importlib.import_module('tabs.observations')

from data_handler import START_DATE
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)
EDATE_OBJ_STR = str(EDATE_OBJ)
SDATE_OBJ = datetime.strptime(START_DATE, FMT_ISO)
SDATE_OBJ_STR = str(SDATE_OBJ)

#=================TEST OBS_TIME_SLIDER===========================
def test_obs_time_slider():
    #TEST DIV=OBS
    assert f"display_format='DD MMM YYYY', id='obs-date-picker'), Button(id='clear_button', className='clear_button')], className='timesliderline'), Span(children=[Button(id='btn-obs-play', className='fa fa-play', n_clicks=0, title='Play')], className='timesliderline anim-buttons'), Span(children=Slider(min=0, max=23, step=1" in str(code.obs_time_slider(div='obs', start=0, end=23, step=1, start_date=START_DATE, end_date=END_DATE).children[0])

    #TEST DIV=OBS-VIS
    from utils import get_vis_edate
    from datetime import datetime
    hour = datetime.now().hour
    _, default_tstep = get_vis_edate(END_DATE, hour=hour)
    assert "Button(id='clear_button', className='clear_button')], className='timesliderline'), None, Span(children=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')]" % { 'default_tstep': default_tstep } in str(code.obs_time_slider(div='obs-vis', start=0, end=18, step=6, start_date=START_DATE, end_date=END_DATE))

    #TEST DIV = OBS-AOD
    # assert "NavbarSimple(children=[Div(children=[Span(children=[DatePickerSingle(date='20210318', min_date_allowed=datetime.datetime(2023, 3, 30, 0, 0), max_date_allowed=datetime.datetime(2021, 3, 18, 0, 0), placeholder='DD MON YYYY', initial_visible_month=datetime.datetime(2021, 3, 18, 0, 0), display_format='DD MMM YYYY', id='obs-aod-date-picker'), Button(id='clear_button', className='clear_button')]" % { 'default_tstep': default_tstep }  in str (code.obs_time_slider(div='obs-aod', start=0, end=23, step=1))


#=================TEST SIDEBAR OBSERVATIONS ===========================
def test_sidebar_observations():
    assert "Button(children='EUMETSAT RGB', id='rgb', color='link', style={'fontWeight': 'bold'})" in str(code.sidebar_observations('rgb'))
    assert "Button(children='Visibility', id='visibility', color='link', style={'fontWeight': 'bold'})" in str(code.sidebar_observations('visibility'))


#=================TEST TAB_OBSERVATIONS===========================
def test_tab_observations():
    #TEST RGB    
    assert f"All observations are kindly offered by Partners of the WMO Barcelona Dust Regional Center. RGB is a qualitative satellite product that indicates desert dust in the entire atmospheric column (represented by pink colour).']), className='description-body'), Div(children=[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')" in str(code.tab_observations('rgb', start_date=START_DATE, end_date=END_DATE))

    #TEST VISIBILITY
    from utils import get_vis_edate
    from datetime import datetime
    hour = datetime.now().hour
    _, default_tstep = get_vis_edate(END_DATE, hour=hour)
    assert "Button(id='clear_button', className='clear_button')], className='timesliderline'), None, Span(children=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(code.tab_observations('visibility', start_date=START_DATE, end_date=END_DATE))
