import pytest
import importlib
code = importlib.import_module('data_handler')
from data_handler import START_DATE
from data_handler import END_DATE
from data_handler import OBS

@pytest.fixture
def aeronet_instance():
    return code.Observations1dHandler('20220404','20220405' , 'aeronet') 

def test_generate_obs1d_tstep_trace1(aeronet_instance):
    run = aeronet_instance.generate_obs1d_tstep_trace('OD550_DUST')
    assert str(type(run[1])) == "<class 'dash_leaflet.GeoJSON.GeoJSON'>"
    assert run[0].shape ==(120, 3)

# @pytest.fixture 
# def obsTSHandler_instance():
#     return code.ObsTimeSeriesHandler('aeronet','20220404','20220405', 'OD550_DUST', ['median', 'monarch']) 
#
# def test_retrieve_timeseries():
#     assert obsTSHandler_instance.retrieve_timeseries(idx, st_name, model) == 'hello'

    
# =================== TIME SERIES HANDLER ============================
@pytest.fixture
def TSHandler():
    return code.TimeSeriesHandler('median', '20220606', 'OD550_DUST')

def test_TimeSeriesHandler(TSHandler):
    run = TSHandler.retrieve_single_point(1, 45, 45, model='median')
    assert float(run) == 0.10633552074432373

def test_TimeSeriesHandler_1(TSHandler):
    run = TSHandler.retrieve_single_point(3, 5, 5, model=None)
    assert float(run) == 0.00830854382365942

def test_retrieve_timeseries(TSHandler):
    run = TSHandler.retrieve_timeseries(5, 5, model=None, method='netcdf', forecast=False)
    assert run.layout.title.text =='Dust Optical Depth @ lat = 5 and lon = 5'

def test_retrieve_timeseries_1(TSHandler):
    run = TSHandler.retrieve_timeseries(35, 15, model='monarch', method='netcdf', forecast=True)
    assert run.layout.title.text =='Dust Optical Depth @ lat = 35 and lon = 15'

def test_retrieve_timeseries_2(TSHandler):
    run = TSHandler.retrieve_timeseries(35, 15, model='monarch', method='netcdf', forecast=True)
    assert run.data[0].name == 'MULTI-MODEL (35.25, 15.25)'

# =================== FIGURE HANDLER ============================
@pytest.fixture
def FigureHandler():
    return code.FigureHandler('median', '20220606')

def test_FH_get_center(FigureHandler):
    assert FigureHandler.get_center([35,45]) == [35, 45]
    assert FigureHandler.get_center([-35,-45]) == [-35, -45]
    assert FigureHandler.get_center() == [43.25, 16.5]


def test_set_data_AOD(FigureHandler):
    assert len(FigureHandler.set_data('OD550_DUST', tstep=0)[0]) == 22620
    assert FigureHandler.set_data('OD550_DUST', tstep=0)[0][0] == -26.75

    assert len(FigureHandler.set_data('OD550_DUST', tstep=3)[0]) == 22620
    assert FigureHandler.set_data('OD550_DUST', tstep=3)[0][0] == -26.75

def test_set_data_SCONC(FigureHandler):
    assert len(FigureHandler.set_data('SCONC_DUST', tstep=0)[0]) == 22620
    assert FigureHandler.set_data('SCONC_DUST', tstep=0)[0][0] == -26.75


def test_retrieve_cdatetime(FigureHandler):
    assert str(FigureHandler.retrieve_cdatetime(tstep=0)) == '2022-06-06 12:00:00'
    assert str(FigureHandler.retrieve_cdatetime(tstep=6)) == '2022-06-07 06:00:00'
    assert str(FigureHandler.retrieve_cdatetime(tstep=12)) == '2022-06-08 00:00:00'

def test_generate_contour_tstep_trace_leaflet(FigureHandler):
    result = "GeoJSON(hideout={'colorscale': ['rgba(255,255,255,0.4)', '#a1ede3', '#5ce3ba', '#fcd775', '#da7230', '#9e6226', '#714921', '#392511', '#1d1309'], 'bounds': array([ 0. ,  0.1,  0.2,  0.4,  0.8,  1.2,  1.6,  3.2,  6.4, 10. ]"
    assert result in str(FigureHandler.generate_contour_tstep_trace_leaflet('OD550_DUST', tstep=0)[0])
    result2 = "Colorbar(classes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], colorscale=['rgba(255,255,255,0.4)', '#a1ede3', '#5ce3ba', '#fcd775', '#da7230', '#9e6226', '#714921', '#392511', '#1d1309']"
    assert result2 in str(FigureHandler.generate_contour_tstep_trace_leaflet('OD550_DUST', tstep=0)[1])

def test_generate_contour_tstep_trace_AOD(FigureHandler):
    assert FigureHandler.generate_contour_tstep_trace('OD550_DUST', tstep=0)['name'] == 'Dust Optical Depth_contours'
    assert FigureHandler.generate_contour_tstep_trace('OD550_DUST', tstep=0)['showlegend'] == False

def test_generate_contour_tstep_trace_SCONC(FigureHandler):
    assert FigureHandler.generate_contour_tstep_trace('SCONC_DUST', tstep=0)['name'] == 'Dust Surface Conc. (µg/m³)_contours'
    assert FigureHandler.generate_contour_tstep_trace('SCONC_DUST', tstep=0)['showlegend'] == False

