import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image
from tkinter import font
import threading

def start_gui():
    def SaveCalloutReport():
        # Erstelle das Fenster
        root = tk.Tk()
        root.configure(bg="LightBlue")
        root.title("Callout Report")

        # Erstelle die Eingabefelder
        tk.Label(root, text="Callout Type: ", bg="LightBlue").grid(row=0, column=0)
        callout_type_entry = tk.Entry(root)
        callout_type_entry.grid(row=0, column=1)

        tk.Label(root, text="Callout Date: ", bg="LightBlue").grid(row=1, column=0)
        callout_date_entry = tk.Entry(root)
        callout_date_entry.grid(row=1, column=1)

        tk.Label(root, text="Callout Time: ", bg="LightBlue").grid(row=2, column=0)
        callout_time_entry = tk.Entry(root)
        callout_time_entry.grid(row=2, column=1)

        tk.Label(root, text="Officer Name: ", bg="LightBlue").grid(row=3, column=0)
        officer_name_entry = tk.Entry(root)
        officer_name_entry.grid(row=3, column=1)

        tk.Label(root, text="Officer Rank: ", bg="LightBlue").grid(row=4, column=0)
        officer_rank_entry = tk.Entry(root)
        officer_rank_entry.grid(row=4, column=1)

        tk.Label(root, text="Officer Number: ", bg="LightBlue").grid(row=5, column=0)
        officer_number_entry = tk.Entry(root)
        officer_number_entry.grid(row=5, column=1)

        tk.Label(root, text="Division: ", bg="LightBlue").grid(row=6, column=0)
        division_entry = tk.Entry(root)
        division_entry.grid(row=6, column=1)

        tk.Label(root, text="Agency: ", bg="LightBlue").grid(row=7, column=0)
        agency_entry = tk.Entry(root)
        agency_entry.grid(row=7, column=1)

        tk.Label(root, text="Location: ", bg="LightBlue").grid(row=8, column=0)
        location_entry = tk.Entry(root)
        location_entry.grid(row=8, column=1)

        tk.Label(root, text="Date: ", bg="LightBlue").grid(row=9, column=0)
        date_entry = tk.Entry(root)
        date_entry.grid(row=9, column=1)

        tk.Label(root, text="Code 4 at: ", bg="LightBlue").grid(row=10, column=0)
        code4_entry = tk.Entry(root)
        code4_entry.grid(row=10, column=1)

        tk.Label(root, text="Event Log: ", bg="LightBlue").grid(row=11, column=0)
        event_log_text = tk.Text(root, height=10, width=50)
        event_log_text.grid(row=11, column=1)

        # Funktion zum Speichern des Berichts
        def save_report():
            # Öffne den Dateidialog zum Speichern der Datei
            file_path = filedialog.asksaveasfilename(defaultextension=".txt")
            if file_path:
                # Schreibe die Daten in die Datei
                with open(file_path, "w") as f:
                    f.write(f"Callout Type: {callout_type_entry.get()}\n")
                    f.write(f"Callout Date: {callout_date_entry.get()}\n")
                    f.write(f"Callout Time: {callout_time_entry.get()}\n")
                    f.write(f"Officer Name: {officer_name_entry.get()}\n")
                    f.write(f"Officer Rank: {officer_rank_entry.get()}\n")
                    f.write(f"Officer Number: {officer_number_entry.get()}\n")
                    f.write(f"Division: {division_entry.get()}\n")
                    f.write(f"Agency: {agency_entry.get()}\n")
                    f.write(f"Location: {location_entry.get()}\n")
                    f.write(f"Date: {date_entry.get()}\n")
                    f.write(f"Code 4 at: {code4_entry.get()}\n")
                    f.write(f"Event Log: {event_log_text.get('1.0', 'end')}")

        save_button = tk.Button(root, text="Save Report", command=save_report, bg="LightGreen")
        save_button.grid(row=12, column=1)

        # root.mainloop()

    def SaveIncidentReport():
        # Erstelle das Fenster
        root = tk.Tk()
        root.configure(bg="LightBlue")
        root.title("Incident Report")

        # Erstelle die Eingabefelder
        tk.Label(root, text="Incident Type: ", bg="LightBlue").grid(row=0, column=0)
        callout_type_entry = tk.Entry(root)
        callout_type_entry.grid(row=0, column=1)

        tk.Label(root, text="Incident Date: ", bg="LightBlue").grid(row=1, column=0)
        callout_date_entry = tk.Entry(root)
        callout_date_entry.grid(row=1, column=1)

        tk.Label(root, text="Incident Time: ", bg="LightBlue").grid(row=2, column=0)
        callout_time_entry = tk.Entry(root)
        callout_time_entry.grid(row=2, column=1)

        tk.Label(root, text="Officer Name: ", bg="LightBlue").grid(row=3, column=0)
        officer_name_entry = tk.Entry(root)
        officer_name_entry.grid(row=3, column=1)

        tk.Label(root, text="Officer Rank: ", bg="LightBlue").grid(row=4, column=0)
        officer_rank_entry = tk.Entry(root)
        officer_rank_entry.grid(row=4, column=1)

        tk.Label(root, text="Officer Number: ", bg="LightBlue").grid(row=5, column=0)
        officer_number_entry = tk.Entry(root)
        officer_number_entry.grid(row=5, column=1)

        tk.Label(root, text="Division: ", bg="LightBlue").grid(row=6, column=0)
        division_entry = tk.Entry(root)
        division_entry.grid(row=6, column=1)

        tk.Label(root, text="Agency: ", bg="LightBlue").grid(row=7, column=0)
        agency_entry = tk.Entry(root)
        agency_entry.grid(row=7, column=1)

        tk.Label(root, text="Location: ", bg="LightBlue").grid(row=8, column=0)
        location_entry = tk.Entry(root)
        location_entry.grid(row=8, column=1)

        tk.Label(root, text="Grounds for Action taken:", bg="LightBlue").grid(row=9, column=0)
        grounds_entry = tk.Text(root, height=2, width=10)
        grounds_entry.grid(row=9, column=1)

        tk.Label(root, text="Code 4 at: ", bg="LightBlue").grid(row=10, column=0)
        code4_entry = tk.Entry(root)
        code4_entry.grid(row=10, column=1)

        tk.Label(root, text="Event Log: ", bg="LightBlue").grid(row=11, column=0)
        event_log_text = tk.Text(root, height=10, width=50)
        event_log_text.grid(row=11, column=1)

        # Funktion zum Speichern des Berichts
        def save_report():
            # Öffne den Dateidialog zum Speichern der Datei
            file_path = filedialog.asksaveasfilename(defaultextension=".txt")
            if file_path:
                # Schreibe die Daten in die Datei
                with open(file_path, "w") as f:
                    f.write(f"Incident Type: {callout_type_entry.get()}\n")
                    f.write(f"Incident Date: {callout_date_entry.get()}\n")
                    f.write(f"Incident Time: {callout_time_entry.get()}\n")
                    f.write(f"Officer Name: {officer_name_entry.get()}\n")
                    f.write(f"Officer Rank: {officer_rank_entry.get()}\n")
                    f.write(f"Officer Number: {officer_number_entry.get()}\n")
                    f.write(f"Division: {division_entry.get()}\n")
                    f.write(f"Agency: {agency_entry.get()}\n")
                    f.write(f"Location: {location_entry.get()}\n")
                    f.write(f"Grounds for Action taken: {grounds_entry.get()}\n")
                    f.write(f"Code 4 at: {code4_entry.get()}\n")
                    f.write(f"Event Log: {event_log_text.get('1.0', 'end')}")

        save_button = tk.Button(root, text="Save Report", command=save_report, bg="LightGreen")
        save_button.grid(row=12, column=1)

        # root.mainloop()

    def SavePatrolReport():
        # Erstelle das Fenster
        root = tk.Tk()
        root.configure(bg="LightBlue")
        root.title("Patrol Report")

        # Erstelle die Eingabefelder
        tk.Label(root, text="Patrol Date: ", bg="LightBlue").grid(row=0, column=0)
        patrol_date = tk.Entry(root)
        patrol_date.grid(row=0, column=1)

        tk.Label(root, text="Patrol Division: ", bg="LightBlue").grid(row=1, column=0)
        patrol_division = tk.Entry(root)
        patrol_division.grid(row=1, column=1)

        tk.Label(root, text="Callsign: ", bg="LightBlue").grid(row=2, column=0)
        patrol_callsign = tk.Entry(root)
        patrol_callsign.grid(row=2, column=1)

        tk.Label(root, text="Unit Nr: ", bg="LightBlue").grid(row=3, column=0)
        unit_number = tk.Entry(root)
        unit_number.grid(row=3, column=1)

        tk.Label(root, text="Officer(s) + Rank: ", bg="LightBlue").grid(row=4, column=0)
        officer_entry = tk.Entry(root)
        officer_entry.grid(row=4, column=1)

        tk.Label(root, text="Patrol Start Time: ", bg="LightBlue").grid(row=5, column=0)
        patrol_start = tk.Entry(root)
        patrol_start.grid(row=5, column=1)

        tk.Label(root, text="Patrol End Time: ", bg="LightBlue").grid(row=6, column=0)
        patrol_end = tk.Entry(root)
        patrol_end.grid(row=6, column=1)

        tk.Label(root, text="Station: ", bg="LightBlue").grid(row=7, column=0)
        station = tk.Entry(root)
        station.grid(row=7, column=1)

        tk.Label(root, text="Notes: ", bg="LightBlue").grid(row=8, column=0)
        notes = tk.Text(root, height=4, width=50)
        notes.grid(row=8, column=1)

        # Funktion zum Speichern des Berichts
        def save_report():
            # Öffne den Dateidialog zum Speichern der Datei
            file_path = filedialog.asksaveasfilename(defaultextension=".txt")
            if file_path:
                # Schreibe die Daten in die Datei
                with open(file_path, "w") as f:
                    f.write(f"Patrol Date: {patrol_date.get()}\n")
                    f.write(f"Patrol Division: {patrol_division.get()}\n")
                    f.write(f"Callsign: {patrol_callsign.get()}\n")
                    f.write(f"Unit Nr: {unit_number.get()}\n")
                    f.write(f"Officer(s) + Rank: {officer_entry.get()}\n")
                    f.write(f"Patrol Start Time: {patrol_start.get()}\n")
                    f.write(f"Patrol End Time: {patrol_end.get()}\n")
                    f.write(f"Station: {station.get()}\n")
                    f.write(f"Notes: {notes.get('1.0', 'end')}")

        save_button = tk.Button(root, text="Save Report", command=save_report, bg="LightGreen")
        save_button.grid(row=12, column=1)

        # root.mainloop()

    def ShowReport():
        # Erstelle das Fenster
        root = tk.Tk()
        root.configure(bg="LightBlue")
        root.title("Show Report")

        # Erstelle das Textfeld für die Anzeige des Berichts
        report_text = tk.Text(root, height=30, width=80)
        report_text.pack()

        # Funktion zum Öffnen der Datei und Anzeigen des Berichts
        def show_report():
            # Öffne den Dateidialog zum Öffnen der Datei
            file_path = filedialog.askopenfilename()
            if file_path:
                # Lese die Daten aus der Datei
                with open(file_path, "r") as f:
                    data = f.readlines()

                # Lösche den vorigen Text
                report_text.delete('1.0', tk.END)

                # Füge die formatierten Daten in das Textfeld ein
                report_text.insert(tk.END, "Police Report\n\n")
                for line in data:
                    key, value = line.strip().split(": ")
                    report_text.insert(tk.END, f"{key.upper()}: {value}\n")

        # Erstelle den Button zum Öffnen des Berichts
        show_button = tk.Button(root, text="Open Report", command=show_report, bg="Orange")
        show_button.pack(pady=10)

    def ShowTextRank():
        # Erstelle das Fenster
        root = tk.Tk()
        root.title("Ranks")

        # Erstelle das Textfeld für die Anzeige des Berichts
        text = tk.Text(root, height=30, width=80)
        text.pack()

        # Funktion zum Öffnen der Datei und Anzeigen des Berichts
        def show_report():
            # Lösche den vorigen Text
            text.delete('1.0', tk.END)

            # Lese die Daten aus der Datei
            with open("RankList.txt", "r") as file:
                file_content = file.read()
                text.insert("1.0", file_content)

        show_report()

    def ShowTextDivision():
        # Erstelle das Fenster
        root = tk.Tk()
        root.title("Divisions")

        # Erstelle das Textfeld für die Anzeige des Berichts
        text = tk.Text(root, height=30, width=80)
        text.pack()

        # Funktion zum Öffnen der Datei und Anzeigen des Berichts
        def show_report():
            # Lösche den vorigen Text
            text.delete('1.0', tk.END)

            # Lese die Daten aus der Datei
            with open("DivisionList.txt", "r") as file:
                file_content = file.read()
                text.insert("1.0", file_content)

        show_report()

    # main
    # Erstelle das Fenster
    root = tk.Tk()
    root.geometry("370x220")
    root.resizable(False, False)
    root.title("Report Manager")
    root.configure(bg="black")

    # Laden des Bildes
    img = Image.open("SA.png")
    photo = ImageTk.PhotoImage(img)

    # Erstellen des Labels für das Bild
    label = tk.Label(image=photo)
    label.image = photo  # Behalten Sie eine Referenz, um sicherzustellen, dass das Bild nicht gelöscht wird

    # Platzieren des Labels an einer bestimmten Position
    label.place(x=75, y=0)

    bold_font = font.Font(weight="bold")

    callout_button = tk.Button(root, text="Create Callout Report", command=SaveCalloutReport, bg="Red")
    callout_button.grid(row=0, column=0)

    incident_button = tk.Button(root, text="Create Incident Report", command=SaveIncidentReport, bg="LightBlue")
    incident_button.grid(row=0, column=1)

    patrol_button = tk.Button(root, text="Create Patrol Report", command=SavePatrolReport, bg="Red")
    patrol_button.grid(row=0, column=2)

    rank_button = tk.Button(root, text="Ranks", command=ShowTextRank, bg="White")
    rank_button.grid(row=1, column=0)

    display_button = tk.Button(root, text="Show Report", command=ShowReport, bg="Orange")
    display_button.grid(row=2, column=1)

    division_button = tk.Button(root, text="Divisions", command=ShowTextDivision, bg="White")
    division_button.grid(row=1, column=2)

    def open_image():
        img = Image.open("PatrolDistricts.jpg")
        img.show()

    districts_button = tk.Button(root, text="Patrol Districts", command=open_image, bg="White")
    districts_button.grid(row=1, column=1)

    description_label = tk.Label(root, text="San Andreas Law Enforcement Report Manager", fg="Blue", font=bold_font)
    description_label.place(x=0, y=140)

    description_label = tk.Label(root, text="FOR OFFICIAL USE ONLY", fg="White", bg="Blue")
    description_label.place(x=120, y=200)

    description_label = tk.Label(root, text="SALERM", fg="Red")
    description_label.place(x=0, y=200)

    description_label = tk.Label(root, text="SA State Dept.", fg="Blue")
    description_label.place(x=290, y=200)

    root.mainloop()

if __name__ == "__main__":
    gui_thread = threading.Thread(target=start_gui)
    gui_thread.start()