import gradio as gr

from .pipeline import Pipeline
from .processing import process_file, process_text_input

def create_interface():
    # Initialize the language model
    language_model = Pipeline()

    # Define the Output Format Selector
    output_format_selector = gr.Radio(
        choices=["CSV", "JSON"],
        label="Select Output Format",
        value="JSON",
        type="value"
    )

    # Define the Output Flashcards
    flashcard_output_file = gr.Textbox(
        label="Flashcards",
        lines=20,
        placeholder="Your flashcards will appear here..."
    )
    flashcard_output_text = gr.Textbox(
        label="Flashcards",
        lines=20,
        placeholder="Your flashcards will appear here..."
    )

    # Define the Gradio interface function for File Upload
    def handle_file_upload(file_obj, output_format):
        try:
            flashcards = process_file(file_obj, output_format, Pipeline())
            return flashcards
        except ValueError as ve:
            return str(ve)

    # Define the Gradio interface function for Text Input
    def handle_text_input(input_text, output_format):
        try:
            flashcards = process_text_input(input_text, output_format)
            return flashcards
        except ValueError as ve:
            return str(ve)

    # Create the Gradio Tabs
    with gr.Blocks() as interface:
        gr.Markdown("# Flashcard Studio")
        gr.Markdown(
            "Make flashcards from uploaded files or directly input text. Choose CSV or JSON output format."
        )
        with gr.Tab("Upload File"):
            with gr.Row():
                with gr.Column():
                    file_input = gr.File(
                        label="Upload a File",
                        file_types=['.txt', '.md']
                    )
                    format_selector = gr.Radio(
                        choices=["CSV", "JSON"],
                        label="Select Output Format",
                        value="CSV",
                        type="value"
                    )
                    submit_file = gr.Button("Extract Flashcards")
                with gr.Column():
                    flashcard_output_file = gr.Textbox(
                        label="Flashcards",
                        lines=20,
                        placeholder="Extracted flashcards will appear here..."
                    )
            submit_file.click(
                fn=handle_file_upload,
                inputs=[file_input, format_selector],
                outputs=flashcard_output_file
            )

        with gr.Tab("Input Text"):
            with gr.Row():
                with gr.Column():
                    text_input = gr.Textbox(
                        label="Enter Text",
                        lines=20,
                        placeholder="Type or paste your text here..."
                    )
                    format_selector_text = gr.Radio(
                        choices=["CSV", "JSON"],
                        label="Select Output Format",
                        value="CSV",
                        type="value"
                    )
                    submit_text = gr.Button("Extract Flashcards")
                with gr.Column():
                    flashcard_output_text = gr.Textbox(
                        label="Flashcards",
                        lines=20,
                        placeholder="Extracted flashcards will appear here..."
                    )
            submit_text.click(
                fn=handle_text_input,
                inputs=[text_input, format_selector_text],
                outputs=flashcard_output_text
            )

        gr.Markdown(
            """
            ---
            **Notes:**
            - Supported file types: `.txt`, `.md`.
            """
        )

    return interface