def test_generate_var_tstep_trace_leaflet_AOD(FigureHandler):
    assert str(FigureHandler.generate_var_tstep_trace_leaflet(varname='OD550_DUST', tstep=0))[0:200] == "GeoJSON(data={'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-26.75, 0.25]}, 'properties': {'value': 0.0, 'tooltip': 'Lat 0.25 Lon -26.75 V"

def test_generate_var_tstep_trace_leaflet_SCONC(FigureHandler):
    assert str(FigureHandler.generate_var_tstep_trace_leaflet(varname='SCONC_DUST', tstep=0))[0:200] == "GeoJSON(data={'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-26.75, 0.25]}, 'properties': {'value': 0.009999999776482582, 'tooltip': 'Lat " \


def test_generate_var_tstep_trace_AOD(FigureHandler):
    assert FigureHandler.generate_var_tstep_trace(varname='OD550_DUST', tstep=0)['name'] == "MULTI-MODEL" 
    assert len(FigureHandler.generate_var_tstep_trace(varname='OD550_DUST', tstep=6)['lat']) == 22620


def test_generate_var_tstep_trace_SCONC(FigureHandler):
    assert FigureHandler.generate_var_tstep_trace(varname='SCONC_DUST', tstep=6)['name'] == "MULTI-MODEL" 
    assert len(FigureHandler.generate_var_tstep_trace(varname='SCONC_DUST', tstep=0)['lat']) == 22620


def test_get_title(FigureHandler):
    assert FigureHandler.get_title('OD550_DUST', tstep=0) == 'MULTI-MODEL Dust Optical Depth (550nm)<br>Valid: 12h 06 Jun 2022 (H+00)'
    assert FigureHandler.get_title('SCONC_DUST', tstep=9) == 'MULTI-MODEL Dust Surface Conc. (µg/m³)<br>Valid: 15h 07 Jun 2022 (H+27)'


def test_hour_to_step(FigureHandler):
    assert FigureHandler.hour_to_step(0) == 0
    assert FigureHandler.hour_to_step(1) == 0
    assert FigureHandler.hour_to_step(5) == 0
    assert FigureHandler.hour_to_step(36) == 8
    assert FigureHandler.hour_to_step(54) == 14
    assert FigureHandler.hour_to_step(70) == 0

def test_retrieve_var_tstep(FigureHandler):
    assert FigureHandler.retrieve_var_tstep(varname='OD550_DUST', tstep=0, hour=None, static=True, aspect=(1,1), center=None, selected_tiles='carto-positron', zoom=None, layer=None, tag='empty').children[0].id == {'tag': 'model-tile-layer', 'index': 'median'}
    assert FigureHandler.retrieve_var_tstep(varname='OD550_DUST', tstep=0, hour=None, static=True, aspect=(1,1), center=None, selected_tiles='carto-positron', zoom=None, layer=None, tag='empty').children[3].classes == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    assert str(FigureHandler.retrieve_var_tstep(varname='OD550_DUST', tstep=0, hour=None, static=True, aspect=(1,1), center=None, selected_tiles='carto-positron', zoom=None, layer=None, tag='empty').children[4].children) == "P(B(['MULTI-MODEL Dust Optical Depth (550nm)', Br(None), 'Valid: 12h 06 Jun 2022 (H+00)']))"

# =================== Scores Figure Handler ============================
@pytest.fixture
def ScoresFigureHandler():
    return code.ScoresFigureHandler('aeronet', 'bias', '202206')


def test_get_mapbox(ScoresFigureHandler):
    assert ScoresFigureHandler.get_mapbox('carto-positron', False, 2.8, None)['bearing'] == 0
    assert ScoresFigureHandler.get_mapbox('carto-positron', False, 2.8, None)['pitch'] == 0
    assert ScoresFigureHandler.get_mapbox('carto-positron', False, 2.8, None)['uirevision'] == True
    assert ScoresFigureHandler.get_mapbox('carto-positron', False, 2.8, None)['zoom'] == 2.8
    assert str(ScoresFigureHandler.get_mapbox('carto-positron', False, 2.8, None)['center']) == "layout.mapbox.Center({\n    'lat': 33.5, 'lon': 16.5\n})"


def test_generate_trace(ScoresFigureHandler):
    stats = ['Lille', 'Mainz', 'Palaiseau', 'Paris', 'Kyiv']
    vals = [-0.11, -0.12, -0.07, -0.09, -0.14]
    assert ScoresFigureHandler.generate_trace(35, 35, stats, vals)['name'] == 'MBE score'
    assert ScoresFigureHandler.generate_trace(35, 35, stats, vals)['marker']['cmin'] == -0.1
    assert ScoresFigureHandler.generate_trace(35, 35, stats, vals)['marker']['cmax'] == 0.1
    assert ScoresFigureHandler.generate_trace(35, 35, stats, vals)['marker']['colorbar']['x'] == 0.94

def test_retrieve_scores(ScoresFigureHandler):
    assert ScoresFigureHandler.retrieve_scores('median', aspect=(1,1), center=None).data[1].name == 'MBE score'  
    assert ScoresFigureHandler.retrieve_scores('median', aspect=(1,1), center=None).data[1].type == 'scattermapbox'  


# =================== Vis Handler ============================
@pytest.fixture
def VisFigureHandler():
    return code.VisFigureHandler('20220808')

def test_set_data(VisFigureHandler):
    assert VisFigureHandler.set_data(0)[5][1][0] == [11]
    assert VisFigureHandler.set_data(0)[5][3][0][1] == 1
    assert VisFigureHandler.set_data(24) == ([], [], [], [], [], ())
    assert VisFigureHandler.set_data(36) == ([], [], [], [], [], ())


