<?php
/**
 * Flavor – Restaurant Menu
 *
 * Elementor widget (Free version) — Card Grid layout, Elegant Dark theme.
 *
 * @package    FlavorMenu
 * @copyright  Copyright (c) 2024 Flavor Studio
 * @license    GPL-2.0-or-later
 * @link       https://flavor-menu.com
 */

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

class Flavor_Elementor_Widget extends \Elementor\Widget_Base {

    public function get_name() {
        return 'flavor_menu';
    }

    public function get_title() {
        return __( 'Menu Card', 'flavor-restaurant-menu' );
    }

    public function get_icon() {
        return 'eicon-menu-card';
    }

    public function get_categories() {
        return [ 'general' ];
    }

    public function get_keywords() {
        return [ 'menu', 'restaurant', 'food', 'card' ];
    }

    /**
     * Helper: build category options array for Select controls.
     */
    private function get_cat_options() {
        $cats    = get_terms( [ 'taxonomy' => 'flavor_menu_cat', 'hide_empty' => false ] );
        $options = [ '' => __( 'All Categories', 'flavor-restaurant-menu' ) ];
        if ( ! is_wp_error( $cats ) ) {
            foreach ( $cats as $cat ) {
                $options[ $cat->slug ] = $cat->name;
            }
        }
        return $options;
    }

    protected function register_controls() {
        $cat_options = $this->get_cat_options();

        // ── General ────────────────────────────────────────────────────────────
        $this->start_controls_section( 'section_general', [
            'label' => __( 'General', 'flavor-restaurant-menu' ),
        ] );

        $this->add_control( 'title', [
            'label'       => __( 'Heading', 'flavor-restaurant-menu' ),
            'type'        => \Elementor\Controls_Manager::TEXT,
            'default'     => __( 'Our Menu', 'flavor-restaurant-menu' ),
            'label_block' => true,
        ] );

        $this->add_control( 'description', [
            'label'       => __( 'Description', 'flavor-restaurant-menu' ),
            'type'        => \Elementor\Controls_Manager::TEXTAREA,
            'default'     => '',
            'label_block' => true,
        ] );

        $this->add_control( 'category', [
            'label'   => __( 'Filter by Category', 'flavor-restaurant-menu' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'options' => $cat_options,
            'default' => '',
        ] );

        $this->add_control( 'limit', [
            'label'   => __( 'Max. Items', 'flavor-restaurant-menu' ),
            'type'    => \Elementor\Controls_Manager::NUMBER,
            'default' => 12,
            'min'     => 1,
            'max'     => 200,
        ] );

        $this->end_controls_section();

        // ── Display ────────────────────────────────────────────────────────────
        $this->start_controls_section( 'section_design', [
            'label' => __( 'Display', 'flavor-restaurant-menu' ),
        ] );

        $this->add_control( 'show_filter', [
            'label'        => __( 'Show Category Filter', 'flavor-restaurant-menu' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'label_on'     => __( 'Yes', 'flavor-restaurant-menu' ),
            'label_off'    => __( 'No', 'flavor-restaurant-menu' ),
            'return_value' => 'yes',
            'default'      => 'yes',
        ] );

        $this->add_control( 'hide_all_btn', [
            'label'        => __( 'Hide "All" Button', 'flavor-restaurant-menu' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'label_on'     => __( 'Yes', 'flavor-restaurant-menu' ),
            'label_off'    => __( 'No', 'flavor-restaurant-menu' ),
            'return_value' => 'yes',
            'default'      => '',
            'description'  => __( 'Hides the "All" button. Recommended when a default category is set.', 'flavor-restaurant-menu' ),
            'condition'    => [ 'show_filter' => 'yes' ],
        ] );

        $this->add_control( 'default_category', [
            'label'       => __( 'Default Category (Preselect)', 'flavor-restaurant-menu' ),
            'type'        => \Elementor\Controls_Manager::SELECT,
            'options'     => $cat_options,
            'default'     => '',
            'description' => __( 'This category will be preselected on load.', 'flavor-restaurant-menu' ),
            'condition'   => [ 'show_filter' => 'yes' ],
        ] );

        $this->end_controls_section();

        // ── Advanced ───────────────────────────────────────────────────────────
        $this->start_controls_section( 'section_advanced', [
            'label' => __( 'Advanced', 'flavor-restaurant-menu' ),
        ] );

        $this->add_control( 'currency', [
            'label'   => __( 'Currency Symbol', 'flavor-restaurant-menu' ),
            'type'    => \Elementor\Controls_Manager::TEXT,
            'default' => '€',
        ] );

        $this->add_control( 'el_class', [
            'label'       => __( 'Additional CSS Class', 'flavor-restaurant-menu' ),
            'type'        => \Elementor\Controls_Manager::TEXT,
            'default'     => '',
            'label_block' => true,
        ] );

        $this->end_controls_section();
    }

    protected function render() {
        $s = $this->get_settings_for_display();

        $atts = [
            'title'            => $s['title'] ?? '',
            'description'      => $s['description'] ?? '',
            'category'         => $s['category'] ?? '',
            'limit'            => $s['limit'] ?? 12,
            'layout'           => 'card-grid',
            'columns'          => 3,
            'style'            => 'dark',
            'image_ratio'      => '4-3',
            'show_filter'      => $s['show_filter'] ?? 'yes',
            'hide_all_btn'     => $s['hide_all_btn'] ?? '',
            'default_category' => $s['default_category'] ?? '',
            'show_badges'      => 'yes',
            'currency'         => $s['currency'] ?? '€',
            'el_class'         => $s['el_class'] ?? '',
            'location'         => '',
        ];

        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted plugin HTML, internally sanitized by Flavor_Shortcode::render().
        echo Flavor_Shortcode::render( $atts );
    }
}
