<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * The class for the admin-specific functionality of the plugin.
 */
class SIMPPONO_Admin
{
    /**
     * Initialize the class sets its properties.
     *
     * @since 1.1
     */
    function __construct()
    {
        $this->init_admin();
    } // __construct end;

    /**
     * Initialize the class sets its properties.
     *
     * @since 1.1
     */
    public function init_admin()
    {
        add_action('admin_enqueue_scripts', array($this, 'simppono_color_picker'));
        add_action('admin_menu', array($this, 'simppono_section_plugin_menu'));
        add_filter('plugin_row_meta', array($this, 'simppono_add_custom_plugin_links'), 10, 2);
        add_action('admin_init', array($this, 'simppono_section_settings'));
        add_filter('plugin_action_links', array($this, 'simppono_add_plugin_action_links'), 10, 2);
        add_action('admin_init', array($this, 'simppono_section_save_settings'));
        add_action('wp_ajax_get_image_html', array($this, 'simppono_get_image_html_callback'));
    } // init_admin(); end

    /**
	 * You can use these filters to add custom links to your plugin row in the plugin list.
	 * @since  1.1
	 * @param $links, $plugin_file
	 * @return $links [array]
	 */
    public function simppono_add_plugin_action_links( $links, $plugin_file ) {
		if( SIMPPONO_BASENAME == $plugin_file ){
            $settings_ui_links = sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=simppono-popup-section' ), __( 'Settings', 'simple-popup-notification' ) );
            array_unshift( $links, $settings_ui_links );
		}
		return $links;
	}

    /**
	 * You can use these filters to add custom links to your plugin row in the plugin list.
	 * @since  1.1
	 * @param $links, $file
	 * @return $links [array]
	 */
    public function simppono_add_custom_plugin_links($links, $file){

        if ($file === 'simple-popup-notification/simple-popup-notification.php') {
			$links[] = '<a href="https://wp-plugins.galaxyweblinks.com/wp-plugins/simple-popup-notification/doc/" target="_blank">Documentation</a>';
			$links[] = '<a href="https://wp-plugins.galaxyweblinks.com/contact/" target="_blank">Contact Support</a>';
		}
        return $links;
    }

