/*
 * GnomeSettingsVault - A GNOME configuration backup utility.
 */

#pragma once
#include <gtkmm.h>
#include <giomm.h>
#include <string>
#include <fstream>
#include <thread>
#include <mutex>
#include <queue>
#include <atomic>

struct JobConfig {
    bool themes;
    bool icons;
    bool wallpapers;
    bool extensions;
    bool dconf;
    bool dry_run;
    std::string vault_name;
    std::string custom_restore_path;
};

class MainWindow : public Gtk::Window {
public:
    MainWindow();
    virtual ~MainWindow();

private:
    Gtk::HeaderBar m_HeaderBar;
    Gtk::Box m_MainLayout;
    Gtk::Notebook m_Notebook;
    Gtk::ScrolledWindow m_ScrolledWindow;
    Gtk::TextView m_LogView;
    Glib::RefPtr<Gtk::TextBuffer> m_RefTextBuffer;
    
    Gtk::Box m_BottomBox;
    Gtk::Label m_StatusLabel;
    Gtk::CheckButton m_CheckDebugMode;

    Gtk::Box m_VBoxBackup;
    Gtk::Label m_LabelBackupInstruction;
    Gtk::CheckButton m_CheckThemesBackup;
    Gtk::CheckButton m_CheckIconsBackup;
    Gtk::CheckButton m_CheckWallpapersBackup;
    Gtk::CheckButton m_CheckExtensionsBackup;
    Gtk::CheckButton m_CheckDconfBackup;
    Gtk::Button m_ButtonBackup;

    Gtk::Box m_VBoxRestore;
    Gtk::Label m_LabelRestoreInstruction;
    Gtk::Button m_ButtonSelectVault;
    Gtk::Label m_LabelSelectedVault;
    Gtk::CheckButton m_CheckThemesRestore;
    Gtk::CheckButton m_CheckIconsRestore;
    Gtk::CheckButton m_CheckWallpapersRestore;
    Gtk::CheckButton m_CheckExtensionsRestore;
    Gtk::CheckButton m_CheckDconfRestore;
    Gtk::CheckButton m_CheckDryRunRestore;
    Gtk::Button m_ButtonRestore;

    Glib::Dispatcher m_Dispatcher;
    std::mutex m_LogMutex;
    std::queue<std::string> m_LogQueue;
    std::atomic<bool> m_WorkerRunning;
    std::atomic<bool> m_JobSuccess; 
    std::atomic<bool> m_IsDebugMode;
    std::atomic<bool> m_LastJobWasRestore;
    std::string m_SelectedVaultPath;

    void on_button_backup_clicked();
    void on_button_restore_clicked();
    void on_button_select_vault_clicked();
    void on_dispatcher_ping();
    void add_log_ui(const std::string& message);
    void set_ui_locked(bool locked);
    void queue_log(const std::string& message);
    void scan_vault_contents(const std::string& path);
    void run_backup_job(JobConfig config, std::string home_dir);
    void run_restore_job(JobConfig config, std::string home_dir);
    bool create_tar_archive(const std::string& source_dir, const std::string& out_filename, bool strip_path);
    bool extract_tar_archive(const std::string& archive_path, const std::string& dest_dir, bool dry_run, bool use_absolute);
    std::string get_wallpaper_directory();
    void send_notification(const std::string& title, const std::string& body);
};
