<?php

namespace Elementor;

use RivaxStudio\Traits\Rivax_Group_Control_Query;
use RivaxStudio\Traits\Rivax_Global_Widget_Controls;
use RivaxStudio\Traits\Rivax_Post_skin_base;

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


class Rivax_Post_Mini_Widget extends Widget_Base {

    use Rivax_Group_Control_Query;
    use Rivax_Global_Widget_Controls;
    use Rivax_Post_skin_base;

    public function get_name() {
        return 'rivax-post-mini';
    }

    public function get_title() {
        return esc_html__('Post Mini', 'newsfy');
    }

    public function get_icon() {
        return 'eicon-post-list rivax-e-icon';
    }

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

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


    protected function register_post_controls() {

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

        $this->add_responsive_control(
            'items_spacing',
            [
                'label'     => esc_html__( 'Items Spacing', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'selectors' => [
                    '{{WRAPPER}} .post-wrapper' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

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

        $this->add_control(
            'icon_position',
            [
                'label'     => esc_html__( 'Icon Position', 'newsfy' ),
                'type'      => Controls_Manager::CHOOSE,
                'options'   => [
                    'start'   => [
                        'title' => esc_html__( 'Top', 'newsfy' ),
                        'icon'  => 'eicon-v-align-top',
                    ],
                    'center' => [
                        'title' => esc_html__( 'Center', 'newsfy' ),
                        'icon'  => 'eicon-v-align-middle',
                    ],
                    'stretch' => [
                        'title' => esc_html__( 'Stretch', 'newsfy' ),
                        'icon'  => 'eicon-align-stretch-v',
                    ],
                    'end'  => [
                        'title' => esc_html__( 'Bottom', 'newsfy' ),
                        'icon'  => 'eicon-v-align-bottom',
                    ],
                ],
                'selectors'      => [
                    '{{WRAPPER}} .post-wrapper' => 'align-items: {{VALUE}};',
                ],
            ]
        ); 

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

        $this->register_title_controls('h6');

	    $this->register_author_controls();

        $this->register_date_controls();

	    $this->register_comments_controls();

	    $this->register_views_count_controls();

	    $this->register_reading_time_controls();

        $this->add_control('link_wrapper', [
            'label' => esc_html__('Item Wrapper Link', 'newsfy'),
            'description' => esc_html__('Add link to whole item.', 'newsfy'),
            'type' => Controls_Manager::SWITCHER,
            'separator' => 'before',
        ]);

        $this->end_controls_section();

    }


    protected function register_style_post_controls() {   
        
        $this->start_controls_section('section_style_item',
        [
            'label' => esc_html__('Item', 'newsfy'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

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

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

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

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

        $this->add_control('last_item_border_none', [
            'label' => esc_html__('Disable Last Item Border', 'newsfy'),
			'type' => Controls_Manager::SWITCHER,
            'condition' => [
                'item_border_border!' => '',
            ],
            'selectors' => [
                    '{{WRAPPER}} .post-wrapper:last-child' => 'border: none;',
                ],
        ]);

        $this->end_controls_section();


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

        $this->add_responsive_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_responsive_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->add_responsive_control(
                'icon_size',
                [
                    'label'     => esc_html__( 'Icon Size', 'newsfy' ),
                    'type'      => Controls_Manager::SLIDER,
                    'selectors' => [
                        '{{WRAPPER}} .icon' => 'font-size: {{SIZE}}{{UNIT}};'
                    ],
                ]
            );

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

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

        $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}};',
                ],
            ]
        );

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

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

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

        $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_group_control(Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'icon_box_shadow',
                'selector' => '{{WRAPPER}} .icon',
            ]);

        $this->end_controls_tab();

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

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

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

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'icon_hover_background',
                'exclude' => [ 'image' ],
                'selector' => '{{WRAPPER}} .post-wrapper:hover .icon',
            ]
        );

        $this->add_control(
            'heading_icon_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' => 'icon_hover_bg_dark_mode',
                'exclude' => [ 'image' ],
                'selector' => 'html[scheme="dark"] {{WRAPPER}} .post-wrapper:hover .icon',
            ]
        );

        $this->add_control('icon_hover_border_color',
            [
                'label' => esc_html__('Border Color', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'condition' => [
                    'icon_border_border!' => '',
                ],
                'selectors' => [
                    '{{WRAPPER}} .post-wrapper: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}} .post-wrapper:hover .icon' => 'border-color: {{VALUE}};',
                ],
            ]);

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

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


    protected function register_controls() {

        $this->register_post_controls();
        $this->register_query_builder_controls();        

        $this->register_style_post_controls();
        $this->register_style_title_controls();
        $this->register_style_meta_controls();

    }

    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';
    }

}