// SPDX-FileCopyrightText: 2026, Shaun Reed <shaunrd0@gmail.com>
//
// SPDX-License-Identifier: GNU General Public License v3.0 or later

use crate::AppContext;
use anyhow::Result;
use cxx_qt_lib::{QMapPair, QMapPair_QString_QVariant, QString, QVariant};

pub mod colors;
pub mod filesystem;

pub fn run(app_context: AppContext) -> Result<()> {
    libclide::trace!(target:"gui::run()", "Starting the GUI editor at {:?}", app_context.path);

    use cxx_qt_lib::{QGuiApplication, QQmlApplicationEngine, QUrl};

    let mut app = QGuiApplication::new();
    let mut engine = QQmlApplicationEngine::new();

    // Set QML property for the directory provided to the CLI.
    let path = QString::from(app_context.path.to_string_lossy().to_string());
    let mut map = QMapPair_QString_QVariant::default();
    map.insert(QString::from("appContextPath"), QVariant::from(&path));
    engine.as_mut().unwrap().set_initial_properties(&map);

    if let Some(engine) = engine.as_mut() {
        engine.add_import_path(&QString::from("qml/"));
    }
    if let Some(engine) = engine.as_mut() {
        engine.load(&QUrl::from("qml/main.qml"));
    }

    if let Some(app) = app.as_mut() {
        app.exec();
    }

    Ok(())
}
