import os
os.environ['POLLINATION_API_URL'] = 'https://api.staging.pollination.cloud'

import streamlit.components.v1 as components
from pollination_streamlit.selectors import get_api_client
from pollination_streamlit_io import (select_account, select_project, select_recipe, 
                                      recipe_inputs_form, study_progress)

from pollination_streamlit_viewer import viewer
import streamlit as st

api_client = get_api_client()

option = st.selectbox('Select Recipe', 
             options=('Direct sun hours', 'Cumulative radiation'))

if option == 'Direct sun hours':
    default_recipe = {'owner': 'ladybug-tools', 'name': 'direct-sun-hours-*', 'tag': '*'}
else:
    default_recipe = {'owner': 'ladybug-tools', 'name': 'cumulative-radiation-*', 'tag': '*'}

external_trigger = st.slider('Slider as trigger', min_value=1, max_value=10)
st.slider('Common slider', min_value=1, max_value=10)

account = select_account('st-account', api_client)
if account:
    project_owner = account.get('username') or account.get('account_name')

    project = select_project('st-project', 
                         api_client, 
                         project_owner=project_owner)
    if project:
        project_name = project.get('name')
        recipe = select_recipe('st-recipe', api_client,
                       project_owner=project_owner,
                       project_name=project_name,
                       default_recipe=default_recipe)

        if 'study' not in st.session_state:
            st.session_state['study'] = None

        if recipe:
            north = st.slider('north', 0, 100, 0)
            st.text(north)

            controlled_values = {
                'wea': {
                    'value': {
                        'pollination_local_path': 'C:/ladybug/ITA_Campobasso.162520_IGDG/ITA_Campobasso.162520_IGDG.wea'
                    }
                },
                'north': {
                    'value': north
                }
            }

            study = recipe_inputs_form(key='st-study', 
                                api_client=api_client, 
                                trigger_inputs=[controlled_values, external_trigger],
                                project_name=project_name,
                                project_owner=project_owner,
                                recipe_filter=recipe,
                                user_inputs=controlled_values,
                                is_local=True)
            if study:
                name, value = study_progress(api_client,
                                        study_info = study,
                                        show_output=True,
                                        show_result=True)
                
                if name:
                    if (name.endswith('vtkjs')):
                        viewer(key='my-test', content=value)