package vassistent.ui;

import vassistent.bootstrap.ApplicationContext;
import vassistent.controller.DashboardController;
import vassistent.util.ConfigLoader;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.Properties;

/**
 * Main Swing frame containing streaming and dashboard tabs plus status indicators.
 */
public class AppWindow extends JFrame {

    private static final Properties config =
            ConfigLoader.loadProperties(
                    "config/application.properties"
            );

    private PixelStreamingView streamingView;
    private DashboardView dashboardView;
    private JTabbedPane tabs;

    private JLabel mqttStatusLabel;
    private JLabel problemLevelLabel;

    /**
     * Creates and initializes the main application window and its child views.
     */
    public AppWindow() {

        setTitle("Virtueller Gesundheitsassistent");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1400, 850);
        setLocationRelativeTo(null);

        setLayout(new BorderLayout(10,10));

        String streamUrl =
                normalizeConfigValue(
                        config.getProperty("streaming.url", "http://localhost")
                );

        streamingView = new PixelStreamingView(
                streamUrl,
                false,
                false
        );

        dashboardView = new DashboardView();

        tabs = createTabPane();
        add(tabs, BorderLayout.CENTER);
        add(createStatusBar(), BorderLayout.SOUTH);

        Font uiFont = new Font("Segoe UI", Font.PLAIN, 14);
        UIManager.put("Label.font", uiFont);
        UIManager.put("TabbedPane.font", uiFont);
    }

    /**
     * Creates the tabbed pane that hosts streaming and dashboard views.
     *
     * @return configured tabbed pane
     */
    private JTabbedPane createTabPane() {

        JTabbedPane tabs = new JTabbedPane();
        tabs.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        JPanel streamingPanel = new JPanel(new BorderLayout(10,10));
        streamingPanel.add(streamingView, BorderLayout.CENTER);

        tabs.addTab("Avatar Streaming", streamingView);
        tabs.addTab("Dashboard", dashboardView);

        return tabs;
    }

    /**
     * Creates the status bar with MQTT and problem-level indicators.
     *
     * @return configured status bar panel
     */
    private JPanel createStatusBar() {

        JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT,15,5));

        statusBar.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createMatteBorder(1,0,0,0, Color.LIGHT_GRAY),
                BorderFactory.createEmptyBorder(5,10,5,10)
        ));

        mqttStatusLabel = new JLabel("MQTT: Disconnected");
        problemLevelLabel = new JLabel("Problem: NONE");

        statusBar.add(mqttStatusLabel);
        statusBar.add(new JLabel(" | "));
        statusBar.add(problemLevelLabel);

        return statusBar;
    }

    /**
     * Updates the MQTT connection status label.
     *
     * @param connected whether MQTT is currently connected
     */
    public void updateMqttStatus(boolean connected) {
        mqttStatusLabel.setText("MQTT: " +
                (connected ? "Connected" : "Disconnected"));
    }

    /**
     * Updates the currently displayed problem level label.
     *
     * @param level problem level name to display
     */
    public void updateProblemLevel(String level) {
        problemLevelLabel.setText("Problem: " + level);
    }

    /**
     * Returns the dashboard view component.
     *
     * @return dashboard view
     */
    public DashboardView getDashboardView() {
        return dashboardView;
    }

    /**
     * Returns the pixel streaming view component.
     *
     * @return streaming view
     */
    public PixelStreamingView getStreamingView() {
        return streamingView;
    }

    /**
     * Returns the main tabbed pane.
     *
     * @return tab container
     */
    public JTabbedPane getTabs() {
        return tabs;
    }

    /**
     * Trims a config value and strips optional wrapping quotes.
     *
     * @param value raw config value
     * @return normalized value or {@code null}
     */
    private static String normalizeConfigValue(String value) {
        if (value == null) {
            return null;
        }

        String trimmed = value.trim();

        if ((trimmed.startsWith("\"") && trimmed.endsWith("\""))
                || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
            return trimmed.substring(1, trimmed.length() - 1);
        }

        return trimmed;
    }
}
