<?php

namespace OxygenApp\Views\Admin\Settings\General;
if ( ! defined( 'ABSPATH' ) ) exit;

use OxygenApp\Views\BaseView;
use OxygenApp\Views\IView;
use OxygenApp\Utils\OptionUtils;
use OxygenApp\Utils\FrontendData;
use OxygenApp\Models\Background;

class GeneralSettings extends BaseView implements IView {
    public function render(): void
    {
        $options_group = OptionUtils::OPTION_GROUP_SLUG;
        $pages = get_pages(['post_type' => 'page']);
        $appPageId = OptionUtils::getInstance()->getOption('app_page_id');
        $isHomepageApp = OptionUtils::getInstance()->getOption('is_homepage_app');

        ?>
        <div class="row">
            <div class="col-md-12 mb-2">
                <h5><?php esc_html_e('Documentation', 'oxyleaf-app-working-and-relaxing-app'); ?></h5>
                <div class="alert alert-info" role="alert">
                    <span><?php esc_html_e("Full documentation can be found at", 'oxyleaf-app-working-and-relaxing-app') ?> <a href="https://sakuraleaf.com/showcase/oxygen/documentation/" target="_blank"><?php esc_html_e("OxygenApp Docs", 'oxyleaf-app-working-and-relaxing-app') ?></a></span>.
                    <span><?php esc_html_e("Pro version can be found at", 'oxyleaf-app-working-and-relaxing-app') ?> <a href="https://sakuraleaf.com/products/oxygen-working-and-relaxing-app-saas-ready/" target="_blank"><?php esc_html_e("OxygenApp Pro", 'oxyleaf-app-working-and-relaxing-app') ?></a></span>. 
                    <span><?php esc_html_e("Demo can be found at", 'oxyleaf-app-working-and-relaxing-app') ?> <a href="https://sakuraleaf.com/showcase/oxygen/" target="_blank"><?php esc_html_e("OxygenApp Showcase", 'oxyleaf-app-working-and-relaxing-app') ?></a></span>
                </div>
            </div>

            <div><hr></div>

            <div class="col-md-12 mb-2">
                <h5><?php esc_html_e('Frontend page', 'oxyleaf-app-working-and-relaxing-app'); ?></h5>
            </div>
            <div class="col-md-6">
                <?php if (is_array($pages) && sizeof($pages) > 0): ?>
                    <select name="<?php echo esc_attr("{$options_group}[app_page_id]") ?>" class="form-select wd_100" style="width: 100%;">
                        <option value=""><?php esc_html_e('Select page ...', 'oxyleaf-app-working-and-relaxing-app'); ?></option>
                        <?php foreach ($pages as $page): ?>
                            <option value="<?php echo esc_attr($page->ID); ?>" <?php echo((int) $appPageId === $page->ID ? 'selected' : '') ?>><?php echo esc_html($page->post_title) ?></option>
                        <?php endforeach; ?>
                    </select>
                <?php endif; ?>

                <div class="mt-4">
                    <input type="checkbox" id="is-homepage-app" name="<?php echo esc_attr("{$options_group}[is_homepage_app]") ?>" value="on" <?php echo $isHomepageApp === 'on' ? esc_attr('checked') : '' ?>>
                    <label for="is-homepage-app"> <?php esc_html_e('Web App displays on the homepage', 'oxyleaf-app-working-and-relaxing-app'); ?></label><br>
                    <div class="option-entry-info"><?php esc_html_e("Renders the web app on the homepage (it's not required to modify the WP's reading settings).", 'oxyleaf-app-working-and-relaxing-app'); ?></div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="alert alert-info" role="alert">
                    <?php esc_html_e("Select the page that will render the web app within the front end.", 'oxyleaf-app-working-and-relaxing-app') ?>
                </div>
            </div>
            <div class="col-md-12 mt-3">
                <?php $this->renderSettingsSaveBtn() ?>
            </div>

            <div><hr></div>

            <div class="col-md-12 mb-2">
                <h5><?php esc_html_e('Cloudflare Turnstile', 'oxyleaf-app-working-and-relaxing-app'); ?></h5>
                <?php
                $isRecaptchaActive = OptionUtils::getInstance()->getOption('is_recaptcha_active');
                $recaptcha_site_key = trim(OptionUtils::getInstance()->getOption('recaptcha_site_key'));
                $recaptcha_site_secret = trim(OptionUtils::getInstance()->getOption('recaptcha_site_secret'));
                ?>

                <div class="mt-4 mb-3">
                    <div class="alert alert-info" role="alert">
                        <?php esc_html_e("Cloudflare Turnstile is a privacy-friendly CAPTCHA alternative that verifies whether a visitor is human without requiring them to solve puzzles. It helps websites reduce spam and bot traffic while improving user experience and maintaining strong privacy standards.", 'oxyleaf-app-working-and-relaxing-app') ?>
                    </div>
                </div>

                <div class="mb-3">
                    <input type="checkbox" id="is_recaptcha_active" name="<?php echo esc_attr("{$options_group}[is_recaptcha_active]") ?>" value="on" <?php echo $isRecaptchaActive === 'on' ? esc_attr('checked') : '' ?>>
                    <label for="is_recaptcha_active"> <?php esc_html_e('Activate Cloudflare Turnstile', 'oxyleaf-app-working-and-relaxing-app'); ?></label><br>
                    <div class="option-entry-info"><?php esc_html_e("Recommended for security reasons, will be used within the Sign-Up page.", 'oxyleaf-app-working-and-relaxing-app'); ?></div>
                </div>

                <div class="mb-3">
                    <div class="input-group">
                        <label for="recaptcha_site_key" class="form-label"><?php esc_html_e('Turnstile site key', 'oxyleaf-app-working-and-relaxing-app'); ?></label>
                        <input type="text" name="<?php echo esc_attr("{$options_group}[recaptcha_site_key]") ?>" style="width: 100%"
                        class="form-control skp-lang-input"
                        id="recaptcha_site_key"
                        value="<?php echo esc_attr($recaptcha_site_key) ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <div class="input-group">
                        <label for="recaptcha_site_secret" class="form-label"><?php esc_html_e('Turnstile secret key', 'oxyleaf-app-working-and-relaxing-app'); ?></label>
                        <input type="text" name="<?php echo esc_attr("{$options_group}[recaptcha_site_secret]") ?>" style="width: 100%"
                        class="form-control skp-lang-input"
                        id="recaptcha_site_secret"
                        value="<?php echo esc_attr($recaptcha_site_secret) ?>">
                    </div>
                </div>
            </div>

            <div class="col-md-12 mt-3">
                <?php $this->renderSettingsSaveBtn() ?>
            </div>

            <div><hr></div>

            <div class="col-md-12 mb-2">
                <h5><?php esc_html_e('Google Sign-In', 'oxyleaf-app-working-and-relaxing-app'); ?></h5>
                <?php
                $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'));
                ?>
                <div class="mt-4 mb-3">
                    <input type="checkbox" id="is_google_signin_active" name="<?php echo esc_attr("{$options_group}[is_google_signin_active]") ?>" value="on" <?php echo $is_google_signin_active === 'on' ? esc_attr('checked') : '' ?>>
                    <label for="is_google_signin_active"> <?php esc_html_e('Activate Google Sign-In', 'oxyleaf-app-working-and-relaxing-app'); ?></label><br>
                    <div class="option-entry-info"><?php esc_html_e("Allows visitors to sign in with Google.", 'oxyleaf-app-working-and-relaxing-app'); ?></div>
                </div>

                <div class="mb-3">
                    <div class="input-group">
                        <label for="google_client_id" class="form-label"><?php esc_html_e('Client Id', 'oxyleaf-app-working-and-relaxing-app'); ?></label>
                        <input type="text" name="<?php echo esc_attr("{$options_group}[google_client_id]") ?>" style="width: 100%"  
                        class="form-control skp-lang-input" 
                        id="google_client_id" 
                        value="<?php echo esc_attr($google_client_id) ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <div class="input-group">
                        <label for="google_client_secret" class="form-label"><?php esc_html_e('Client secret', 'oxyleaf-app-working-and-relaxing-app'); ?></label>
                        <input type="text" name="<?php echo esc_attr("{$options_group}[google_client_secret]") ?>" style="width: 100%"  
                        class="form-control skp-lang-input" 
                        id="google_client_secret" 
                        value="<?php echo esc_attr($google_client_secret) ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <div><label><?php esc_html_e('Authorized redirect URI', 'oxyleaf-app-working-and-relaxing-app'); ?></label></div>
                    <?php if (FrontendData::getGoogleOAuthCallbackUrl() === ''): ?>
                        <div class="alert alert-warning mt-2" role="alert">
                            <?php esc_html_e('Please setup the Frontpage above.', 'oxyleaf-app-working-and-relaxing-app') ?>
                        </div>
                    <?php endif; ?>
                    <div><small style="background-color: #F2F2F2;"><?php echo esc_url(FrontendData::getGoogleOAuthCallbackUrl()) ?></small></div>
                </div>
            </div>

            <div class="col-md-12 mt-3">
                <?php $this->renderSettingsSaveBtn() ?>
            </div>

            <div><hr></div>

            <div class="col-md-6 mb-2">
                <?php
                $default_background = OptionUtils::getInstance()->getOption('default_background', '');
                $freeBackgrounds = Background::getAll(['onlyFree' => true]);
                
                ?>
                <h5><?php esc_html_e('Background settings', 'oxyleaf-app-working-and-relaxing-app'); ?></h5>
                <div class="mt-4 mb-3">
                    <div><?php esc_html_e("Select default free background", "oxyleaf-app-working-and-relaxing-app") ?></div>
                    <?php if (is_array($freeBackgrounds) && sizeof($freeBackgrounds) > 0): ?>
                        <div class="alert alert-primary mt-2" role="alert"><?php esc_html_e("For non-logged-in visitors and free users, the app will start with the default background, otherwise will display a random free background.", "oxyleaf-app-working-and-relaxing-app") ?></div>
                        <select class="form-select default-background-select" name="<?php echo esc_attr("{$options_group}[default_background]") ?>">
                            <option value=""><?php echo esc_html_e("Select background", "oxyleaf-app-working-and-relaxing-app") ?>...</option>
                            <?php foreach ($freeBackgrounds as $key => $backgroundPost): ?>
                                <?php
                                $selected = $default_background === (string) $backgroundPost->ID ? 'selected' : '';                                
                                ?>
                                <option value="<?php echo esc_attr($backgroundPost->ID) ?>" <?php echo esc_attr($selected) ?>><?php echo esc_html($backgroundPost->post_title) ?></option>
                            <?php endforeach; ?>
                        </select>
                    <?php else: ?>
                        <div class="alert alert-warning mt-2" role="alert"><?php esc_html_e("There are no free backgrounds", "oxyleaf-app-working-and-relaxing-app") ?></div>
                    <?php endif; ?>
                </div>
                <?php
                $show_backgrounds_credits = OptionUtils::getInstance()->getOption('show_backgrounds_credits');
                ?>
                <div class="mt-4 mb-3">
                    <input type="checkbox" id="show_backgrounds_credits" name="<?php echo esc_attr("{$options_group}[show_backgrounds_credits]") ?>" value="on" <?php echo $show_backgrounds_credits === 'on' ? esc_attr('checked') : '' ?>>
                    <label for="show_backgrounds_credits"> <?php esc_html_e('Show background credits link', 'oxyleaf-app-working-and-relaxing-app'); ?></label><br>
                    <div class="option-entry-info"><?php esc_html_e("Display credits links in the frontend for backgrounds.", 'oxyleaf-app-working-and-relaxing-app'); ?></div>
                </div>
            </div>

            <div class="col-md-12 mt-3">
                <?php $this->renderSettingsSaveBtn() ?>
            </div>


            <div><hr></div>

            <div class="col-md-12 mb-2">
                <h5><?php esc_html_e('User settings', 'oxyleaf-app-working-and-relaxing-app'); ?></h5>
                <?php
                $max_playlists_free_users = OptionUtils::getInstance()->getOption('max_playlists_free_users', 3);
                $max_playlists_pro_users = OptionUtils::getInstance()->getOption('max_playlists_pro_users', 10);
                $max_sounds_in_playlist_free_users = OptionUtils::getInstance()->getOption('max_sounds_in_playlist_free_users', 4);
                $max_sounds_in_playlist_pro_users = OptionUtils::getInstance()->getOption('max_sounds_in_playlist_pro_users', 9);
                ?>

                <div class="row">
                    <div class="col-md-6">
                        <div class="mb-3">
                            <div class="input-group">
                                <label for="max_playlists_free_users" class="form-label"><?php esc_html_e('Users max playlists', 'oxyleaf-app-working-and-relaxing-app'); ?></label>
                                <input type="number" min="1" max="100" name="<?php echo esc_attr("{$options_group}[max_playlists_free_users]") ?>" style="width: 100%"  
                                class="form-control skp-lang-input" 
                                id="max_playlists_free_users" 
                                value="<?php echo esc_attr($max_playlists_free_users) ?>">
                            </div>
                        </div>

                        <?php
                        $show_playlist_names = OptionUtils::getInstance()->getOption('show_playlist_names');
                        ?>
                        <div class="mb-3">
                            <input type="checkbox" id="show_playlist_names" name="<?php echo esc_attr("{$options_group}[show_playlist_names]") ?>" value="on" <?php echo $show_playlist_names === 'on' ? esc_attr('checked') : '' ?>>
                            <label for="show_playlist_names"> <?php esc_html_e('Show playlist names', 'oxyleaf-app-working-and-relaxing-app'); ?></label><br>
                            <div class="option-entry-info"><?php esc_html_e("Show playlist names within users play sounds view.", 'oxyleaf-app-working-and-relaxing-app'); ?></div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="mb-3">
                            <div class="input-group">
                                <label for="max_sounds_in_playlist_free_users" class="form-label"><?php esc_html_e('Max sounds in playlist', 'oxyleaf-app-working-and-relaxing-app'); ?></label>
                                <input type="number" min="1" max="100" name="<?php echo esc_attr("{$options_group}[max_sounds_in_playlist_free_users]") ?>" style="width: 100%"  
                                class="form-control skp-lang-input" 
                                id="max_sounds_in_playlist_free_users" 
                                value="<?php echo esc_attr($max_sounds_in_playlist_free_users) ?>">
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="mb-3">
                            <?php
                            $show_user_time_date = OptionUtils::getInstance()->getOption('show_user_time_date');
                            ?>
                            <input type="checkbox" id="show_user_time_date" name="<?php echo esc_attr("{$options_group}[show_user_time_date]") ?>" value="on" <?php echo $show_user_time_date === 'on' ? esc_attr('checked') : '' ?>>
                            <label for="show_user_time_date"> <?php esc_html_e('Show user time and date by default', 'oxyleaf-app-working-and-relaxing-app'); ?></label><br>
                            <div class="option-entry-info"><?php esc_html_e("Users can disable it from their dashboard.", 'oxyleaf-app-working-and-relaxing-app'); ?></div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="mb-3">
                            <?php
                            $allow_free_users_to_setup_daily_sleep_time = OptionUtils::getInstance()->getOption('allow_free_users_to_setup_daily_sleep_time');
                            ?>
                            <input type="checkbox" id="allow_free_users_to_setup_daily_sleep_time" name="<?php echo esc_attr("{$options_group}[allow_free_users_to_setup_daily_sleep_time]") ?>" value="on" <?php echo $allow_free_users_to_setup_daily_sleep_time === 'on' ? esc_attr('checked') : '' ?>>
                            <label for="allow_free_users_to_setup_daily_sleep_time"> <?php esc_html_e('Allow users to setup daily sleep time', 'oxyleaf-app-working-and-relaxing-app'); ?></label><br>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-12 mt-3">
                <?php $this->renderSettingsSaveBtn() ?>
            </div>

            <div><hr></div>

            <div class="col-md-12 mb-2">
                <h5><?php esc_html_e('Support', 'oxyleaf-app-working-and-relaxing-app'); ?></h5>
                <?php
                $support_url = OptionUtils::getInstance()->getOption('support_url', '');
                ?>

                <div class="row">
                    <div class="col-md-6">
                        <div class="mb-3">
                            <div class="input-group">
                                <label for="max_playlists_free_users" class="form-label"><?php esc_html_e('Support URL (Used in user main menu)', 'oxyleaf-app-working-and-relaxing-app'); ?></label>
                                <input type="text" name="<?php echo esc_attr("{$options_group}[support_url]") ?>" style="width: 100%"  
                                class="form-control skp-lang-input" 
                                id="support_url" 
                                value="<?php echo esc_url($support_url) ?>">
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-12 mt-3">
                <?php $this->renderSettingsSaveBtn() ?>
            </div>

            <div><hr></div>

            <div class="col-md-12 mb-2">
                <div class="row">
                    <div class="col-md-6">
                        <div class="mb-3">
                            <?php
                            $clean_up_on_uninstall = OptionUtils::getInstance()->getOption('clean_up_on_uninstall');
                            ?>
                            <input type="checkbox" id="clean_up_on_uninstall" name="<?php echo esc_attr("{$options_group}[clean_up_on_uninstall]") ?>" value="on" <?php echo $clean_up_on_uninstall === 'on' ? esc_attr('checked') : '' ?>>
                            <label for="clean_up_on_uninstall"> <?php esc_html_e('Delete all plugin data on uninstall', 'oxyleaf-app-working-and-relaxing-app'); ?></label><br>
                        </div>
                        <div class="mt-4 mb-3">
                            <div class="alert alert-info" role="alert">
                                <?php esc_html_e("Not recommended if you plan to use the Pro version and don't want to lose all data (users, sounds, backgrounds ...).", 'oxyleaf-app-working-and-relaxing-app') ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-12 mt-3">
                <?php $this->renderSettingsSaveBtn() ?>
            </div>

        </div>
        <?php
    }
}