"""
massdash/gui
~~~~~~~~~~~~
"""

import os
import click
import logging
import streamlit as st
from streamlit_javascript import st_javascript
from PIL import Image
import platform

# Constants
from massdash.constants import MASSDASH_ICON, MASSDASH_LOGO_LIGHT, MASSDASH_LOGO_DARK, OPENMS_LOGO
from massdash.constants import URL_TEST_SQMASS, URL_TEST_OSW, URL_TEST_PQP, URL_TEST_RAW_MZML, URL_TEST_DREAMDIA_REPORT, URL_TEST_RAW_MZML_IM, URL_TEST_OSW_IM
# Server
from massdash.server.ExtractedIonChromatogramAnalysisServer import ExtractedIonChromatogramAnalysisServer
from massdash.server.RawTargetedExtractionAnalysisServer import RawTargetedExtractionAnalysisServer
from massdash.server.SearchResultsAnalysisServer import SearchResultsAnalysisServer
# UI 
from massdash.ui.MassDashGUI import MassDashGUI
# Utils
from massdash.util import LOGGER, get_download_folder, download_file, reset_app, open_page, close_app


# Current streamlit version only supports bokeh 2.4.3
# See work around: https://github.com/streamlit/streamlit/issues/5858#issuecomment-1482042533
# Support for Bokeh is currently on Streamlit's roadmap: https://roadmap.streamlit.app/
def use_file_for_bokeh(chart, chart_height=1200):
    import streamlit.components.v1 as components

    from bokeh.plotting import save
    from bokeh.io import output_file
    output_file('bokeh_graph.html')
    save(chart)
    with open("bokeh_graph.html", 'r', encoding='utf-8') as f:
        html = f.read()
    components.html(html, height=chart_height, scrolling=True)
# Update the bokeh_chart method to use the file workaround
st.bokeh_chart = use_file_for_bokeh

