# template.py

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from tkinter import ttk as tkttk
from datetime import datetime
import screeninfo

def create_gui():
    screen = screeninfo.get_monitors()[0]
    screen_width = int(screen.width * 0.75)
    screen_height = int(screen.height * 0.75)

    root = ttk.Window(title="Endurance Test Bench", themename="morph")
    root.geometry(f"{screen_width}x{screen_height}")
    root.resizable(True, True)  # Allow resizing

    # === Top control bar ===
    top_frame = ttk.Frame(root, padding=10)
    top_frame.pack(fill=X)

    connect_btn = ttk.Button(top_frame, text="✅ Connect to CAN", bootstyle="success-outline")
    connect_btn.pack(side=LEFT, padx=5)

    disconnect_btn = ttk.Button(top_frame, text="❌ Disconnect", bootstyle="danger-outline")
    disconnect_btn.pack(side=LEFT, padx=5)

    graph_btn = ttk.Button(top_frame, text="🖥 Show Graphs", bootstyle="info-outline")
    graph_btn.pack(side=RIGHT, padx=5)

    # === Valve Overview Title ===
    valve_title = ttk.Label(root, text="Valve Overview", font=("Helvetica", 16, "bold"), padding=(10, 5))
    valve_title.pack(anchor=W)

    # === Valve Tree Container ===
    tree_container = ttk.Frame(root, padding=(10, 5))
    tree_container.pack(fill=BOTH, expand=True)

    # === Valve Treeview Widget ===
    valve_columns = ("Node ID", "Serial No.", "Setpoint", "Feedback", "Drift")
    valve_tree = tkttk.Treeview(tree_container, columns=valve_columns, show="tree headings", height=16)

    # Style: Font and row size
    style = ttk.Style()
    style.configure("Treeview", font=("Segoe UI Emoji", 11), rowheight=28)

    # Set column widths proportionally to screen size
    # First column (tree column) for icons
    valve_tree.heading("#0", text="Status")
    valve_tree.column("#0", width=50, anchor="center", stretch=False)

    # Other data columns
    col_width = int(screen_width / (len(valve_columns) + 1))  # +1 because of the tree column
    for col in valve_columns:
        valve_tree.heading(col, text=col)
        valve_tree.column(col, anchor='center', width=col_width, stretch=True)

    valve_tree.pack(side=LEFT, fill=BOTH, expand=True)

    # === Scrollbar ===
    scrollbar = ttk.Scrollbar(tree_container, orient=VERTICAL, command=valve_tree.yview)
    valve_tree.configure(yscrollcommand=scrollbar.set)
    scrollbar.pack(side=RIGHT, fill=Y)

    # === Feedback section ===
    feedback_title = ttk.Label(root, text="System Feedback", font=("Helvetica", 12, "bold"), padding=(10, 10))
    feedback_title.pack(anchor=W)

    feedback_frame = ttk.Frame(root, padding=(10, 0))
    feedback_frame.pack(fill=X, padx=10, pady=10)

    pressure_frame = ttk.Labelframe(feedback_frame, text="Pressure Sensor", padding=10)
    pressure_frame.pack(side=LEFT, fill=BOTH, expand=True, padx=5)
    pressure_labels = []
    for i in range(3):
        lbl = ttk.Label(pressure_frame, text=f"Pressure {i+1}: 0 bar")
        lbl.pack(anchor=W)
        pressure_labels.append(lbl)

    flow_frame = ttk.Labelframe(feedback_frame, text="Flowmeter", padding=10)
    flow_frame.pack(side=LEFT, fill=BOTH, expand=True, padx=5)
    flow_labels = []
    for i in range(4):
        lbl = ttk.Label(flow_frame, text=f"Flowmeter {i+1}: 0 L/h")
        lbl.pack(anchor=W)
        flow_labels.append(lbl)

    pump_frame = ttk.Labelframe(feedback_frame, text="Pump RPM", padding=10)
    pump_frame.pack(side=LEFT, fill=BOTH, expand=True, padx=5)
    pump_rpm_label = ttk.Label(pump_frame, text="0", font=("Helvetica", 16))
    pump_rpm_label.pack(anchor=W)

    return root, connect_btn, disconnect_btn, graph_btn, valve_tree, pressure_labels, flow_labels, pump_rpm_label