    /**
     * Backend Script enqueue
     *
     * @since 1.1
     */
    public function simppono_color_picker()
    {
        wp_enqueue_media();
        wp_enqueue_style('wp-color-picker');
        wp_enqueue_script('color-picker', SIMPPONO_PLUGINURL . '/includes/admin/js/color-picker.js', array('wp-color-picker'), 1.1, true);
        wp_enqueue_style('popup-admin-style', SIMPPONO_PLUGINURL . '/includes/admin/css/simple-popup-notification-admin.css', array(), '1.1');
        wp_enqueue_script('spn-admin-ajax-script', SIMPPONO_PLUGINURL . '/includes/admin/js/custom-script.js', array('jquery'), 1.1, true);

        // Localize the script with new data
        wp_localize_script('spn-admin-ajax-script', 'admin_ajax_object', array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce'    => wp_create_nonce('fetch_media_nonce_action')
        ));
    } // simppono_color_picker(); end

    /**
     * simppono_section_plugin_menu
     *
     * @since 1.1
     */
    public function simppono_section_plugin_menu()
    {
        add_menu_page(
            'Simple Popup Notification',
            'Simple Popup Notification',
            'manage_options',
            'simppono-popup-section',
            array($this, 'simppono_section_page'),
            'dashicons-welcome-widgets-menus'
        );
    } // simppono_section_plugin_menu(); end

    /**
     * simppono_section_page
     *
     * @since 1.1
     */
    public function simppono_section_page()
    { ?>

        <div class="wrap">
            <div class="notice simple-popup-notification--notice">
                <div>
                    <h3><?php esc_attr_e('Simple Popup Notification', 'simple-popup-notification'); ?></h3>
                    <p><?php esc_html_e('Here`s a link to the demo and documentation for the plugin. This will help you learn more about its features and how to use it.', 'simple-popup-notification'); ?></p>
                    <div class="e-notice__actions">
                        <a href="https://wp-plugins.galaxyweblinks.com/wp-plugins/simple-popup-notification/doc/" class="e-button--cta cta-secondary" target="_blank"><span>Documentation</span></a>
                    </div>
                    <p class="e-note">For any feedback or queries regarding this plugin, please contact our <a href="https://wp-plugins.galaxyweblinks.com/contact/" target="_blank">Support team</a>.</p>
                </div>
            </div>

            <h1><?php esc_html_e('Simple Popup Notification', 'simple-popup-notification'); ?></h1>

            <div class="metabox-flex">
                <form method="post" action="options.php">
                    <div class="postbox-container" style="width: 55%;margin-right: 15px;">
                        <div class="postbox" style="padding:0 20px;">
                            <?php settings_fields('simppono-popup-section-settings'); ?>
                            <?php do_settings_sections('simppono-popup-section'); ?>
                            <?php submit_button(); ?>
                        </div>
                    </div>
                    <div class="postbox-container" style="width: 43%;">
                        <div class="postbox">
                            <div class="inside">
                                <h2><?php esc_html_e('Simple Popup Notification Instructions', 'simple-popup-notification'); ?></h2>
                                <div class="dpffm-howtouse">
                                    <p><?php esc_html_e('A lightweight plugin to display customizable notification popups on the frontend. Users can manage pop-up content and styles from the admin settings. Once closed, the pop-up will not reappear for the user until the cookie expires.', 'simple-popup-notification'); ?></p>
                                    <p><strong><?php esc_html_e('Select how long the cookie should last before the popup reappears.', 'simple-popup-notification'); ?></strong></p>
                                    <table class="form-table" role="presentation">
                                        <tbody>
                                            <tr>
                                                <th scope="row">Set Cookie Expire (In Day)</th>
                                                <td>
                                                    <?php $expiry = get_option('simppono_setcookieexpire', ''); ?>
                                                    <select name="simppono_setcookieexpire">
                                                        <option value="1" <?php selected($expiry, '1'); ?>>1 Day</option>
                                                        <option value="2" <?php selected($expiry, '2'); ?>>2 Days</option>
                                                        <option value="3" <?php selected($expiry, '3'); ?>>3 Days</option>
                                                        <option value="never" <?php selected($expiry, 'never'); ?>>Never</option>
                                                    </select>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>

                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    <?php
    } // simppono_section_page(); end

    /**
     * simppono_section_settings
     *
     * @since 1.1
     */
    public function simppono_section_settings()
    {

        add_settings_section(
            'simppono-popup-section-settings',
            'Popup Section Settings',
            array($this, 'simppono_popup_section_settings_description'),
            'simppono-popup-section'
        );

        // Add popup Enable field
        add_settings_field(
            'simppono_enabled',
            'Enable Popup',
            array($this, 'simppono_enabled_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add Background Color field
        add_settings_field(
            'simppono_background_color',
            'Background Color',
            array($this, 'simppono_background_field_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add Background Color field
        add_settings_field(
            'simppono_text_color',
            'Text Color',
            array($this, 'simppono_text_field_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add Heading field
        add_settings_field(
            'simppono_heading',
            'Heading',
            array($this, 'simppono_heading_field_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add Description field
        add_settings_field(
            'simppono_description',
            'Description',
            array($this, 'simppono_description_field_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add CTA Button field
        add_settings_field(
            'simppono_cta_button',
            'CTA Button Text',
            array($this, 'simppono_cta_button_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add CTA Button Link field
        add_settings_field(
            'simppono_cta_button_link',
            'CTA Button Link',
            array($this, 'simppono_cta_button_link_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add CTA Button Background Color field
        add_settings_field(
            'simppono_cta_button_bg_color',
            'CTA Button Background Color',
            array($this, 'simppono_cta_button_bg_color_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add CTA Button Text Color field
        add_settings_field(
            'simppono_cta_button_text_color',
            'CTA Button Text Color',
            array($this, 'simppono_cta_button_text_color_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add Image Upload field
        add_settings_field(
            'simppono_image_upload',
            'Upload Image',
            array($this, 'simppono_image_upload_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add Select Option (Dropdown) field
        add_settings_field(
            'simppono_select_option',
            'Select Image Position Option',
            array($this, 'simppono_select_option_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );

        // Add Select Option (Dropdown) field
        add_settings_field(
            'simppono_popup_width',
            'Popup Width <small>(optional)</small>',
            array($this, 'simppono_popup_width_callback'),
            'simppono-popup-section',
            'simppono-popup-section-settings'
        );
        // Add other settings fields similarly
    }

    /**
     * Register other settings fields similarly
     *
     * @since 1.1
     */
    public function simppono_section_save_settings()
    {
        register_setting('simppono-popup-section-settings', 'simppono_enabled', 'sanitize_text_field');
        register_setting('simppono-popup-section-settings', 'simppono_background_color', 'sanitize_hex_color');
        register_setting('simppono-popup-section-settings', 'simppono_text_color', 'sanitize_hex_color');
        register_setting('simppono-popup-section-settings', 'simppono_heading', 'sanitize_text_field');
        register_setting('simppono-popup-section-settings', 'simppono_description', 'sanitize_textarea_field');
        register_setting('simppono-popup-section-settings', 'simppono_cta_button', 'sanitize_text_field');
        register_setting('simppono-popup-section-settings', 'simppono_cta_button_link', 'esc_url_raw');
        register_setting('simppono-popup-section-settings', 'simppono_cta_button_bg_color', 'sanitize_hex_color');
        register_setting('simppono-popup-section-settings', 'simppono_cta_button_text_color', 'sanitize_hex_color');
        register_setting('simppono-popup-section-settings', 'simppono_image_upload', 'sanitize_text_field');
        register_setting('simppono-popup-section-settings', 'simppono_select_option', 'sanitize_key');
        register_setting('simppono-popup-section-settings', 'simppono_popup_width', 'sanitize_text_field');
        register_setting('simppono-popup-section-settings', 'simppono_setcookieexpire', 'sanitize_text_field');
    }

    /**
     * simppono_get_image_html_callback
     *
     * @since 1.1
     */
    public function simppono_get_image_html_callback()
    {

        // Verify the nonce
        check_ajax_referer('fetch_media_nonce_action', 'nonce');

        // Check if the attachment ID is set
        if (isset($_POST['attachment_id'])) {
            $attachment_id = intval($_POST['attachment_id']);

            // Get the image HTML using wp_get_attachment_image()
            $image_html = wp_get_attachment_image($attachment_id, 'full'); // You can change 'full' to any other size

            // Return the image HTML
            echo wp_kses_post($image_html);
        }

        wp_die(); // Required to terminate immediately and return a proper response
    }

    /**
     * simppono_popup_section_settings_description
     *
     * @since 1.1
     */
    function simppono_popup_section_settings_description()
    {
        echo '<p>' . esc_html_e('Configure the general settings for your popup notification.', 'simple-popup-notification') . '</p>';
    }

    /**
     * simppono_enabled_callback
     *
     * @since 1.1
     */
    function simppono_enabled_callback()
    {
        $simppono_enabled = get_option('simppono_enabled', 'off');
        echo '<input type="checkbox" name="simppono_enabled" value="on" ' . checked('on', $simppono_enabled, false) . '>';
    }

    /**
     * simppono_background_field_callback
     *
     * @since 1.1
     */
    function simppono_background_field_callback()
    {
        $background_color = get_option('simppono_background_color', '');
        echo '<input type="text" class="color-picker" name="simppono_background_color" value="' . esc_attr($background_color) . '" />';
    }

    /**
     * simppono_text_field_callback
     *
     * @since 1.1
     */
    function simppono_text_field_callback()
    {
        $text_color = get_option('simppono_text_color', '');
        echo '<input type="text" class="color-picker" name="simppono_text_color" value="' . esc_attr($text_color) . '" />';
    }

    /**
     * simppono_heading_field_callback
     *
     * @since 1.1
     */
    function simppono_heading_field_callback()
    {
        $heading = get_option('simppono_heading');
        echo '<input type="text" name="simppono_heading" size="46" value="' . esc_attr($heading) . '" />';
    }

    /**
     * simppono_description_field_callback
     *
     * @since 1.1
     */
    function simppono_description_field_callback()
    {
        $description = get_option('simppono_description', '');
        echo '<textarea name="simppono_description" rows="4" cols="50"> ' . esc_textarea($description) . ' </textarea>';
    }

    /**
     * simppono_cta_button_callback
     *
     * @since 1.1
     */
    function simppono_cta_button_callback()
    {
        $cta_button_text = get_option('simppono_cta_button', '');
        echo '<input type="text" name="simppono_cta_button" size="46" value="' . esc_attr($cta_button_text) . '" />';
    }

    /**
     * simppono_cta_button_link_callback
     *
     * @since 1.1
     */
    function simppono_cta_button_link_callback()
    {
        $cta_button_link = get_option('simppono_cta_button_link', '');
        echo '<input type="text" name="simppono_cta_button_link" size="46" value="' . esc_attr($cta_button_link) . '" />';
    }

    /**
     * simppono_cta_button_bg_color_callback
     *
     * @since 1.1
     */
    function simppono_cta_button_bg_color_callback()
    {
        $cta_bg_color = get_option('simppono_cta_button_bg_color', '');
        echo '<input type="text" name="simppono_cta_button_bg_color" value="' . esc_attr($cta_bg_color) . '" class="color-picker"/>';
    }

    /**
     * simppono_cta_button_text_color_callback
     *
     * @since 1.1
     */
    function simppono_cta_button_text_color_callback()
    {
        $cta_text_color = get_option('simppono_cta_button_text_color', '');
        echo '<input type="text" name="simppono_cta_button_text_color" value="' . esc_attr($cta_text_color) . '" class="color-picker" />';
    }

    /**
     * simppono_image_upload_callback
     *
     * @since 1.1
     */
    function simppono_image_upload_callback()
    {
        $attachment_image_id = get_option('simppono_image_upload', '');
        $image_attributes = '';
        if (isset($attachment_image_id) && !empty($attachment_image_id)) {
            $image_attributes = wp_get_attachment_image($attachment_image_id, 'thumbnail'); // 'full' for original size, or use other sizes
        }
        ?>
        <p class="preview-img" data-media-id="<?php echo esc_attr($attachment_image_id); ?>"><?php echo wp_kses_post($image_attributes); ?></p>
        <input type="hidden" id="popup_image_url" name="simppono_image_upload" value="<?php echo esc_attr($attachment_image_id); ?>" />
        <input type="file" id="upload_image_button" value="Upload Image" />
        <?php
    }

    /**
     * simppono_popup_width_callback
     *
     * @since 1.1
     */
    public function simppono_popup_width_callback()
    {
        $width_option = get_option('simppono_popup_width', '');
        echo '<input type="number" name="simppono_popup_width" value="' . esc_attr($width_option) . '" class="popupwidth" />px';
        echo '<br/><small>default : 1000px</small>';

    } // simppono_popup_width_callback(); end

    /**
     * simppono_select_option_callback
     *
     * @since 1.1
     */
    public function simppono_select_option_callback()
    {
        $selected_option = get_option('simppono_select_option', '');
        $options = array(
            'left_image' => 'Left Image',
            'right_image' => 'Right Image',
        );
        echo '<select name="simppono_select_option" style="width: 100%;">';
        foreach ($options as $value => $label) {
            echo '<option value="' . esc_attr($value) . '" ' . selected($selected_option, $value, false) . '>' . esc_html($label) . '</option>';
        }
        echo '</select>';
    } // simppono_select_option_callback(); end

} // class SIMPPONO_Admin end

$admin = new SIMPPONO_Admin();
