
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

env = st.selectbox('Select ENV', options=('Prod', 'Staging'), 
                   index=0)

if env == 'Prod':
    api_client = get_api_client()
else:
    # Use it with staging
    api_client = {
        '_api_token': st.text_input('API key', type='password'),
        '_host': st.text_input('API URL'),
    }

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)

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

        def set_study():
            st.session_state['study'] = st.session_state['st-study']

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

            recipe_inputs_form(key='st-study', 
                                api_client=api_client, 
                                project_name=project_name,
                                project_owner=project_owner,
                                recipe_filter=recipe,
                                controlled_values={
                                    'wea': {
                                        'value': {
                                            'pollination_local_path': 'C:/ladybug/ITA_Campobasso.162520_IGDG/ITA_Campobasso.162520_IGDG.wea'
                                        }
                                    },
                                    'north': {
                                        'value': north
                                    }
                                }, on_change=set_study)
            if st.session_state['study']:
                study_info = st.session_state['study']
                name, value = study_progress(api_client,
                                        study_info = study_info,
                                        show_output=True)
                
                if name:
                    if (name.endswith('vtkjs')):
                        viewer(key='my-test', content=value)