<?php

// Ensure the file is being run within the WordPress environment
if ( !defined( 'ABSPATH' ) ) {
    exit;
    // Exit if accessed directly
}
function erbwp_register_settings() {
    add_option( 'erbwp_rating_label', 'Rating' );
    add_option( 'erbwp_submit_label', 'Submit' );
    add_option( 'erbwp_header_success_message', 'Your rating was successfully saved' );
    add_option( 'erbwp_success_label', 'Thanks for your rating' );
    add_option( 'erbwp_header_fail_label', 'Your rating was not saved' );
    add_option( 'erbwp_fail_label', 'Cannot save your rating. Please try it again later' );
    add_option( 'erbwp_modal_header_background_color', '#007bff' );
    add_option( 'erbwp_modal_header_color', '#ffffff' );
    add_option( 'erbwp_enabled_for', 'post' );
    add_option( 'erbwp_remind_rating', false );
    add_option( 'erbwp_header_remind_rating_label', 'We value your feedback! ⭐' );
    add_option( 'erbwp_remind_rating_message_label', 'If you enjoyed this post or found it helpful, please take a moment to rate it. Your feedback helps us improve and provide more of the content you love. Thank you!' );
    add_option( 'erbwp_send_email_notify_to_admin_when_new_rating', false );
    add_option( 'erbwp_send_email_notify_to_author_when_new_rating', false );
    add_option( 'erbwp_ratings_per_page', 10 );
    add_option( 'erbwp_sort_ratings_direction', 'Newest_First' );
    add_option( 'erbwp_hold_for_review_comment_with_x_links', 2 );
    add_option( 'erbwp_hold_for_review_comment_with_x_links_message', 'Your comment has been held for review by the admin/author due to the presence of ' . get_option( 'erbwp_hold_for_review_comment_with_x_links' ) . ' or more links. It will be published once it has been reviewed. Thank you for your understanding.' );
    add_option( 'erbwp_avatar_url_default', '' );
    add_option( 'erbwp_blacklist_keyword', '' );
    add_option( 'erbwp_hold_for_review_comment_with_blacklist_keyword_message', 'Your comment has been held for review by the admin/author due to contain the blacklist keyword. It will be published once it has been reviewed. Thank you for your understanding.' );
    register_setting( 'erbwp_rating_settings', 'erbwp_rating_label', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_submit_label', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_header_success_message', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_success_label', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_header_fail_label', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_fail_label', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_modal_header_background_color', 'sanitize_hex_color' );
    register_setting( 'erbwp_rating_settings', 'erbwp_modal_header_color', 'sanitize_hex_color' );
    register_setting( 'erbwp_rating_settings', 'erbwp_enabled_for', 'sanitize_select_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_remind_rating', 'sanitize_boolean_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_header_remind_rating_label', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_remind_rating_message_label', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_send_email_notify_to_admin_when_new_rating', 'sanitize_boolean_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_send_email_notify_to_author_when_new_rating', 'sanitize_boolean_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_ratings_per_page', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_sort_ratings_direction', 'sanitize_select_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_hold_for_review_comment_with_x_links', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_hold_for_review_comment_with_x_links_message', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_avatar_url_default', 'sanitize_url' );
    register_setting( 'erbwp_rating_settings', 'erbwp_blacklist_keyword', 'sanitize_text_field' );
    register_setting( 'erbwp_rating_settings', 'erbwp_hold_for_review_comment_with_blacklist_keyword_message', 'sanitize_text_field' );
    add_settings_section(
        'erbwp_rating_section',
        'Rating Settings',
        'erbwp_rating_section_callback',
        'erbwp_settings_page'
    );
    add_settings_field(
        'erbwp_rating_label_field',
        'Rating Label',
        'erbwp_rating_label_field_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_header_success_message_label_field',
        'Header Success Message',
        'erbwp_header_success_message_field_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_success_message_label_field',
        'Success Message',
        'erbwp_success_message_field_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_header_fail_message_label_field',
        'Header Fail Message',
        'erbwp_header_fail_message_field_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_fail_message_label_field',
        'Fail Message',
        'erbwp_fail_message_field_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_submit_label_field',
        'Submit Label',
        'erbwp_submit_label_field_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_modal_header_background_color',
        'Modal Header Background Color',
        'erbwp_modal_header_background_color_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_modal_header_color',
        'Modal Header Text Color',
        'erbwp_modal_header_color_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_enabled_for',
        'Enable for',
        'erbwp_enabled_for_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_remind_rating',
        'Remind user rating',
        'erbwp_remind_rating_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_header_remind_rating_label',
        'Header remind user rating',
        'erbwp_header_remind_rating_label_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_remind_rating_message_label',
        'Message remind user rating',
        'erbwp_remind_rating_message_label_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_send_email_notify_to_admin_when_new_rating',
        'Send email notify to admin when new rating',
        'erbwp_send_email_notify_to_admin_when_new_rating_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_send_email_notify_to_author_when_new_rating',
        'Send email notify to author when new rating',
        'erbwp_send_email_notify_to_author_when_new_rating_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_ratings_per_page',
        'Ratings per page',
        'erbwp_ratings_per_page_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_sort_ratings_direction',
        'Sort ratings by',
        'erbwp_sort_ratings_direction_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_hold_for_review_comment_with_x_links',
        'Hold for Review: Comments with [X]+ Links',
        'erbwp_hold_for_review_comment_with_x_links_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_hold_for_review_comment_with_x_links_message',
        'Hold for review comment message with links exceed',
        'erbwp_hold_for_review_comment_with_x_links_message_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_avatar_url_default',
        'Avatar URL default',
        'erbwp_avatar_url_default_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_blacklist_keyword',
        'Blacklist keyword',
        'erbwp_blacklist_keyword_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
    add_settings_field(
        'erbwp_hold_for_review_comment_with_blacklist_keyword_message',
        'Hold for review comment message with blacklist keyword',
        'erbwp_hold_for_review_comment_with_blacklist_keyword_message_callback',
        'erbwp_settings_page',
        'erbwp_rating_section'
    );
}

function erbwp_rating_section_callback() {
    echo '<h3>Customize</h3>';
}

function erbwp_rating_label_field_callback() {
    $rating_label = get_option( 'erbwp_rating_label' );
    echo '<input type="text" id="erbwp_rating_label" name="erbwp_rating_label" value="' . esc_attr( $rating_label ) . '" />';
}

function erbwp_header_success_message_field_callback() {
    $success_message_label = get_option( 'erbwp_header_success_message' );
    echo '<input type="text" id="erbwp_header_success_message" name="erbwp_header_success_message" value="' . esc_attr( $success_message_label ) . '" />';
}

function erbwp_success_message_field_callback() {
    $success_message_label = get_option( 'erbwp_success_label' );
    echo '<input type="text" id="erbwp_success_label" name="erbwp_success_label" value="' . esc_attr( $success_message_label ) . '" />';
}

function erbwp_header_fail_message_field_callback() {
    $fail_message_label = get_option( 'erbwp_header_fail_label' );
    echo '<input type="text" id="erbwp_header_fail_label" name="erbwp_header_fail_label" value="' . esc_attr( $fail_message_label ) . '" />';
}

function erbwp_fail_message_field_callback() {
    $fail_message_label = get_option( 'erbwp_fail_label' );
    echo '<input type="text" id="erbwp_fail_label" name="erbwp_fail_label" value="' . esc_attr( $fail_message_label ) . '" />';
}

function erbwp_submit_label_field_callback() {
    $submit_label = get_option( 'erbwp_submit_label' );
    echo '<input type="text" id="erbwp_submit_label" name="erbwp_submit_label" value="' . esc_attr( $submit_label ) . '" />';
}

function erbwp_modal_header_background_color_callback() {
    $color = get_option( 'erbwp_modal_header_background_color' );
    echo '<input type="text" id="erbwp_modal_header_background_color" name="erbwp_modal_header_background_color" value="' . esc_attr( $color ) . '" class="my-color-field" data-default-color="#007bff" />';
}

function erbwp_modal_header_color_callback() {
    $color = get_option( 'erbwp_modal_header_color' );
    echo '<input type="text" id="erbwp_modal_header_color" name="erbwp_modal_header_color" value="' . esc_attr( $color ) . '" class="my-color-field" data-default-color="#ffffff" />';
}

function erbwp_enabled_for_callback() {
    $enabledFor = get_option( 'erbwp_enabled_for' );
    ?>
    <select name="erbwp_enabled_for">
        <option value="none" <?php 
    selected( $enabledFor, 'none' );
    ?>>None</option>
        <option value="post" <?php 
    selected( $enabledFor, 'post' );
    ?>>Post</option>
        <option value="page" <?php 
    selected( $enabledFor, 'page' );
    ?>>Page</option>
        <option value="both" <?php 
    selected( $enabledFor, 'both' );
    ?>>Both</option>
    </select>
    <?php 
}

function erbwp_remind_rating_callback() {
    echo '<input type="checkbox" id="erbwp_remind_rating" name="erbwp_remind_rating" value="1" ' . checked( 1, get_option( 'erbwp_remind_rating' ), false ) . '>';
}

function erbwp_header_remind_rating_label_callback() {
    $headerRemindRatingLabel = get_option( 'erbwp_header_remind_rating_label' );
    echo '<input type="text" id="erbwp_header_remind_rating_label" name="erbwp_header_remind_rating_label" value="' . esc_attr( $headerRemindRatingLabel ) . '" />';
}

function erbwp_remind_rating_message_label_callback() {
    $remindRatingMessageLabel = get_option( 'erbwp_remind_rating_message_label' );
    echo '<input type="text" id="erbwp_remind_rating_message_label" name="erbwp_remind_rating_message_label" value="' . esc_attr( $remindRatingMessageLabel ) . '" />';
}

function erbwp_send_email_notify_to_admin_when_new_rating_callback() {
    echo '<input type="checkbox" id="erbwp_send_email_notify_to_admin_when_new_rating" name="erbwp_send_email_notify_to_admin_when_new_rating" value="1" ' . checked( 1, get_option( 'erbwp_send_email_notify_to_admin_when_new_rating' ), false ) . '>';
}

function erbwp_send_email_notify_to_author_when_new_rating_callback() {
    echo '<input type="checkbox" id="erbwp_send_email_notify_to_author_when_new_rating" name="erbwp_send_email_notify_to_author_when_new_rating" value="1" ' . checked( 1, get_option( 'erbwp_send_email_notify_to_author_when_new_rating' ), false ) . '>';
}

function erbwp_ratings_per_page_callback() {
    $ratingPerPage = get_option( 'erbwp_ratings_per_page' );
    echo '<input type="number" id="erbwp_ratings_per_page" name="erbwp_ratings_per_page" value="' . esc_attr( $ratingPerPage ) . '" min="1" />';
}

function erbwp_hold_for_review_comment_with_x_links_callback() {
    $maxLinks = get_option( 'erbwp_hold_for_review_comment_with_x_links' );
    echo '<input type="number" id="erbwp_hold_for_review_comment_with_x_links" name="erbwp_hold_for_review_comment_with_x_links" value="' . esc_attr( $maxLinks ) . '" min="1" />';
}

function erbwp_hold_for_review_comment_with_x_links_message_callback() {
    $holdForReviewMessage = get_option( 'erbwp_hold_for_review_comment_with_x_links_message' );
    echo '<textarea id="erbwp_hold_for_review_comment_with_x_links_message" name="erbwp_hold_for_review_comment_with_x_links_message">' . esc_attr( $holdForReviewMessage ) . '</textarea>';
}

function erbwp_sort_ratings_direction_callback() {
    $sortRatingsBy = get_option( 'erbwp_sort_ratings_direction' );
    ?>
    <select name="erbwp_sort_ratings_direction">
        <option value="Newest_First" <?php 
    selected( $sortRatingsBy, 'Newest_First' );
    ?>>Newest First</option>
        <option value="Oldest_First" <?php 
    selected( $sortRatingsBy, 'Oldest_First' );
    ?>>Oldest First</option>
    </select>
    <?php 
}

function erbwp_avatar_url_default_callback() {
    $avatarURLDefault = get_option( 'erbwp_avatar_url_default' );
    echo '<input type="text" id="erbwp_avatar_url_default" name="erbwp_avatar_url_default" value="' . esc_attr( $avatarURLDefault ) . '" min="1" />';
}

function erbwp_blacklist_keyword_callback() {
    $blacklistKeyword = get_option( 'erbwp_blacklist_keyword' );
    echo '<textarea id="erbwp_blacklist_keyword" name="erbwp_blacklist_keyword" rows="5">' . esc_attr( $blacklistKeyword ) . '</textarea>';
}

function erbwp_hold_for_review_comment_with_blacklist_keyword_message_callback() {
    $holdForReviewMessage = get_option( 'erbwp_hold_for_review_comment_with_blacklist_keyword_message' );
    echo '<textarea id="erbwp_hold_for_review_comment_with_blacklist_keyword_message" name="erbwp_hold_for_review_comment_with_blacklist_keyword_message" rows="5">' . esc_attr( $holdForReviewMessage ) . '</textarea>';
}

add_action( 'admin_init', 'erbwp_register_settings' );
function erbwp_add_settings_menu_page() {
    add_options_page(
        'ERB Settings',
        // Page title
        'ERB Settings',
        // Menu title
        'manage_options',
        // Capability
        'erbwp-settings',
        // Menu slug
        'erbwp_settings_page_html'
    );
}

add_action( 'admin_menu', 'erbwp_add_settings_menu_page' );
// Function to display the HTML for the settings page
function erbwp_settings_page_html() {
    // Check user capabilities
    if ( !current_user_can( 'manage_options' ) ) {
        return;
    }
    ?>
    <div class="wrap">
        <h1><?php 
    esc_html( get_admin_page_title() );
    ?></h1>
        <form action="options.php" method="post">
        <?php 
    settings_fields( 'erbwp_rating_settings' );
    do_settings_sections( 'erbwp_settings_page' );
    submit_button( 'Save Changes' );
    if ( erbwp_fs()->is_not_paying() ) {
        echo '<section><h1>Awesome Premium Features <span id="premium-features-question-mark">?</span></h1>';
        echo '<a href="' . esc_attr( erbwp_fs()->get_upgrade_url() ) . '">Upgrade Now!</a>';
        echo '</section>';
    }
    ?>
        </form>
        <div class="modal fade" id="premiumFeatureModal" tabindex="-1" role="dialog" aria-labelledby="premiumFeatureModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="premiumFeatureModalLabel">Premium Feature</h5>
                    </div>
                    <div class="modal-body">
                        <ul>
                            <li>Enable Google reCAPTCHA</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php 
}

add_action( 'admin_enqueue_scripts', function ( $hook_suffix ) {
    wp_enqueue_style( 'wp-color-picker' );
    wp_enqueue_script(
        'erbwp-color-picker',
        plugins_url( '/js/erbwp-color-picker.js', __FILE__ ),
        array('wp-color-picker'),
        false,
        true
    );
} );