<?php

namespace EasyRestaurantManager\Hooks;
use EasyRestaurantManager\Hooks\Handler\AdminMenuHandler;
use EasyRestaurantManager\Resources\SettingResource;
use EasyRestaurantManager\Resources\CategoryResource;
use EasyRestaurantManager\Resources\MenuResource;
// use EasyRestaurantManager\Hooks\CLI\Commands;

use EasyRestaurantManager\Http\View;
use EasyRestaurantManager\Resources\ReservationResource;
use EasyRestaurantManager\Resources\TableResource;
use EasyRestaurantManager\Modules\Blocks\BlockManager;

class Actions {
    public function __construct() {
        add_shortcode('easy_restaurant_manager_menus', [$this, 'menuShortcode']);
        add_shortcode('easy_restaurant_manager_menus_single', [$this, 'menuSingleShortcode']);
        add_shortcode('easy_restaurant_manager_templates', [$this, 'renderTemplates']);
        add_shortcode( 'easy_restaurant_manager_menus_categories', [$this, 'renderCategories']);
        add_shortcode('easy_restaurant_manager_reservations', [$this, 'renderReservations']);

        add_shortcode('easy_res_man_recipt', [$this, 'easy_res_man_recipt_function']);
    
        add_shortcode('easy_res_man_user_name', [$this, 'easy_res_man_user_name_function']);
        
        add_shortcode('easy_res_man_user_email', [$this, 'easy_res_man_user_email_function']);


        add_action( 'plugin_loaded', function () {
            wp_enqueue_style('easy_restaurant_manager_css', EASY_RESTAURANT_MANAGER_PLUGIN_URL.'assets/css/erm_public.css', [], EASY_RESTAURANT_MANAGER_VERSION);
            wp_enqueue_script( 'easy_restaurant_manager_js', EASY_RESTAURANT_MANAGER_PLUGIN_URL.'assets/js/erm_public.js',array('jquery'),EASY_RESTAURANT_MANAGER_VERSION, false );
        });

        add_action('init', function() {
            (new BlockManager());
            // Register custom post type
            // register_post_type('easy_restaurant_manager_menu', [
            //     'labels' => [
            //         'name' => 'Campaigns',
            //         'singular_name' => 'Campaign',
            //         'add_new_item' => 'Add New Campaign',
            //         'edit_item' => 'Edit Campaign',
            //     ],
            //     'public' => true,
            //     'has_archive' => true,
            //     'rewrite' => ['slug' => 'campaign'],
            //     'supports' => ['title', 'editor', 'thumbnail'],
            //     'menu_icon' => 'dashicons-megaphone',
            //     'show_in_menu' => false, // keep using your custom admin menu
            //     'show_in_rest' => true
            // ]);
        });
        
      
        $this->register_hooks();
        if (defined('WP_CLI') && WP_CLI) {
            \WP_CLI::add_command('easyRestaurantManager', '\EasyRestaurantManager\Hooks\CLI\Commands');
        }
    }

    public function easy_res_man_user_name_function() {
        // Logic to retrieve user name
        return "User name";
    }

    public function menuSingleShortcode($atts) {
        $menu_id = (int) $atts['menu_id'];
        $menu = MenuResource::getMenuById($menu_id);
        if (!$menu) {
            return '<p>No menu found</p>';
        }
        ob_start();
        View::render('MenuTemplate/SingleMenu',[
            'menu' => $menu
        ]);
        return ob_get_clean();
    }

    public function easy_res_man_recipt_function() {
        // Logic to retrieve receipt
        return "Your receipt info";
    }

    public function easy_res_man_user_email_function() {
        // Logic to retrieve user email
        return "User email";
    }

    private function register_hooks() {
        $admin_menu_handler = new AdminMenuHandler();
        add_action('admin_menu', [$admin_menu_handler, 'add_admin_menu']);
        remove_all_actions('admin_notices');
    }

    public function menuShortcode($atts) {
        wp_enqueue_style('easy_restaurant_manager_css_2', EASY_RESTAURANT_MANAGER_PLUGIN_URL.'assets/css/erm_menu_template_2.css', [], EASY_RESTAURANT_MANAGER_VERSION);
        $settings = SettingResource::getTemplateSettings();
        $menus = MenuResource::getMenuWithCategory();
  
       if(!$menus) {
        return '<p>No menus found . please add menus by easy restaurant manger dashboard</p>';
       }

       $primary_light = self::mix_with_white($settings['primary_color'], 80);
       $font_light = self::mix_with_white($settings['font_color'], 40);
        ob_start();
        ?><div class="erm_template_menu_wrapper" style="
            --primary-color: <?php echo esc_attr($settings['primary_color']); ?>; 
           --secondary-color: <?php echo esc_attr($settings['secondary_color']); ?>;
           --background-color: <?php echo esc_attr($settings['background_color']); ?>; 
           --font-color: <?php echo esc_attr($settings['font_color']); ?>;
           --primary-light: <?php echo esc_attr($primary_light); ?>;
           --font-light: <?php echo esc_attr($font_light); ?>;
           min-width: 1040px;
        "><?php
        if($settings['template'] == 'standard') {
            View::render('MenuTemplate/Standard/MenuPage',[
                'menus' => $menus,
                'settings' => $settings
            ]);
        } else if($settings['template'] == 'classic') {
            wp_enqueue_style('easy_classic_template', EASY_RESTAURANT_MANAGER_PLUGIN_URL.'assets/css/classic_temp.css', [], EASY_RESTAURANT_MANAGER_VERSION);
            wp_enqueue_script( 'easy_classic_template_js', EASY_RESTAURANT_MANAGER_PLUGIN_URL.'assets/js/classic_temp.js',array('jquery'),EASY_RESTAURANT_MANAGER_VERSION, false );
            $categories = CategoryResource::getCategory();
            View::render('MenuTemplate/Classic/MenuPage',[
                'menus' => $menus,
                'settings' => $settings,
                'categories' => $categories
            ]);
        }
        ?></div><?php
        return ob_get_clean();
    }

