import os

import gradio as gr
from gradio_client import Client

client = Client("Iker/FactChecking-Backend")


def fact_checking(article_topic: str, config="pro"):
    result = client.predict(
        article_topic=article_topic, config=config, api_name="/fact_checking"
    )

    return result


if __name__ == "__main__":
    theme = gr.themes.Soft(
        primary_hue="green",
        secondary_hue="gray",
        neutral_hue="neutral",
        font=[
            gr.themes.GoogleFont("Poppins"),
            gr.themes.GoogleFont("Poppins"),
            gr.themes.GoogleFont("Poppins"),
            gr.themes.GoogleFont("Poppins"),
        ],
        font_mono=[
            gr.themes.GoogleFont("Poppins"),
            gr.themes.GoogleFont("Poppins"),
            gr.themes.GoogleFont("Poppins"),
            gr.themes.GoogleFont("Poppins"),
        ],
    ).set(
        body_text_color="*secondary_600",
        button_border_width="*block_label_border_width",
        button_primary_background_fill="*primary_600",
        button_secondary_background_fill="*primary_500",
        button_secondary_background_fill_hover="*primary_400",
        button_secondary_border_color="*primary_500",
    )

    with gr.Blocks(
        theme=theme,
        title="🤖 Automated Fact-Checking Engine",
        analytics_enabled=False,
    ) as demo:
        gr_text = gr.Textbox(
            label="Fact-Checking Statement",
            # info="Write here the statement you want to fact-check",
            show_label=False,
            lines=1,
            interactive=True,
            placeholder="Los coches electricos contaminan más que los coches de gasolina",
        )

        gr_mode = gr.Radio(
            label="Fact-Checking Mode",
            info='Choose the fact-checking mode. The "Turbo" mode is faster and cheaper. The "Pro" mode is more accurate but more expensive.',
            choices=["Pro", "Turbo"],
            type="value",
            value="Pro",
            show_label=True,
            interactive=True,
        )

        gr_play = gr.Button("Fact-Checking")

        gr_output = gr.Markdown(
            label="Fact-Checking Results",
            visible=True,
        )

        gr_ft = gr.Textbox(
            label="fact_checking",
            info="String with fact checking text",
            lines=1,
            interactive=False,
            visible=False,
        )

        gr_qa = gr.Textbox(
            label="qa",
            info="Questions and answers, first line is the question, second line is the answer, and so on",
            lines=1,
            interactive=False,
            visible=False,
        )

        gr_citations = gr.Textbox(
            label="citations",
            info="Here you will see the citations, first line is the citation id, second line is the url, and so on",
            lines=1,
            interactive=False,
            visible=False,
        )

        gr_image = gr.Textbox(
            label="image",
            info="Contains the image url",
            interactive=False,
            visible=False,
        )

        gr_play.click(
            fact_checking,
            inputs=[gr_text, gr_mode],
            outputs=[gr_output, gr_ft, gr_qa, gr_citations, gr_image],
        )

        demo.queue(default_concurrency_limit=1)
        demo.launch(auth=(os.getenv("GRADIO_USERNAME"), os.getenv("GRADIO_PASSWORD")))