<?php

namespace Elementor;

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


class Rivax_Category_Spot_Widget extends Widget_Base {

    public function get_name() {
        return 'rivax-category-spot';
    }

    public function get_title() {
        return esc_html__('Category Spot', 'newsfy');
    }

    public function get_icon() {
        return 'eicon-info-box rivax-e-icon';
    }

    protected function get_html_wrapper_class() {
        return $this->get_name() . '-widget';
    }

    public function get_categories() {
        return ['rivax-elements'];
    }


    protected function register_controls() {

        $this->start_controls_section(
            'section_layout',
            [
                'label' => esc_html__( 'Layout', 'newsfy' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_responsive_control(
            'columns',
            [
                'label'              => esc_html__( 'Columns', 'newsfy' ),
                'type'               => Controls_Manager::SELECT,
                'default'            => '5',
                'options'            => [
                    '1' => '1',
                    '2' => '2',
                    '3' => '3',
                    '4' => '4',
                    '5' => '5',
                    '6' => '6',
                    '7' => '7',
                    '8' => '8',
                ],
                'selectors' => [
                    '{{WRAPPER}} .rivax-category-spot' => 'grid-template-columns: repeat({{SIZE}}, 1fr);',
                ],
            ]
        );

        $this->add_responsive_control(
            'column_gap',
            [
                'label'     => esc_html__( 'Column Gap', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'default'   => [
                    'size' => '20',
                ],
                'selectors' => [
                    '{{WRAPPER}} .rivax-category-spot' => 'column-gap: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'row_gap',
            [
                'label'     => esc_html__( 'Row Gap', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'default'   => [
                    'size' => '20',
                ],
                'selectors' => [
                    '{{WRAPPER}} .rivax-category-spot' => 'row-gap: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'icon_spacing',
            [
                'label'     => esc_html__( 'Icon Spacing', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'selectors' => [
                    '{{WRAPPER}} .cat-item' => 'gap: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'hide_last_item_on_tablet',
            [
                'label'     => esc_html__( 'Hide Last Item On Tablet', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'selectors' => [
                    'body[data-elementor-device-mode="tablet"] {{WRAPPER}} .cat-item:last-of-type' => 'display: none;',
                ],
            ]
        );

        $this->add_control(
            'hide_last_item_on_mobile',
            [
                'label'     => esc_html__( 'Hide Last Item On Mobile', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'selectors' => [
                    'body[data-elementor-device-mode="mobile"] {{WRAPPER}} .cat-item:last-of-type' => 'display: none;',
                ],
            ]
        );

        $this->add_control(
            'open_link_new_tab',
            [
                'label'     => esc_html__( 'Open Link In New Tab', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
            ]
        );

        $this->add_control(
            'show_count',
            [
                'label'     => esc_html__( 'Show Posts Count', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
            ]
        );

        $this->add_control(
            'category_count_text',
            [
                'label'     => esc_html__( 'Count Text', 'newsfy' ),
                'type'      => Controls_Manager::TEXT,
                'default' => esc_html__( ' Posts' , 'newsfy' ),
                'condition' => [
                    'show_count' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'browse_text',
            [
                'label'     => esc_html__( 'Browse Text', 'newsfy' ),
                'type'      => Controls_Manager::TEXT,
                'default' => esc_html__( 'Browse' , 'newsfy' ),
            ]
        );

        $this->add_control(
            'icon_left',
            [
                'label'     => esc_html__( 'Icon Left', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'selectors' => [
                    '{{WRAPPER}} .cat-item' => 'flex-direction: row; align-items: center;',
                ],
            ]
        );

        $this->add_control(
            'center_content',
            [
                'label'     => esc_html__( 'Center Content', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'condition' => [
                    'icon_left' => '',
                ],
                'selectors' => [
                    '{{WRAPPER}} .cat-item' => 'text-align: center; align-items: center;',
                ],
            ]
        );

        $this->end_controls_section();


        $this->start_controls_section(
            'section_categories',
            [
                'label' => esc_html__( 'Categories', 'newsfy' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $categories_list = get_terms(['taxonomy' => 'category', 'fields' => 'id=>name']);


        $repeater = new Repeater();

        $repeater->add_control(
            'category',
            [
                'label'         => esc_html__( 'Category', 'newsfy' ),
                'type'          => Controls_Manager::SELECT,
                'options'       => $categories_list,

            ]
        );

        $repeater->add_control(
            'icon',
            [
                'label' => esc_html__( 'Icon', 'newsfy' ),
                'type' => Controls_Manager::ICONS,
            ]
        );


        $this->add_control(
            'category_items',
            [
                'label' => esc_html__( 'Category Items', 'newsfy' ),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'prevent_empty' => false,
            ]
        );

        $this->end_controls_section();


	    $this->start_controls_section('section_style_cat_item',
		    [
			    'label' => esc_html__('Category Item', 'newsfy'),
			    'tab' => Controls_Manager::TAB_STYLE,
		    ]);

	    $this->add_responsive_control('cat_item_padding',
		    [
			    'label' => esc_html__('Padding', 'newsfy'),
			    'type' => Controls_Manager::DIMENSIONS,
			    'selectors' => [
				    '{{WRAPPER}} .cat-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			    ],
		    ]);

	    $this->add_responsive_control('cat_item_border_radius',
		    [
			    'label' => esc_html__('Border Radius', 'newsfy'),
			    'type' => Controls_Manager::DIMENSIONS,
			    'selectors' => [
				    '{{WRAPPER}} .cat-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			    ],
		    ]);



	    $this->start_controls_tabs('tabs_cat_item_style');

	    $this->start_controls_tab('tab_cat_item_normal',
		    [
			    'label' => esc_html__('Normal', 'newsfy'),
		    ]);

	    $this->add_group_control(
		    Group_Control_Background::get_type(),
		    [
			    'name' => 'cat_item_background',
			    'exclude' => [ 'image' ],
			    'selector' => '{{WRAPPER}} .cat-item',
		    ]
	    );

        $this->add_control(
            'heading_cat_item_bg_dark_mode',
            [
                'label'     => esc_html__( 'Background - Dark Mode', 'newsfy' ),
                'type'      => Controls_Manager::HEADING,
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'cat_item_bg_dark_mode',
                'exclude' => [ 'image' ],
                'selector' => 'html[scheme="dark"] {{WRAPPER}} .cat-item',
            ]
        );


	    $this->add_group_control(
		    Group_Control_Border::get_type(),
		    [
			    'name' => 'cat_item_border',
			    'selector' => '{{WRAPPER}} .cat-item',
		    ]);

        $this->add_control('cat_item_border_color_dark_mode',
            [
                'label' => esc_html__('Border Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'condition' => [
                    'cat_item_border_border!' => '',
                ],
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .cat-item' => 'border-color: {{VALUE}};',
                ],
            ]);


	    $this->add_group_control(Group_Control_Box_Shadow::get_type(),
		    [
			    'name' => 'cat_item_box_shadow',
			    'selector' => '{{WRAPPER}} .cat-item',
		    ]);

	    $this->end_controls_tab();

	    $this->start_controls_tab('tab_cat_item_hover',
		    [
			    'label' => esc_html__('Hover', 'newsfy'),
		    ]);

	    $this->add_group_control(
		    Group_Control_Background::get_type(),
		    [
			    'name' => 'cat_item_hover_background',
			    'exclude' => [ 'image' ],
			    'selector' => '{{WRAPPER}} .cat-item:hover',
		    ]
	    );

        $this->add_control(
            'heading_cat_item_hover_bg_dark_mode',
            [
                'label'     => esc_html__( 'Background - Dark Mode', 'newsfy' ),
                'type'      => Controls_Manager::HEADING,
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'cat_item_hover_bg_dark_mode',
                'exclude' => [ 'image' ],
                'selector' => 'html[scheme="dark"] {{WRAPPER}} .cat-item:hover',
            ]
        );


	    $this->add_control('cat_item_hover_border_color',
		    [
			    'label' => esc_html__('Border Color', 'newsfy'),
			    'type' => Controls_Manager::COLOR,
			    'condition' => [
				    'cat_item_border_border!' => '',
			    ],
			    'selectors' => [
				    '{{WRAPPER}} .cat-item:hover' => 'border-color: {{VALUE}};',
			    ],
		    ]);

        $this->add_control('cat_item_hover_border_color_dark_mode',
            [
                'label' => esc_html__('Border Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'condition' => [
                    'cat_item_border_border!' => '',
                ],
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .cat-item:hover' => 'border-color: {{VALUE}};',
                ],
            ]);


	    $this->add_group_control(Group_Control_Box_Shadow::get_type(),
		    [
			    'name' => 'cat_item_hover_box_shadow',
			    'selector' => '{{WRAPPER}} .cat-item:hover',
		    ]);


	    $this->end_controls_tab();
	    $this->end_controls_tabs();

	    $this->end_controls_section();


        $this->start_controls_section('section_style_content',
            [
                'label' => esc_html__('Content', 'newsfy'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]);


        $this->add_control(
            'browse_heading',
            [
                'label'     => esc_html__( 'Browse', 'newsfy' ),
                'type'      => Controls_Manager::HEADING,
            ]
        );

        $this->add_control('browse_color',
            [
                'label' => esc_html__('Color', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .browse' => 'color: {{VALUE}};',
                ],
            ]);

        $this->add_control('browse_color_dark_mode',
            [
                'label' => esc_html__('Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .browse' => 'color: {{VALUE}};',
                ],
            ]);

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'browse_typography',
                'label' => esc_html__('Typography', 'newsfy'),
                'selector' => '{{WRAPPER}} .browse',
            ]);

        $this->add_control(
            'name_heading',
            [
                'label'     => esc_html__( 'Name', 'newsfy' ),
                'type'      => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $this->add_control('name_color',
            [
                'label' => esc_html__('Color', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .name' => 'color: {{VALUE}};',
                ],
            ]);

        $this->add_control('name_color_dark_mode',
            [
                'label' => esc_html__('Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .name' => 'color: {{VALUE}};',
                ],
            ]);

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'name_typography',
                'label' => esc_html__('Typography', 'newsfy'),
                'selector' => '{{WRAPPER}} .name',
            ]);

        $this->add_control(
            'badge_heading',
            [
                'label'     => esc_html__( 'Badge', 'newsfy' ),
                'type'      => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $this->add_control(
            'disable_badge',
            [
                'label'     => esc_html__( 'Disable', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'selectors' => [
                    '{{WRAPPER}} .name span::before' => 'display: none;',
                ],
            ]
        );

        $this->add_control(
            'badge_width',
            [
                'label'     => esc_html__( 'Width', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'selectors' => [
                    '{{WRAPPER}} .name span::before' => 'width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'badge_height',
            [
                'label'     => esc_html__( 'Height', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'selectors' => [
                    '{{WRAPPER}} .name span::before' => 'height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control('badge_color',
            [
                'label' => esc_html__('Color', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .name span::before' => 'background: {{VALUE}};',
                ],
            ]);

        $this->add_control('badge_color_dark_mode',
            [
                'label' => esc_html__('Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .name span::before' => 'background: {{VALUE}};',
                ],
            ]);

        $this->add_control('badge_border_radius',
            [
                'label' => esc_html__('Border Radius', 'newsfy'),
                'type' => Controls_Manager::DIMENSIONS,
                'selectors' => [
                    '{{WRAPPER}} .name span::before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]);

        $this->end_controls_section();



        $this->start_controls_section('section_style_icon',
            [
                'label' => esc_html__('Icon', 'newsfy'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]);

        $this->add_control(
            'icon_size',
            [
                'label'     => esc_html__( 'Size', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'selectors' => [
                    '{{WRAPPER}} .icon' => 'font-size: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control('icon_padding',
            [
                'label' => esc_html__('Padding', 'newsfy'),
                'type' => Controls_Manager::DIMENSIONS,
                'selectors' => [
                    '{{WRAPPER}} .icon' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]);

        $this->add_control('icon_border_radius',
            [
                'label' => esc_html__('Border Radius', 'newsfy'),
                'type' => Controls_Manager::DIMENSIONS,
                'selectors' => [
                    '{{WRAPPER}} .icon' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]);

        $this->start_controls_tabs('tabs_icon_style');

        $this->start_controls_tab('tab_icon_normal',
            [
                'label' => esc_html__('Normal', 'newsfy'),
            ]);

        $this->add_control(
            'icon_term_color',
            [
                'label'     => esc_html__( 'Term Color', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'selectors' => [
                    '{{WRAPPER}} .icon' => 'color: var(--term-color, var(--accent-color));',
                ],
            ]
        );

        $this->add_control('icon_color',
            [
                'label' => esc_html__('Color', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .icon' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'icon_term_color' => '',
                ],
            ]);

        $this->add_control('icon_color_dark_mode',
            [
                'label' => esc_html__('Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .icon' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'icon_term_color' => '',
                ],
            ]);

        $this->add_control(
            'icon_term_bg',
            [
                'label'     => esc_html__( 'Term Background', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'selectors' => [
                    '{{WRAPPER}} .icon' => 'background: var(--term-color, var(--accent-color));',
                ],
            ]
        );

        $this->add_control('icon_bg',
            [
                'label' => esc_html__('Background', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .icon' => 'background: {{VALUE}};',
                ],
                'condition' => [
                    'icon_term_bg' => '',
                ],
            ]);

        $this->add_control('icon_bg_dark_mode',
            [
                'label' => esc_html__('Background - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .icon' => 'background: {{VALUE}};',
                ],
                'condition' => [
                    'icon_term_bg' => '',
                ],
            ]);

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'icon_border',
                'selector' => '{{WRAPPER}} .icon',
            ]);

        $this->add_control('icon_border_color_dark_mode',
            [
                'label' => esc_html__('Border Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'condition' => [
                    'icon_border_border!' => '',
                ],
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .icon' => 'border-color: {{VALUE}};',
                ],
            ]);

        $this->add_control(
            'icon_term_shadow',
            [
                'label'     => esc_html__( 'Term Shadow', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'selectors' => [
                    '{{WRAPPER}} .icon' => 'box-shadow: 0px 5px 15px -4px var(--term-color, var(--accent-color));',
                ],
            ]
        );

        $this->add_group_control(Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'icon_box_shadow',
                'selector' => '{{WRAPPER}} .icon',
                'condition' => [
                    'icon_term_shadow' => '',
                ],
            ]);

        $this->end_controls_tab();

        $this->start_controls_tab('tab_icon_hover',
            [
                'label' => esc_html__('Hover', 'newsfy'),
            ]);

        $this->add_control(
            'icon_hover_term_color',
            [
                'label'     => esc_html__( 'Term Color', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'selectors' => [
                    '{{WRAPPER}} .cat-item:hover .icon' => 'color: var(--term-color, var(--accent-color));',
                ],
            ]
        );

        $this->add_control('icon_hover_color',
            [
                'label' => esc_html__('Color', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .cat-item:hover .icon' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'icon_hover_term_color' => '',
                ],
            ]);

        $this->add_control('icon_hover_color_dark_mode',
            [
                'label' => esc_html__('Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .cat-item:hover .icon' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'icon_hover_term_color' => '',
                ],
            ]);

        $this->add_control(
            'icon_hover_term_bg',
            [
                'label'     => esc_html__( 'Term Background', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'selectors' => [
                    '{{WRAPPER}} .cat-item:hover .icon' => 'background: var(--term-color, var(--accent-color));',
                ],
            ]
        );

        $this->add_control('icon_hover_bg',
            [
                'label' => esc_html__('Background', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .cat-item:hover .icon' => 'background: {{VALUE}};',
                ],
                'condition' => [
                    'icon_hover_term_bg' => '',
                ],
            ]);

        $this->add_control('icon_hover_bg_dark_mode',
            [
                'label' => esc_html__('Background - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .cat-item:hover .icon' => 'background: {{VALUE}};',
                ],
                'condition' => [
                    'icon_hover_term_bg' => '',
                ],
            ]);


        $this->add_control('icon_hover_border_color',
            [
                'label' => esc_html__('Border Color', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'condition' => [
                    'icon_border_border!' => '',
                ],
                'selectors' => [
                    '{{WRAPPER}} .cat-item:hover .icon' => 'border-color: {{VALUE}};',
                ],
            ]);

        $this->add_control('icon_hover_border_color_dark_mode',
            [
                'label' => esc_html__('Border Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'condition' => [
                    'icon_border_border!' => '',
                ],
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .cat-item:hover .icon' => 'border-color: {{VALUE}};',
                ],
            ]);

        $this->add_control(
            'icon_hover_term_shadow',
            [
                'label'     => esc_html__( 'Term Shadow', 'newsfy' ),
                'type'      => Controls_Manager::SWITCHER,
                'selectors' => [
                    '{{WRAPPER}} .cat-item:hover .icon' => 'box-shadow: 0px 5px 15px -4px var(--term-color, var(--accent-color));',
                ],
            ]
        );

        $this->add_group_control(Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'icon_hover_box_shadow',
                'selector' => '{{WRAPPER}} .cat-item:hover .icon',
                'condition' => [
                    'icon_hover_term_shadow' => '',
                ],
            ]);


        $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->end_controls_section();

    }

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

        $widget_path_name = str_replace('rivax-', '', $this->get_name() );
        include RIVAX_THEME_DIR . '/elementor/templates/' . $widget_path_name . '/' . $widget_path_name . '.php';
    }

}