# Copyright (c) 2025 Martin Kayser (tebarius)
# Licensed under the MIT License. See LICENSE file in the project root.
import streamlit as st

import tools

standard_output = ('#### Um den HILFE-Text zu einzelnen Funktionen aufzurufen bitte die Funktion mit leerem'
                  ' Eingabefeld aufrufen.')

st.set_page_config(
    # we do also patching static-files of streamlit in the docker-container so bookmarks will have
    # the same favicon and if posting links for example in whatsapp they will have the same title
    page_title="tebarius Mysteryhelfer (web)",
    page_icon="./favicon.ico",
    layout="wide"
)

st.logo('./logo.png', size='large')

logo_col, title_col = st.columns([1,5])
with logo_col:
    st.image('./logo-mit-tb.png', width=150)
with title_col:
    st.write('### tebarius Mysteryhelfer (web)')

# Callback-Funktion zur Verarbeitung der Auswahl
def auswahl_verarbeiten():
    text = st.session_state.input_text
    auswahl = st.session_state.option
    if auswahl == "Wähle eine Funktion":
        st.session_state.output_text = standard_output
        st.session_state.map_data = None
    elif auswahl == "Cesarchiffre (all)":
        st.session_state.output_text = tools.cesar_all(text)
        st.session_state.map_data = None
    elif auswahl == "BW,BWW,... ermitteln":
        st.session_state.output_text = tools.buchstabenwortwert(text)
        st.session_state.map_data = None
    elif auswahl == "Buchstabenwert->Text":
        st.session_state.output_text = tools.buchstabenwert_zu_text(text)
        st.session_state.map_data = None
    elif auswahl == "Zeichenzählen":
        st.session_state.output_text = tools.zeichenanzahl(text)
        st.session_state.map_data = None
    elif auswahl == "Text rückwärts":
        st.session_state.output_text = tools.zeichenkette_reverse(text)
        st.session_state.map_data = None
    elif auswahl == "Quersumme(n)":
        st.session_state.output_text = tools.quersummen(text)
        st.session_state.map_data = None
    elif auswahl == "Einschlüsse zählen":
        st.session_state.output_text = tools.einschluss_count(text)
        st.session_state.map_data = None
    elif auswahl == "ABC -> Morse":
        st.session_state.output_text = tools.abc_to_morse(text)
        st.session_state.map_data = None
    elif auswahl == "Morse -> ABC":
        st.session_state.output_text = tools.morse_to_abc(text)
        st.session_state.map_data = None
    elif auswahl == "ROT5":
        st.session_state.output_text = tools.rot5(text)
        st.session_state.map_data = None
    elif auswahl == "ROT13":
        st.session_state.output_text = tools.rot13(text)
        st.session_state.map_data = None
    elif auswahl == "ROT18":
        st.session_state.output_text = tools.rot18(text)
        st.session_state.map_data = None
    elif auswahl == "ROT47":
        st.session_state.output_text = tools.rot47(text)
        st.session_state.map_data = None
    elif auswahl == "ASCII-Text->HEX":
        st.session_state.output_text = tools.ascii_to_hex(text)
        st.session_state.map_data = None
    elif auswahl == "ASCII-Text->DEZ":
        st.session_state.output_text = tools.ascii_to_dez(text)
        st.session_state.map_data = None
    elif auswahl == "ASCII-Text->Octal":
        st.session_state.output_text = tools.ascii_to_octal(text)
        st.session_state.map_data = None
    elif auswahl == "ASCII-Text->BIN (16bit)":
        st.session_state.output_text = tools.ascii_to_bin16(text)
        st.session_state.map_data = None
    elif auswahl == "ASCII-Text->BIN (8bit)":
        st.session_state.output_text = tools.ascii_to_bin8(text)
        st.session_state.map_data = None
    elif auswahl == "HEX->ASCII-Text":
        st.session_state.output_text = tools.hex_to_ascii(text)
        st.session_state.map_data = None
    elif auswahl == "DEZ->ASCII-Text":
        st.session_state.output_text = tools.dez_to_ascii(text)
        st.session_state.map_data = None
    elif auswahl == "Octal->ASCII-Text":
        st.session_state.output_text = tools.octal_to_ascii(text)
        st.session_state.map_data = None
    elif auswahl == "BIN->ASCII-Text":
        st.session_state.output_text = tools.bin_to_ascii(text)
        st.session_state.map_data = None
    elif auswahl == "Zahlwortsuche-DE (0-12)":
        st.session_state.output_text = tools.zahlwortsuche_de(text)
        st.session_state.map_data = None
    elif auswahl == "Zahlwortsuche-EN (0-15)":
        st.session_state.output_text = tools.zahlwortsuche_en(text)
        st.session_state.map_data = None
    elif auswahl == "KENNYspeak kodieren":
        st.session_state.output_text = tools.kenny_kodieren(text)
        st.session_state.map_data = None
    elif auswahl == "KENNYspeak dekodieren":
        st.session_state.output_text = tools.kenny_dekodieren(text)
        st.session_state.map_data = None
    elif auswahl == "KENNYspeak raten":
        st.session_state.output_text = tools.kenny_raten(text)
        st.session_state.map_data = None
    elif auswahl == "Primz.Alpha dekodieren":
        st.session_state.output_text = tools.primzahlalphabet_dekodieren(text)
        st.session_state.map_data = None
    elif auswahl == "ist (n.te) Primzahl?":
        st.session_state.output_text = tools.primzahlpruefen(text)
        st.session_state.map_data = None
    elif auswahl == "zeige n.te Primzahl":
        st.session_state.output_text = tools.nte_primzahl(text)
        st.session_state.map_data = None
    elif auswahl == "Primfaktorenzerlegung":
        st.session_state.output_text = tools.primfaktoren(text)
        st.session_state.map_data = None
    elif auswahl == "HEX -> DEZ,OCT,BIN":
        st.session_state.output_text = tools.hex_to_dez_oct_bin(text)
        st.session_state.map_data = None
    elif auswahl == "DEZ -> HEX,OCT,BIN":
        st.session_state.output_text = tools.dez_to_hex_oct_bin(text)
        st.session_state.map_data = None
    elif auswahl == "OCT -> HEX,DEZ,BIN":
        st.session_state.output_text = tools.oct_to_hex_dez_bin(text)
        st.session_state.map_data = None
    elif auswahl == "BIN -> HEX,DEZ,OCT":
        st.session_state.output_text = tools.bin_to_hex_dez_oct(text)
        st.session_state.map_data = None
    elif auswahl == "Text->Tomtom":
        st.session_state.output_text = tools.abc_to_tomtom(text)
        st.session_state.map_data = None
    elif auswahl == "Tomtom->Text":
        st.session_state.output_text = tools.tomtom_to_abc(text)
        st.session_state.map_data = None
    elif auswahl == "Text->Slash and Pipe":
        st.session_state.output_text = tools.text_to_slashpipe(text)
        st.session_state.map_data = None
    elif auswahl == "Slash and Pipe->Text":
        st.session_state.output_text = tools.slashpipe_to_text(text)
        st.session_state.map_data = None
    elif auswahl == "PSE: O.zahl<->Symbol":
        st.session_state.output_text = tools.periodensystem(text)
        st.session_state.map_data = None
    elif auswahl == "Nak-Nak -> Text":
        st.session_state.output_text = tools.naknak_to_text(text)
        st.session_state.map_data = None
    elif auswahl == "Navajo -> Text":
        st.session_state.output_text = tools.navajo_to_text(text)
        st.session_state.map_data = None
    elif auswahl == "Kreiszahl PI":
        st.session_state.output_text = tools.pi_suche(text)
        st.session_state.map_data = None
    elif auswahl == "Eulersche Zahl":
        st.session_state.output_text = tools.euler_suche(text)
        st.session_state.map_data = None
    elif auswahl == "phi (goldener Schnitt)":
        st.session_state.output_text = tools.goldener_schnitt_suche(text)
        st.session_state.map_data = None
    elif auswahl == "Anagrammsuche-DE":
        st.session_state.output_text = tools.anagramm_suche_de(text)
        st.session_state.map_data = None
    elif auswahl == "Anagrammsuche-EN":
        st.session_state.output_text = tools.anagramm_suche_en(text)
        st.session_state.map_data = None
    elif auswahl == "Wortsuche-DE":
        st.session_state.output_text = tools.wortsuche_de(text)
        st.session_state.map_data = None
    elif auswahl == "Wortsuche-EN":
        st.session_state.output_text = tools.wortsuche_en(text)
        st.session_state.map_data = None
    elif auswahl == "Re-Morse-DE":
        st.session_state.output_text = tools.remorse_de(text)
        st.session_state.map_data = None
    elif auswahl == "Re-Morse-EN":
        st.session_state.output_text = tools.remorse_en(text)
        st.session_state.map_data = None
    elif auswahl == "T9-DE dekodieren":
        st.session_state.output_text = tools.t9_de(text)
        st.session_state.map_data = None
    elif auswahl == "T9-EN dekodieren":
        st.session_state.output_text = tools.t9_en(text)
        st.session_state.map_data = None
    elif auswahl == "Vigenere-Chiffre":
        st.session_state.output_text = tools.vigenere(text, additional_parameter)
        st.session_state.map_data = None
    elif auswahl == "Wolseley-Chiffre":
        st.session_state.output_text = tools.wolseley(text, additional_parameter)
        st.session_state.map_data = None
    elif auswahl == "Mono.-Substitution":
        st.session_state.output_text = tools.monoalphasubstitution(text, additional_parameter)
        st.session_state.map_data = None
    elif auswahl == "REPLACE":
        st.session_state.output_text = tools.REPLACE(text)
        st.session_state.map_data = None
    elif auswahl == "REPLACE":
        st.session_state.output_text = tools.REPLACE(text)
        st.session_state.map_data = None
    elif auswahl == "REPLACE":
        st.session_state.output_text = tools.REPLACE(text)
        st.session_state.map_data = None


    elif auswahl == "Dummy mit Karte":
        output, mapd = tools.funktion_mit_karte(text, additional_parameter)
        st.session_state.output_text = output
        st.session_state.map_data = mapd

