<?php
namespace OxygenApp\Utils;
if ( ! defined( 'ABSPATH' ) ) exit;

use OxygenApp\Config\Config;
use OxygenApp\Enums\FrontendDataType;
use OxygenApp\Utils\OptionUtils;
use OxygenApp\Utils\UserUtil;
use OxygenApp\Utils\BackgroundUtils;
use OxygenApp\Utils\AdminPlaylistUtils;
use OxygenApp\Utils\FrontendLabels;
use OxygenApp\Models\BackgroundCategory;
use OxygenApp\Utils\LanguagesHelper;

class FrontendData {

    private static $instance = null;
    private $data = [];

    function __construct() {

        $recaptcha_site_key = trim(OptionUtils::getInstance()->getOption('recaptcha_site_key'));
        $recaptcha_site_secret = trim(OptionUtils::getInstance()->getOption('recaptcha_site_secret'));
        $isRecaptchaActive = OptionUtils::getInstance()->getOption('is_recaptcha_active');
        $isRecaptchaActive = $isRecaptchaActive && strlen($recaptcha_site_key) > 0 && strlen($recaptcha_site_secret) > 0;
        $max_playlists = OptionUtils::getInstance()->getOption('max_playlists_free_users', 10);
        $max_sounds_in_playlist = OptionUtils::getInstance()->getOption('max_sounds_in_playlist_free_users', 10);
        $show_playlist_names = OptionUtils::getInstance()->getOption('show_playlist_names');
        $show_user_time_date = OptionUtils::getInstance()->getOption('show_user_time_date');
        $allow_free_users_to_setup_daily_sleep_time = OptionUtils::getInstance()->getOption('allow_free_users_to_setup_daily_sleep_time');
        $default_daily_sleep_time = '22:00'; // Default to 10:00 PM
        $show_backgrounds_credits = OptionUtils::getInstance()->getOption('show_backgrounds_credits');

        $this->data = [
            'routes' => Config::getInstance()->getFrontendRoutes(),
            'rest_root_url' => rtrim(get_rest_url(), '/'),
            'is_recaptcha_active' => $isRecaptchaActive,
            'recaptcha_site_key' => $recaptcha_site_key,
            'app_url' => self::getAppPageUrl(),
            'user_data' => UserUtil::getInstance()->getUserFromSession(),
            'default_background' => BackgroundUtils::getSysDefaultBackground(),
            'user_background' => BackgroundUtils::getUserDefaultBackground(),
            'app_rest_namespace' => Config::getInstance()->getSetting('app_rest_namespace'),
            'sounds_collections' => AdminPlaylistUtils::getPlaylists(),
            'background_collections' => BackgroundCategory::getAllPretty(),
            'fe_labels' => FrontendLabels::getFrontendlabels(),
            'max_playlists_free_users' => absint($max_playlists),
            'max_sounds_in_playlist_free_users' => absint($max_sounds_in_playlist),
            'all_backgrounds' => BackgroundUtils::getBackgrounds(),
            'show_playlist_names' => esc_html($show_playlist_names),
            'show_user_time_date' => esc_html($show_user_time_date),
            'allow_free_users_to_setup_daily_sleep_time' => esc_html($allow_free_users_to_setup_daily_sleep_time),
            'default_daily_sleep_time' => esc_html($default_daily_sleep_time),
            'current_lang_code' => LanguagesHelper::getInstance()->getCurrentLanguageCode(),
            'show_backgrounds_credits' => $show_backgrounds_credits === 'on',
        ];
    }

    function getDefaultSettings(FrontendDataType $type = FrontendDataType::ARRAY): mixed {
        return match ($type) {
            FrontendDataType::JSON => json_encode($this->data),
            FrontendDataType::ARRAY => $this->data
        };
    }

    static function getAppPageUrl(): string {
        $isHomepageApp = OptionUtils::getInstance()->getOption('is_homepage_app');
        $baseRedirect = '';
        if ($isHomepageApp) {
            $baseRedirect = rtrim(site_url(), '/');
        } else {
            $appPageId = OptionUtils::getInstance()->getOption('app_page_id');
            $baseRedirect = rtrim(get_the_permalink($appPageId), '/');
        }
        return $baseRedirect;
    }

    static function getGoogleOAuthCallbackUrl(): string {
        $googleAuthCallbackUrl = rtrim(get_rest_url(), '/') . '/oxygen-app/v1/handle-google-auth-callback';
        return esc_url($googleAuthCallbackUrl);
    }

    static function isGoogleSignInActive() {
        $is_google_signin_active = OptionUtils::getInstance()->getOption('is_google_signin_active');
        $google_client_id = trim(OptionUtils::getInstance()->getOption('google_client_id'));
        $google_client_secret = trim(OptionUtils::getInstance()->getOption('google_client_secret'));

        return $is_google_signin_active === 'on' && strlen($google_client_id) > 0 && strlen($google_client_secret) > 0;
    }

    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new FrontendData();
        }
        return self::$instance;
    }
}