/*
 * Copyright © 2024 Joel Winarske
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <csignal>

#include <cxxopts.hpp>

#include "app.h"

static volatile bool gRunning = true;

/**
 * @brief Signal handler function to handle signals.
 *
 * This function is a signal handler for handling signals. It sets the value of
 * keep_running to false, which will stop the program from running. The function
 * does not take any input parameters.
 *
 * @param signal The signal number. This parameter is not used by the function.
 *
 * @return void
 */
void handle_signal(const int signal) {
    if (signal == SIGINT) {
        gRunning = false;
    }
}

int main(const int argc, char **argv) {
    std::signal(SIGINT, handle_signal);

    cxxopts::Options options("view-manager", "Example View Manager");
    options.add_options()
            // clang-format off
            ("w,width", "Set width", cxxopts::value<int>()->default_value("512"))
            ("h,height", "Set height", cxxopts::value<int>()->default_value("512"))
            ("c,disable-cursor", "Disable Cursor")
            ("f,fullscreen", "Run in fullscreen mode")
            ("m,maximized", "Run in maximized mode")
            ("r,fullscreen-ratio", "Fullscreen Ratio")
            ("t,tearing", "Enable tearing via the tearing_control protocol");
    // clang-format on

    const auto result = options.parse(argc, argv);

    App app({
                    .width = result["width"].as<int>(),
                    .height = result["height"].as<int>(),
                    .disable_cursor = result["disable-cursor"].as<bool>(),
                    .fullscreen = result["fullscreen"].as<bool>(),
                    .maximized = result["maximized"].as<bool>(),
                    .fullscreen_ratio = result["fullscreen-ratio"].as<bool>(),
                    .tearing = result["tearing"].as<bool>(),
            });

    while (gRunning && app.run()) {}

    return EXIT_SUCCESS;
}