import dash_bootstrap_components as dbc
from dash import html
from ines_core_map_handler import MAP_LAYERS
from map_handler import DELAY, DELAY_DATE, END_DATE, FORECAST_FINAL_DAY
from datetime import datetime
from datetime import timedelta

def get_forecast_days(curdate=END_DATE, lastday=FORECAST_FINAL_DAY):
    """ Return forecast days according to configuration file """
    delay = DELAY
    st_date = DELAY_DATE
    if (delay and st_date \
            and (datetime.strptime(curdate, "%Y%m%d") >= datetime.strptime(st_date, "%Y%m%d"))) or \
            (not delay and st_date \
            and (datetime.strptime(curdate, "%Y%m%d") < datetime.strptime(st_date, "%Y%m%d"))) or \
            (delay and not st_date):
        st_day = 1
    else:
        st_day = 0

    return dict([
        (
            idx, (datetime.strptime(curdate, "%Y%m%d") + \
            timedelta(days=idx)).strftime("%a %d").upper()
        ) for idx in range(st_day, lastday)
    ])

def layout_view():
    """ Return the menu for the various mapview types"""

    return html.Div([
        html.Span(
            dbc.DropdownMenu(
                id='map-view-dropdown',
                label='VIEW',
                children=[
                    dbc.DropdownMenuItem(
                        MAP_LAYERS[style]['name'],
                        id=dict(
                            tag='view-style',
                            index=style
                            ),
                        active=active
                        )
                    for style, active in zip(list(MAP_LAYERS.keys()),
                                             [True if i == 'carto-positron'
                                              else False for i in MAP_LAYERS])
                    ],
                direction="up",
                in_navbar=True,
                ),
            )
        ],
                           id='map-view-dropdown-div')


def time_series():
    """ Return the timeseries element"""
    return html.Div(
            id='open-timeseries',
            children=[
                dbc.Spinner(
                    id='loading-ts-modal',
                    fullscreen=True,
                    fullscreen_style={'opacity': '0.5', 'zIndex' : '200000'},
                    show_initially=False,
                    # debounce=200,
                    children=[
                        dbc.Modal([],
                                  id='ts-modal',
                                  size='xl',
                                  centered=True,
                                  is_open=False,
                                  ),
                        ],
                    )],
                #style={'display': 'none'},
                )

def layout_layers(*args):
    """ Build the layers dropdown. User inputs strings for the item ids.
        Returns the dropdown element with the ids shown capitalized as items. """
    dropdown = []
    for layer in args:
        dropdown.append(dbc.DropdownMenuItem(layer.upper(), id=layer))
    return html.Div([
        html.Span(
            dbc.DropdownMenu(
                id='map-layers-dropdown',
                label='LAYERS',
                children=dropdown,
                direction="up",
                ),
            )])