    public function renderTemplates($attr)
    {
        ob_start();
        echo do_shortcode('[easy_restaurant_manager_menus_categories]');
        echo do_shortcode('[easy_restaurant_manager_menus]' );
        return ob_get_clean();
    }

    public function renderCategories($atts)
    {
        wp_enqueue_style('easy_restaurant_manager_css_2', EASY_RESTAURANT_MANAGER_PLUGIN_URL.'assets/css/erm_menu_template_2.css', [], EASY_RESTAURANT_MANAGER_VERSION);
        $settings = SettingResource::getTemplateSettings();
        $categories = CategoryResource::getCategory();
       if(!$categories) {
        return '<p>No categories found . please add categories by easy restaurant manger dashboard</p>';
       }

       $primary_light = self::mix_with_white($settings['primary_color'], 80);
       $font_light = self::mix_with_white($settings['font_color'], 40);
        ob_start();
        ?><div class="erm_template_menu_wrapper" style="
            --primary-color: <?php echo esc_attr($settings['primary_color']); ?>; 
           --secondary-color: <?php echo esc_attr($settings['secondary_color']); ?>; 
           --font-color: <?php echo esc_attr($settings['font_color']); ?>;
           --primary-light: <?php echo esc_attr($primary_light); ?>;
           --background-color: <?php echo esc_attr($settings['background_color']); ?>;
           --font-light: <?php echo esc_attr($font_light); ?>;
           min-width: 1040px;
        "><?php
        View::render('MenuTemplate/Standard/Categories',[
            'settings' => $settings,
            'categories' => $categories
        ]); 
        ?></div><?php
        return ob_get_clean();
    }

    public function renderReservations($atts)
    {
        wp_enqueue_style('easy_restaurant_manager_reservations_css', EASY_RESTAURANT_MANAGER_PLUGIN_URL.'assets/css/erm_reservation.css', [], EASY_RESTAURANT_MANAGER_VERSION);
        wp_enqueue_script( 'easy_restaurant_manager_reservations_js', EASY_RESTAURANT_MANAGER_PLUGIN_URL.'assets/js/reservation_table.js',array('jquery'),EASY_RESTAURANT_MANAGER_VERSION, true );
        wp_localize_script('easy_restaurant_manager_reservations_js', 'RestaurantMenuMangeData', [
            'rest_api' => rest_url('easy-restaurant-manage/v1'),
            'nonce' => wp_create_nonce('wp_rest'),
            'branch_id' => 1,
        ]);
        
        $settings = SettingResource::getTemplateSettings();
        // dd($settings);
        // $table = TableResource::getTable();
        $reservations = ReservationResource::getReservation();
        // dd($reservations);
        // if(!$table) {
        //     return '<p>No tables found.</p>';
        // }
       
       $primary_light = self::mix_with_white($settings['primary_color'], 80);
       $font_light = self::mix_with_white($settings['font_color'], 40);
       ob_start();
        ?><div id="easy-restaurant-manager-reservation-app" class="erm_template_menu_wrapper" style="
            --primary-color: <?php echo esc_attr($settings['primary_color']); ?>; 
           --secondary-color: <?php echo esc_attr($settings['secondary_color']); ?>; 
           --font-color: <?php echo esc_attr($settings['font_color']); ?>;
           --background-color: <?php echo esc_attr($settings['background_color']); ?>; 
           --primary-light: <?php echo esc_attr($primary_light); ?>;
           --font-light: <?php echo esc_attr($font_light); ?>;
           min-width: 1040px;
        "></div> <?php
        return ob_get_clean();     
    }

    public static function mix_with_white($color, $percentage) {
        $color = ltrim($color, '#'); // Remove # if present
        $r = hexdec(substr($color, 0, 2));
        $g = hexdec(substr($color, 2, 2));
        $b = hexdec(substr($color, 4, 2));
    
        // Calculate the new color mixed with white
        $new_r = round($r + (255 - $r) * ($percentage / 100));
        $new_g = round($g + (255 - $g) * ($percentage / 100));
        $new_b = round($b + (255 - $b) * ($percentage / 100));
    
        return sprintf("#%02x%02x%02x", $new_r, $new_g, $new_b);
    }
}