import gradio as gr
from core import Core
import os

def start_assistant(phone_number, order_items, delivery_address, payment_method):
    global assistant
    assistant.set_order_settings(
        phone_number, 
        order_items, 
        delivery_address,
        payment_method
    )
    assistant.assistant_start()


def stop_assistant():
    assistant.assistant_stop()


def set_advanced_settings(speech_recog_timeout, window_size_sec, vad_threshold, min_silence_duration_ms, speech_pad_ms):
    assistant.set_speech_recog_settings(
        speech_recog_timeout, 
        window_size_sec, 
        vad_threshold, 
        min_silence_duration_ms, 
        speech_pad_ms
    )

def set_voice_wav(speaker_wav):
    speaker_wav = os.path.join(cwd, "voices", speaker_wav)
    assistant.set_tts_settings(speaker_wav)


assistant = Core(
    use_chatgpt_placeholder = True
)

cwd = os.path.dirname(os.path.realpath(__file__))


with gr.Blocks() as demo:
    with gr.Tab("Basic Settings"):
        with gr.Row():
            phone_number = gr.Textbox(label="Twój Nr. Telefonu")
            order_items = gr.Textbox(label="Zamówienie", lines=5)
            delivery_address = gr.Textbox(label="Adres dostawy")
            payment_method = gr.Dropdown(label="Metoda płatności", choices=["Gotówka", "Karta"])
            with gr.Column():
                speaker_wav = gr.Textbox(label="Wav głosu", value="lector.wav")
                set_voice = gr.Button("Ustaw głos")

                # init settings
                #assistant.set_tts_settings(speaker_wav.value)
                set_voice_wav(speaker_wav.value)

                #set_voice.click(assistant.set_tts_settings, inputs=[speaker_wav], outputs=[])
                set_voice.click(set_voice_wav, inputs=[speaker_wav], outputs=[])
        with gr.Row():
            start_btn = gr.Button("Start Pizzobota")
            stop_btn = gr.Button("Stop Pizzobota")

        

        start_btn.click(start_assistant, inputs=[phone_number, order_items, delivery_address, payment_method], outputs=[])
        stop_btn.click(stop_assistant, inputs=[])

    with gr.Tab("Advanced Settings"):
        speech_recog_timeout = gr.Number(label="Speech Recog Timeout (sec)", value=1)
        window_size_sec = gr.Number(label="Window Size (sec)", value=0.1)
        vad_threshold = gr.Number(label="VAD Threshold", value=0.65)
        min_silence_duration_ms = gr.Number(label="Min Silence Duration (ms)", value=250)
        speech_pad_ms = gr.Number(label="Speech Pad (ms)", value=0)

        # init settings
        set_advanced_settings(speech_recog_timeout.value, window_size_sec.value, vad_threshold.value, min_silence_duration_ms.value, speech_pad_ms.value)

        set_adv_btn = gr.Button("Ustaw")
        set_adv_btn.click(set_advanced_settings, inputs=[speech_recog_timeout, window_size_sec, vad_threshold, min_silence_duration_ms, speech_pad_ms], outputs=[])

demo.launch()