import pytest
import importlib
code = importlib.import_module('router')
from data_handler import ROUTE_DEFAULTS

#============ TEST get_input_aliases=================================
def test_get_input_aliases():
    input = {'tab': ['forecast'], 'var': ['aod'], 'model': ['median', 'monarch', 'cams'], 'for_option': ['models'], 'eval_option': ['nrt'], 'obs_option': ['rgb'], 'date': ['20230110']}
    assert code.get_input_aliases(input) == {'tab': ['forecast-tab'], 'var': ['OD550_DUST'], 'model': ['median', 'monarch', 'cams'], 'for_option': ['models'], 'eval_option': ['nrt'], 'obs_option': ['rgb'], 'date': ['20230110']}

    route_selections = {'model': ['multi-model']}
    result = code.get_input_aliases(route_selections)
    assert result['model'] == ['median']

    route_selections = {'var': ['aod']}
    result = code.get_input_aliases(route_selections)
    assert result['var'] == ['OD550_DUST']

#============ TEST eval_section_query=================================
def test_eval_section_query():
    input = {'tab': ['forecast-tab'], 'var': ['aod'], 'section': ['was'], 'model': ['median', 'monarch', 'cams'], 'for_option': ['models'], 'eval_option': ['nrt'], 'obs_option': ['rgb'], 'date': ['20230110']}
    assert code.eval_section_query(input) == {'tab': ['forecast-tab'], 'var': ['aod'], 'section': ['was'], 'model': ['median', 'monarch', 'cams'], 'for_option': ['was'], 'eval_option': ['nrt'], 'obs_option': ['rgb'], 'date': ['20230110']}

    queries = {'tab': ['forecast-tab'], 'section': ['models']}
    result = code.eval_section_query(queries)
    assert result['for_option'] == ['models']

    queries = {'tab': ['evaluation-tab'], 'section': ['visibility']}
    result = code.eval_section_query(queries)
    assert result['eval_option'] ==  ['visibility']

#============ TEST get_url_queries=================================
def test_get_url_queries():
    url = '/dashboard/?var=aod&model=cams&date=20230110'
    assert code.get_url_queries(url, ROUTE_DEFAULTS) == {'date': ['20230110'], 'eval_option': ['nrt'], 'for_option': ['models'], 'model': ['cams'], 'obs_option': ['rgb'], 'tab': ['forecast-tab'], 'var': ['OD550_DUST'], 'country':['burkinafaso'], 'download': [None]}

    url = "http://localhost/?tab=forecast&section=models&model=multi-model&var=concentration&country=chad"
    result = code.get_url_queries(url)
    expected = {'tab': ['forecast-tab'],
                'var': ['SCONC_DUST'],
                'model': ['median'],
                'for_option': ['models'],
                'eval_option': ['nrt'],
                'obs_option': ['rgb'],
                'country': ['chad'],
                'download': [None],
                'date': ['20220831'],
                'section': ['models']
                }
    assert result == expected

#============ TEST render_sidebar=================================
def test_render_sidebar():
    #first test forecast tab
    assert  "className='collapsible-cards')], className='sidebar-bottom')]" in str(code.render_sidebar('forecast-tab'))
    #test evaluation 
    assert  "Div(children=[Button(children='Statistics', id='scores-evaluation', color='link'" in str(code.render_sidebar('evaluation-tab')) 
    #test observations
    assert  "id='observations-variable', className='sidebar-first-item')" in str(code.render_sidebar('observations-tab')) 
#============ TEST router=================================
def test_router():
    #verify forecast with selections returns correctly
    url ='http://bscesdust02.bsc.es:7778/dashboard/?var=aod&model=median&model=monarch&model=cams&date=20230110' 
    assert "id='app-tabs', value='forecast-tab')]" in str(code.router(url)) 

    #verify evaluation with section 
    url ='http://bscesdust02.bsc.es:7778/dashboard/?tab=evaluation&section=statistics' 
    assert "id='app-tabs', value='evaluation-tab')]" in str(code.router(url))

    #verify observations with section
    url ='http://bscesdust02.bsc.es:7778/dashboard/?tab=observations&section=visibility' 
    assert "id='app-tabs', value='observations-tab')]" in str(code.router(url)) 

    #verify incorrect url lands on default
    url ="children=[H2(children='404 Error', id='error_title"
    assert "id='app-tabs', value='forecast-tab')]" in str(code.router(url)) 
