// SPDX-License-Identifier: GPL-3.0-or-later
//! GUI entry point for Temperlux.

fn main() {
    #[cfg(feature = "gui")]
    {
        temperlux::i18n::init();

        // Initialize perf logging from env var or CLI arg
        let perf_path = std::env::var("TEMPERLUX_PERF_LOG")
            .ok()
            .map(|p| {
                if p.is_empty() {
                    temperlux::perf::default_path()
                } else {
                    std::path::PathBuf::from(p)
                }
            })
            .or_else(|| {
                // Check for --perf-log in args
                let args: Vec<String> = std::env::args().collect();
                let mut path = None;
                let mut i = 1;
                while i < args.len() {
                    if args[i] == "--perf-log" {
                        // Next arg is the path, or no arg = default
                        if i + 1 < args.len() && !args[i + 1].starts_with('-') {
                            path = Some(std::path::PathBuf::from(&args[i + 1]));
                            i += 2;
                        } else {
                            path = Some(temperlux::perf::default_path());
                            i += 1;
                        }
                    } else if args[i].starts_with("--perf-log=") {
                        let val = args[i].strip_prefix("--perf-log=").unwrap();
                        if val.is_empty() {
                            path = Some(temperlux::perf::default_path());
                        } else {
                            path = Some(std::path::PathBuf::from(val));
                        }
                        i += 1;
                    } else if args[i] == "--perf-log-readback" {
                        // Handled below
                        i += 1;
                    } else {
                        i += 1;
                    }
                }
                path
            });

        let readback = std::env::var("TEMPERLUX_PERF_LOG_READBACK")
            .map(|v| v == "1" || v.to_lowercase() == "true")
            .unwrap_or(false)
            || std::env::args().any(|a| a == "--perf-log-readback");

        if let Err(e) = temperlux::perf::init(perf_path, readback) {
            eprintln!("Warning: perf logging init failed: {}", e);
        }

        temperlux::gui::window::run();
    }
    #[cfg(not(feature = "gui"))]
    {
        eprintln!("temperlux-gui requires the 'gui' feature. Build with: cargo build --features gui");
        std::process::exit(1);
    }
}