# Standardwerte im Session State initialisieren
if 'option' not in st.session_state:
    st.session_state.option = "Wähle eine Funktion"
if 'input_text' not in st.session_state:
    st.session_state.input_text = ""
if 'output_text' not in st.session_state:
    st.session_state.output_text = standard_output
if 'map_data' not in st.session_state:
    st.session_state.map_data = None

# Eingabefeld im Hauptbereich
input_text = st.text_area('Gib deinen Text hier ein und wähle eine Funktion im Menü auf der linken Seite:',
                          key='input_text',
                          height=150)

# --- Sidebar: Buttons selektieren und Aktion setzen ---
option = st.sidebar.radio("hidden_label",
                          options=("Wähle eine Funktion",
                                   "Cesarchiffre (all)",
                                   "BW,BWW,... ermitteln",
                                   "Buchstabenwert->Text",
                                   "Zeichenzählen",
                                   "Text rückwärts",
                                   "Quersumme(n)",
                                   "Einschlüsse zählen",
                                   "ABC -> Morse",
                                   "Morse -> ABC",
                                   "ROT5",
                                   "ROT13",
                                   "ROT18",
                                   "ROT47",
                                   "ASCII-Text->HEX",
                                   "ASCII-Text->DEZ",
                                   "ASCII-Text->Octal",
                                   "ASCII-Text->BIN (16bit)",
                                   "ASCII-Text->BIN (8bit)",
                                   "HEX->ASCII-Text",
                                   "DEZ->ASCII-Text",
                                   "Octal->ASCII-Text",
                                   "Zahlwortsuche-DE (0-12)",
                                   "Zahlwortsuche-EN (0-15)",
                                   "KENNYspeak kodieren",
                                   "KENNYspeak dekodieren",
                                   "KENNYspeak raten",
                                   "Primz.Alpha dekodieren",
                                   "ist (n.te) Primzahl?",
                                   "zeige n.te Primzahl",
                                   "Primfaktorenzerlegung",
                                   "HEX -> DEZ,OCT,BIN",
                                   "DEZ -> HEX,OCT,BIN",
                                   "OCT -> HEX,DEZ,BIN",
                                   "BIN -> HEX,DEZ,OCT",
                                   "Text->Tomtom",
                                   "Tomtom->Text",
                                   "Text->Slash and Pipe",
                                   "Slash and Pipe->Text",
                                   "PSE: O.zahl<->Symbol",
                                   "Nak-Nak -> Text",
                                   "Navajo -> Text",
                                   "Kreiszahl PI",
                                   "Eulersche Zahl",
                                   "phi (goldener Schnitt)",
                                   "Anagrammsuche-DE",
                                   "Anagrammsuche-EN",
                                   "Wortsuche-DE",
                                   "Wortsuche-EN",
                                   "Re-Morse-DE",
                                   "Re-Morse-EN",
                                   "T9-DE dekodieren",
                                   "T9-EN dekodieren",
                                   "Vigenere-Chiffre",
                                   "Wolseley-Chiffre",
                                   "Mono.-Substitution",
                                   "",
                                   "",
                                   "",
                                   "",
                                   "",
                                   "Dummy mit Karte",
                                   ),
                          key='option',
                          on_change=auswahl_verarbeiten,
                          label_visibility='collapsed'
                          )

# Optionales einzeiliges Eingabefeld nur für bestimmte Funktionen
additional_parameter = None
if option == "Dummy mit Karte":
    additional_parameter = st.text_input(
        "Optionaler Zusatztext für Dummy mit Karte",
        placeholder="Zusätzliche Eingabe hier"
    )
elif option in ["Vigenere-Chiffre", "Wolseley-Chiffre"]:
    additional_parameter = st.text_input(
        "Schlüsselwort:",
        placeholder="Schlüsselwort hier eingeben"
    )
elif option in ["Mono.-Substitution"]:
    additional_parameter = st.text_input(
        "Schlüsselwort/Schlüsselalphabet:",
        placeholder="Schlüsselwort/Schlüsselalphabet hier eingeben"
    )
# Button zum manuellen Ausführen der Verarbeitungsfunktion
if st.button(f"{st.session_state.option}"):
    auswahl_verarbeiten()

# Ausgabefeld
st.markdown(st.session_state.output_text)

# Karte anzeigen, falls aktiviert
if st.session_state.map_data is not None:
    st.subheader("Kartenansicht")
    st.map(st.session_state.map_data)