@click.command()
# @click.argument('args', default='args', type=str)
@click.option('--verbose', '-v', is_flag=True, help="Enables verbose mode.")
@click.option('--perf', '-t', is_flag=True, help="Enables measuring and tracking of performance.")
@click.option('--perf_output', '-o', default='MassDash_Performance_Report.txt', type=str, help="Name of the performance report file to writeout to.")
@click.option('--cloud', '-c', is_flag=True, help="Set to True to emulate running on streamlit cloud, if False detect if running on streamlit cloud.")
def main(verbose, perf, perf_output, cloud):     
    
    ###########################
    ## Logging
    LOGGER.name = 'MassDashGUIMain'
    if verbose:
        log_level = logging.DEBUG
    else:
        log_level = logging.INFO
    LOGGER.setLevel(log_level) 
    
    # Confit
    st.set_page_config(page_title='MassDash', page_icon=MASSDASH_ICON, layout='wide')

    ###########################
    ## Main Container Window

    st.session_state.WELCOME_PAGE_STATE = True

    # determine if should run in a streamlit cloud context
    isStreamlitCloud = cloud or platform.processor() == "" # will be "" if running on streamlit cloud

    massdash_gui = MassDashGUI(verbose=verbose, perf=perf, perf_output=perf_output, isStreamlitCloud=isStreamlitCloud)
    if st.session_state.WELCOME_PAGE_STATE:
        massdash_gui.show_welcome_message()
    
    ###########################
    ## Sidebar Window

    # MassDash Sidebar Top Logo
    st_theme = st_javascript("""window.getComputedStyle(window.parent.document.getElementsByClassName("stApp")[0]).getPropertyValue("color-scheme")""")
    st.sidebar.image(MASSDASH_LOGO_LIGHT if st_theme == "dark" else MASSDASH_LOGO_DARK)
    cols = st.sidebar.columns(3)
    home_button = cols[0].button("🏠 Home", key="home_button", help="Return to the home page.", on_click=reset_app, use_container_width=True)
    MASSDASH_DOC_URL = "https://massdash.readthedocs.io/en/latest/index.html"
    documentation_button = cols[1].button("📖 Doc", key="documentation_button", help="Open the MassDash documentation in a new tab.", use_container_width=True, on_click=open_page, args=(MASSDASH_DOC_URL,))
    MASSDASH_GITHUB_URL = "https://github.com/Roestlab/massdash"
    github_button = cols[2].button("🐙 GitHub", key="github_button", help="Open the MassDash GitHub repository in a new tab.", use_container_width=True, on_click=open_page, args=(MASSDASH_GITHUB_URL,))
    try:
        stop_button = st.sidebar.button("🛑 Exit", key="stop_button", help="Stop the MassDash app.", on_click=close_app, use_container_width=True)
    except Exception:
        pass
        
    st.sidebar.divider()

    if st.session_state.workflow == "xic_data" and st.session_state.clicked['load_toy_dataset_xic_data']:
        tmp_download_folder = get_download_folder() + "/massdash_example_dataset/"
        sqmass_file_path_input = tmp_download_folder + "/test_raw_1.sqMass"
        download_file(URL_TEST_SQMASS, tmp_download_folder)
        osw_file_path = tmp_download_folder + "/test.osw"
        download_file(URL_TEST_OSW, tmp_download_folder)       
        
        massdash_gui.show_file_input_settings(osw_file_path, sqmass_file_path_input)
        
        # Remove welcome message container if dataset is loaded
        massdash_gui.welcome_container.empty()

        st.session_state.WELCOME_PAGE_STATE = False
        
    elif st.session_state.workflow == "raw_data" and st.session_state.clicked['load_toy_dataset_raw_data']:
        tmp_download_folder = get_download_folder() + "/massdash_example_dataset/"
        transition_list_file_path = tmp_download_folder + "/test.pqp"
        download_file(URL_TEST_PQP, tmp_download_folder)
        raw_file_path_input = tmp_download_folder +  "/test_raw_1.mzML"
        download_file(URL_TEST_RAW_MZML, tmp_download_folder)
        diann_report_file_path_input = tmp_download_folder + "/test.osw"
        download_file(URL_TEST_OSW, tmp_download_folder)
        feature_file_type = "OpenSWATH"
        massdash_gui.show_file_input_settings(diann_report_file_path_input, raw_file_path_input, transition_list_file_path, feature_file_type)
        
        # Remove welcome message container if dataset is loaded
        massdash_gui.welcome_container.empty()

        st.session_state.WELCOME_PAGE_STATE = False
    
    elif st.session_state.workflow == 'raw_data' and st.session_state.clicked['load_toy_dataset_raw_data_im']:
        tmp_download_folder = get_download_folder() + "/massdash_example_dataset/"
        transition_list_file_path = tmp_download_folder + "/ionMobilityTest.osw"
        raw_file_path_input = tmp_download_folder +  "/ionMobilityTest.mzML"
        download_file(URL_TEST_RAW_MZML_IM, tmp_download_folder)
        osw_report_file_path_input = tmp_download_folder + "/ionMobilityTest.osw"
        download_file(URL_TEST_OSW_IM, tmp_download_folder)
        feature_file_type = "OpenSWATH"
        massdash_gui.show_file_input_settings(osw_report_file_path_input, raw_file_path_input, transition_list_file_path, feature_file_type)

        # Remove welcome message container if dataset is loaded
        massdash_gui.welcome_container.empty()

        st.session_state.WELCOME_PAGE_STATE = False
  
    if st.session_state.workflow == "search_results_analysis" and st.session_state.clicked['load_toy_dataset_search_results_analysis']:
        tmp_download_folder = get_download_folder() + "/massdash_example_dataset/"
        feature_file_path = tmp_download_folder + "/test.osw"
        download_file(URL_TEST_OSW, tmp_download_folder)
        feature_file_type = "OpenSWATH"
        
        feature_file_path_3 = tmp_download_folder + "/test_dreamdia_report.tsv"
        download_file(URL_TEST_DREAMDIA_REPORT, tmp_download_folder)
        feature_file_type_3 = "DreamDIA"
        
        search_results_entries_dict = {'entry_1': {'search_results_file_path':          feature_file_path, 'search_results_exp_name': 'OSW', 'search_results_file_type': feature_file_type}, 
                                        'entry_3': {'search_results_file_path': feature_file_path_3, 'search_results_exp_name': 'DreamDIA', 'search_results_file_type': feature_file_type_3}}
        
        massdash_gui.show_file_input_settings(feature_file_entries_dict=search_results_entries_dict)
        
        # Remove welcome message container if dataset is loaded
        massdash_gui.welcome_container.empty()

        st.session_state.WELCOME_PAGE_STATE = False

    if st.session_state.workflow == "xic_data" and massdash_gui.osw_file_path!="*.osw" and massdash_gui.sqmass_file_path_input!="*.sqMass" and not st.session_state.clicked['load_toy_dataset_xic_data']:
        massdash_gui.show_file_input_settings(massdash_gui.osw_file_path, massdash_gui.sqmass_file_path_input)
        # Remove welcome message container if dataset is loaded
        massdash_gui.welcome_container.empty()
        st.session_state.WELCOME_PAGE_STATE = False

    if st.session_state.workflow == "raw_data" and massdash_gui.transition_list_file_path!="*.pqp" and massdash_gui.raw_file_path_input!="*.mzML" and not st.session_state.clicked['load_toy_dataset_raw_data'] and not st.session_state.clicked['load_toy_dataset_raw_data_im']:
        massdash_gui.show_file_input_settings(massdash_gui.feature_file_path, massdash_gui.raw_file_path_input, massdash_gui.transition_list_file_path, massdash_gui.feature_file_type)
        # Remove welcome message container if dataset is loaded
        massdash_gui.welcome_container.empty()
        st.session_state.WELCOME_PAGE_STATE = False
        
    if st.session_state.workflow == "search_results_analysis" and massdash_gui.feature_file_entries is not None and not st.session_state.clicked['load_toy_dataset_search_results_analysis']:
        massdash_gui.show_file_input_settings(feature_file_entries_dict=massdash_gui.feature_file_entries)
        # Remove welcome message container if dataset is loaded
        massdash_gui.welcome_container.empty()
        st.session_state.WELCOME_PAGE_STATE = False
        
    if st.session_state.workflow == "xic_data" and not st.session_state.WELCOME_PAGE_STATE and massdash_gui.file_input_settings is not None:
        show_xic_exp = ExtractedIonChromatogramAnalysisServer(massdash_gui)
        show_xic_exp.main()
        
    if st.session_state.workflow == "raw_data" and not st.session_state.WELCOME_PAGE_STATE:
        if st.session_state.clicked['load_toy_dataset_raw_data']:
            is_toy_dataset = True
        else:
            is_toy_dataset = False
        show_raw_exp = RawTargetedExtractionAnalysisServer(massdash_gui)
        show_raw_exp.is_toy_dataset = is_toy_dataset
        show_raw_exp.main()
        
    if st.session_state.workflow == "search_results_analysis" and not st.session_state.WELCOME_PAGE_STATE:
        show_search_results_analysis = SearchResultsAnalysisServer(massdash_gui)
        show_search_results_analysis.main()

    # OpenMS Siderbar Bottom Logo
    st.sidebar.image(OPENMS_LOGO)
    
if __name__ == "__main__":
    main()
