<?php
/**
 * Plugin Name: Scroll to Top
 * Description: A customizable scroll-to-top button that appears when visitors scroll down the page.
 * Version: 1.0.1
 * Author: WordPress
 * Text Domain: scroll-to-top
 * License: GPL v2 or later
 */

defined('ABSPATH') || exit;

class Scroll_To_Top {
    private static $instance = null;
    public static $plugin_dir;
    public static $plugin_url;

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

    public function __construct() {
        self::$plugin_dir = plugin_dir_path(__FILE__);
        self::$plugin_url = plugin_dir_url(__FILE__);

        add_action('admin_menu', array($this, 'add_admin_menu'));
        add_action('admin_init', array($this, 'register_settings'));
        add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_assets'));
        add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets'));
    }

    public function add_admin_menu() {
        add_options_page(
            'Scroll to Top Settings',
            'Scroll to Top',
            'manage_options',
            'scroll-to-top-settings',
            array($this, 'render_settings_page')
        );
    }

    public function register_settings() {
        register_setting('scroll_to_top_options', 'scroll_to_top_style', array(
            'default' => 'circle',
            'sanitize_callback' => 'sanitize_text_field'
        ));

        register_setting('scroll_to_top_options', 'scroll_to_top_bg_color', array(
            'default' => '#333333',
            'sanitize_callback' => 'sanitize_hex_color'
        ));

        register_setting('scroll_to_top_options', 'scroll_to_top_text_color', array(
            'default' => '#ffffff',
            'sanitize_callback' => 'sanitize_hex_color'
        ));

        register_setting('scroll_to_top_options', 'scroll_to_top_size', array(
            'default' => 'medium',
            'sanitize_callback' => 'sanitize_text_field'
        ));

        register_setting('scroll_to_top_options', 'scroll_to_top_position', array(
            'default' => 'bottom-right',
            'sanitize_callback' => 'sanitize_text_field'
        ));

        register_setting('scroll_to_top_options', 'scroll_to_top_show_mobile', array(
            'default' => '1',
            'sanitize_callback' => 'rest_sanitize_boolean'
        ));
    }

    public function render_settings_page() {
        if (!current_user_can('manage_options')) {
            return;
        }

        include self::$plugin_dir . 'admin/settings-page.php';
    }

    public function enqueue_frontend_assets() {
        $settings = $this->get_settings();

        wp_enqueue_style(
            'scroll-to-top-styles',
            self::$plugin_url . 'assets/frontend.css',
            array(),
            '1.0.0'
        );

        wp_enqueue_script(
            'scroll-to-top-script',
            self::$plugin_url . 'assets/frontend.js',
            array(),
            '1.0.0',
            true
        );

        wp_localize_script('scroll-to-top-script', 'scrollToTopSettings', array(
            'style' => $settings['style'],
            'bgColor' => $settings['bg_color'],
            'textColor' => $settings['text_color'],
            'size' => $settings['size'],
            'position' => $settings['position'],
            'showMobile' => $settings['show_mobile'],
        ));
    }

    public function enqueue_admin_assets($hook) {
        if ('settings_page_scroll-to-top-settings' !== $hook) {
            return;
        }

        wp_enqueue_style(
            'scroll-to-top-admin-styles',
            self::$plugin_url . 'assets/admin.css',
            array(),
            '1.0.0'
        );

        wp_enqueue_script(
            'scroll-to-top-admin-script',
            self::$plugin_url . 'assets/admin.js',
            array(),
            '1.0.0',
            true
        );
    }

    private function get_settings() {
        return array(
            'style' => get_option('scroll_to_top_style', 'circle'),
            'bg_color' => get_option('scroll_to_top_bg_color', '#333333'),
            'text_color' => get_option('scroll_to_top_text_color', '#ffffff'),
            'size' => get_option('scroll_to_top_size', 'medium'),
            'position' => get_option('scroll_to_top_position', 'bottom-right'),
            'show_mobile' => get_option('scroll_to_top_show_mobile', '1'),
        );
    }
}

Scroll_To_Top::get_instance();
