
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)

import streamlit as st

api_client = get_api_client()

account = select_account('st-account', api_client)
project = select_project('st-project', 
                         api_client, 
                         project_owner=account.get('username') or 'ladybug-tools') if account else None
recipe = select_recipe('st-recipe', api_client,
                       project_owner=account['username'] if 'username' in account else account['account_name'],
                       project_name=project.get('name') or 'demo') if project else None

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.get('name'),
                        project_owner=account['username'] if 'username' in account else account['account_name'],
                        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']
        out_run = study_progress(api_client,
                                 project_name = study_info.get('projectName'),
                                 project_owner = study_info.get('accountName'),
                                 run = study_info.get('run'),
                                 is_local = study_info.get('isLocal'),
                                 show_output=True)

