import streamlit as st
import json
from enum import Enum

config_json = {
    "Timelines": {},
    "Blocks": {}
}


class HudWidgetPosition(Enum):
    Hidden = "Hidden"
    StatusBarLeft = "Status bar (left)"
    StatusBarCenter = "Status bar (center)"
    StatusBarRight = "Status bar (right)"
    InformationPanel = "Information panel"


class EnumProgressElementStyle(Enum):
    Continuous = "Continuous"
    # ...


class ShowAccuracyCheckFeedback(Enum):
    Always = "Always"
    Never = "Never"


class ShowCorrectTrialResponseFeedback(Enum):
    Always = "Always"
    OnError = "Only on error"
    Never = "Never"


class StimulusType(Enum):
    Digits = "Digits"
    LowerCaseLetters = "Lowercase letters"
    UpperCaseLetters = "Uppercase letters"
    Symbols = "Symbols"


class DigitSpanBlock:
    """
    UI to generate timeline config file for digit span.
    """

    def render(self):
        st.title("DigitSpan - Block Generator")
        # st.help(DigitSpanBlock)

        self.Name = st.sidebar.text_input('Block Name')

        st.sidebar.header("Stimulus")
        self.StimulusType = self.render_enum("Stimulus Type", StimulusType)
        self.StimulusPackSize = st.sidebar.number_input("Stimulus pack size", min_value=1, max_value=1)
        self.SequenceLength = st.sidebar.number_input("Sequence Length", min_value=1)

        st.sidebar.header("Timing")
        self.StimulusDisplayDuration = st.sidebar.number_input("Stimulus Display Duration")
        self.InterstimulusInterval = st.sidebar.number_input("Interstimulus Interval (sec)", min_value=0)
        self.RetentionDelay = st.sidebar.number_input("Retention Delay (sec)", min_value=0)
        self.MaxIdleTime = st.sidebar.number_input("Max idle time (sec)",
                                                   min_value=self.StimulusDisplayDuration + self.InterstimulusInterval)

        st.sidebar.header("UI")
        self.BlockCounterPosition = self.render_enum("Block Counter", HudWidgetPosition)
        self.ShowAccuracyCheckFeedback = self.render_enum("Show accuracy feedback", ShowAccuracyCheckFeedback)
        self.ShowCorrectTrialResponseFeedback = self.render_enum("Show correct response",
                                                                 ShowCorrectTrialResponseFeedback)

        # show output in json format
        __json = {}
        __json.update(vars(self))
        del __json['Name']
        config_json["Blocks"] = {self.Name: __json}
        st.json(config_json)

    @staticmethod
    def render_enum(text, cls):
        options = list(map(lambda e: e.name, cls))
        widget = st.sidebar.selectbox(
            text,
            options,
            format_func=lambda k: cls[k].value)
        return widget


if __name__ == "__main__":
    DigitSpanBlock().render()
