<?php
/*
Plugin Name: Custom Product Badges
Description: Add customizable badges to WooCommerce products.
Version: 1.0.1
Author: Galaxy Weblinks
Text Domain: custom-product-badges
Domain Path: /languages
Requires Plugins: woocommerce
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

if (!defined('ABSPATH')){
    exit;
} 

class Gwls_Custom_Product_Badges {
    private static $instance = null;

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

    public function __construct() {
        // Plugin initialization
        add_action('admin_menu', array($this, 'add_menu_page'));
        add_action('admin_init', array($this, 'register_settings'));
        add_action('wp_enqueue_scripts', array($this, 'enqueue_front_scripts'));
        add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
        add_action('woocommerce_before_shop_loop_item_title', array($this, 'display_badge'), 5);
        add_action('woocommerce_before_single_product_summary', array($this, 'display_badge'), 5);
        add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_plugin_settings_link'));
        add_action('woocommerce_product_options_advanced', array($this, 'add_badge_field')); 
        add_action('woocommerce_process_product_meta', array($this, 'save_badge_field'));

    }

    public function enqueue_front_scripts() {
        wp_enqueue_style(
            'gcpb-public', 
            plugins_url('public/css/public.css', __FILE__), 
            [], 
            '1.0.0' // Version number
        );
    }

    public function enqueue_admin_scripts($hook) {
        wp_enqueue_style('wp-color-picker');
        wp_enqueue_script('wp-color-picker');
        wp_enqueue_script('gcpb-admin', plugins_url('admin/js/admin.js', __FILE__), array('jquery', 'wp-color-picker'), '1.0.0', true);
    }

    // Add the badge field to product page
    public function add_badge_field() {
        woocommerce_wp_text_input(array(
            'id' => '_gcpb_badge_text',
            'label' => __('Badge Text', 'custom-product-badges'),
            'desc_tip' => true,
            'description' => __('Enter the text to display on the badge.', 'custom-product-badges')
        ));
    }

    // Save the badge field value
    public function save_badge_field($post_id) {
        $badge_text = ( isset( $_POST['_gcpb_badge_text'] ) && ! empty( $_POST['_gcpb_badge_text'] ) ) ? sanitize_text_field( wp_unslash( $_POST['_gcpb_badge_text'] ) ) : '';
        update_post_meta( $post_id, '_gcpb_badge_text', $badge_text );
    }

    public function add_plugin_settings_link($links) {
        $settings_link = '<a href="' . admin_url('admin.php?page=badge-settings') . '">' . __('Settings', 'custom-product-badges') . '</a>';
        array_unshift($links, $settings_link);
        return $links;
    }
    
    public function add_menu_page() {
        add_submenu_page(
            'woocommerce',
            'Badge Settings',
            'Badge Settings',
            'manage_options',
            'badge-settings',
            array($this, 'render_settings_page')
        );
    }


    public function register_settings() {
        register_setting('gcpb_settings', 'gcpb_default_color', array(
            'default' => '#000000',
            'sanitize_callback' => function($value) {
                return sanitize_hex_color($value);
            }
        ));
        register_setting('gcpb_settings', 'gcpb_font_color', array(
            'default' => '#ffffff',
            'sanitize_callback' => function($value) {
                return sanitize_hex_color($value);
            }
        ));
    }

    public function render_settings_page() {
        $default_color = get_option('gcpb_default_color', '#000000');
        $font_color = get_option('gcpb_font_color', '#ffffff');
        ?>
        <div class="wrap">
            <h1><?php esc_html_e('Badge Settings', 'custom-product-badges'); ?></h1>
            <form method="post" action="options.php">
                <?php settings_fields('gcpb_settings'); ?>
                <table class="form-table">
                    <tr>
                        <th scope="row"><?php esc_html_e('Badge Background Color', 'custom-product-badges'); ?></th>
                        <td>
                            <input type="text" name="gcpb_default_color" 
                                   value="<?php echo esc_attr($default_color); ?>" 
                                   class="color-picker" />
                        </td>
                    </tr>
                    <tr>
                        <th scope="row"><?php esc_html_e('Badge Font Color', 'custom-product-badges'); ?></th>
                        <td>
                            <input type="text" name="gcpb_font_color" 
                                   value="<?php echo esc_attr($font_color); ?>" 
                                   class="color-picker" />
                        </td>
                    </tr>
                </table>
                <?php submit_button(); ?>
            </form>
        </div>
        <?php
    }

    public function display_badge() {
        global $product;
        if (!$product) return;

        $badge_text = get_post_meta($product->get_id(), '_gcpb_badge_text', true);
        if (empty($badge_text)) return;

        $default_color = get_option('gcpb_default_color', '#000000');
        $font_color = get_option('gcpb_font_color', '#ffffff');
        $classes = array('gcpb-badge', 'gcpb-badge-left');
        
        echo '<span class="' . esc_attr(implode(' ', $classes)) . '" 
                    style="background-color: ' . esc_attr($default_color) . '; color: ' . esc_attr($font_color) . '">' 
                    . esc_html($badge_text) . '</span>';
    }
}

// Initialize the plugin
add_action('plugins_loaded', function() {
    if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
        Gwls_Custom_Product_Badges::get_instance();
    }
});

