<?php
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Scheme_Color;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
Use Elementor\Core\Schemes\Typography;
use Elementor\Group_Control_Border;

if (!defined('ABSPATH')) exit; // Exit if accessed directly
if(!function_exists('widgetkit_pro_array_get')) {
    function widgetkit_pro_array_get($array, $key, $default=null){
        if(!is_array($array)) return $default;
        return array_key_exists($key, $array) ? $array[$key] : $default;
    }
}

/**
 * magmax Layout
 * =============
 */
class Widgetkit_Pro_Dynamic_Header_Tab extends Widget_Base
{

    public function get_name()
    {
        return 'magmax-dynamic-header-tab';
    }

    public function get_title()
    {
        return __('Dynamic Header Tab', 'widgetkit-pro');
    }

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

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

    /**
     * A list of scripts that the widgets is depended in
     *
     * @since 1.3.0
     **/

//    public function get_script_depends() {
//        return [ 'magmax_test' ];
//    }


    protected function register_controls()
    {
        $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => false,
        ) );
        $cat_names = array();
        foreach( $terms as $t ):
            $cat_names[$t->term_id] = $t->name;
        endforeach;

        /* slides content title subtitle button and button link */
        $this->start_controls_section(
            'section_tab',
            [
                'label' => __('Layout', 'widgetkit-pro'),
            ]
        );

        $this->add_control(
            'layout_style',
            [
                'label'       => __( 'Layout Style', 'widgetkit-pro' ),
                'type' => Controls_Manager::SELECT,
                'default' => 'layout-1',
                'options' => [
                    'layout-1'  => __( 'Layout 1', 'widgetkit-pro' ),
                    'layout-2'  => __( 'Layout 2', 'widgetkit-pro' ),
                ],
            ]
        );
        $this->add_control(
            'cat_single_id',
            [
                'label' => __( 'Category List', 'widgetkit-pro' ),
                'type' => Controls_Manager::SELECT,
                'options' => $cat_names,
                'condition' => [
                    'layout_style' => 'layout-2',
                ]
            ]
        );
        $this->add_control(
            'post_column',
            [
                'label'   => __( 'Post Column', 'widgetkit-pro' ),
                'type'    => Controls_Manager::NUMBER,
                'default' => 4,
                'min'     => 3,
                'max'     => 6,
                'step'    => 1,
            ]
        );
        $this->add_control(
            'post_num_to_show',
            [
                'label'   => __( 'Number of Post to Show', 'widgetkit-pro' ),
                'type'    => Controls_Manager::NUMBER,
                'default' => 4,
                'min'     => 3,
                'max'     => 20,
                'step'    => 1,
            ]
        );
        $this->add_control(
            'custom_height',
            [
                'label' => __( 'Custom Height', 'widgetkit-pro' ),
                'type' => Controls_Manager::SWITCHER,
                'default' => 'No',
                'description' => 'Add custom height',
                'label_on' => __( 'Yes', 'widgetkit-pro' ),
                'label_off' => __( 'No', 'widgetkit-pro' ),
                'return_value' => 'yes',
            ]
        );
        $this->add_control(
            'post-min-height',
            [
                'label' => __( 'Section Height', 'widgetkit-pro' ),
                'type' => Controls_Manager::SLIDER,
                'default' => [
                    'size' => 500,
                ],
                'range' => [
                    'px' => [
                        'min' => 300,
                        'max' => 1500,
                        'step' => 1,
                    ],
                ],
                'size_units' => [ 'px' ],
                'selectors' => [
                    '{{WRAPPER}} .dynamic-header-tab' => 'height: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .dynamic-header-tab-2 .active-post-content .post-data' => 'height: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'custom_height' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'featured_thumbnail_size',
            [
                'label'       => __( 'Highlighted Thumbnail Size', 'widgetkit-pro' ),
                'type' => Controls_Manager::SELECT,
                'default' => 'large',
                'options' => [
                    'medium'  => __( 'Medium', 'widgetkit-pro' ),
                    'medium_large'  => __( 'Medium Large', 'widgetkit-pro' ),
                    'large'  => __( 'Large', 'widgetkit-pro' ),
                    'full'  => __( 'Full', 'widgetkit-pro' ),
                ],
            ]
        );
        $this->add_control(
            'regular_thumbnail_size',
            [
                'label'       => __( 'Thumbnail Size', 'widgetkit-pro' ),
                'type' => Controls_Manager::SELECT,
                'default' => 'large',
                'options' => [
                    'medium'  => __( 'Medium', 'widgetkit-pro' ),
                    'medium_large'  => __( 'Medium Large', 'widgetkit-pro' ),
                    'large'  => __( 'Large', 'widgetkit-pro' ),
                    'full'  => __( 'Full', 'widgetkit-pro' ),
                ],
            ]
        );
        $this->add_control(
            'title_word',
            [
                'label'       => __( 'Title Word Count', 'widgetkit-pro' ),
                'type'    => Controls_Manager::NUMBER,
                'default' => 7,
                'min'     => 5,
                'max'     => 100,
                'step'    => 1,
            ]
        );



        $this->end_controls_section();

        /**
         * Style tab
         * =============
         */
        $this->start_controls_section(
            'section_style',
            [
                'label' => __( 'Typography', 'widgetkit-pro' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'label' => __( 'Section Title', 'widgetkit-pro' ),
                'name' => 'section_title_typography',
                'selector' => '{{WRAPPER}} .layout-title h4',
            ]
        );
        /**
         * Highlight title
         */
        $this->add_control(
            'header_highlight_title',
            [
                'label' => __( 'Highlight Post Title', 'widgetkit-pro' ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',

            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'label' => __( 'Highlight Post Title', 'widgetkit-pro' ),
                'name' => 'header_highlight_post_title_typography',
                'selector' => '{{WRAPPER}} .active-post-content a h3',

            ]
        );
        /**
         * regular title
         */
        $this->add_control(
            'header_regular_title',
            [
                'label' => __( 'Regular Post Title', 'widgetkit-pro' ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',

            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'label' => __( 'Regular Post Title', 'widgetkit-pro' ),
                'name' => 'header_regular_post_title_typography',
                'selector' => '{{WRAPPER}} .regular-post .post-title h3',

            ]
        );

        $this->end_controls_section();


        $this->start_controls_section(
            'content_style',
            [
                'label' => __( 'Content', 'widgetkit-pro' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        /**
         * Custom Color
         */
        $this->add_control(
            'custom_color',
            [
                'label' => __( 'Custom Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::SWITCHER,
                'default' => '',
                'description' => 'Show custom color for content',
                'label_on' => __( 'Yes', 'widgetkit-pro' ),
                'label_off' => __( 'No', 'widgetkit-pro' ),
                'return_value' => 'yes',
            ]
        );
        $this->add_control(
            'header_highlighted_title_color',
            [
                'label' => __( 'Highlighted Title Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .active-post-content a h3' => 'color: {{VALUE}}',
                ],
                'condition' => [
                    'custom_color' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'header_highlighted_meta_color',
            [
                'label' => __( 'Highlighted Meta Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .active-post-content .post-cat, {{WRAPPER}} .active-post-content .post-cat a' => 'color: {{VALUE}}',
                ],
                'condition' => [
                    'custom_color' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'header_regular_title_color',
            [
                'label' => __( 'Regular Title Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .regular-post .post-title h3' => 'color: {{VALUE}}',
                ],
                'condition' => [
                    'custom_color' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'header_regular_meta_color',
            [
                'label' => __( 'Regular Meta Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .regular-post .post-meta span' => 'color: {{VALUE}}',
                    '{{WRAPPER}} .regular-post .post-cat, {{WRAPPER}} .regular-post .post-cat a' => 'color: {{VALUE}}',
                ],
                'condition' => [
                    'custom_color' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'ajax_overlay_color',
            [
                'label' => __( 'Ajax Overlay Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .dynamic-header-tab-2 .active-post-content .ajax-content' => 'background: {{VALUE}} !important',
                ],
                'condition' => [
                    'custom_color' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'ajax_overlay_dot_color',
            [
                'label' => __( 'Ajax Overlay Dot Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .dynamic-header-tab-2 .active-post-content .ajax-content .lds-ellipsis div' => 'background: {{VALUE}}',
                ],
                'condition' => [
                    'custom_color' => 'yes',
                ],
            ]
        );
        $this->end_controls_section();
    }

    protected function render()
    {
        $settings = $this->get_settings();
        $layout = widgetkit_pro_array_get($settings, 'layout_style');
        $layout_header_enable = widgetkit_pro_array_get($settings, 'layout_header_enable');
        $layout_title = widgetkit_pro_array_get($settings, 'layout_title');

        $cat_id = widgetkit_pro_array_get($settings, 'cat_single_id');
        $query_post_type = widgetkit_pro_array_get($settings, 'query_post_type');
        $post_column = widgetkit_pro_array_get($settings, 'post_column');
        $post_column_gap = widgetkit_pro_array_get($settings, 'post_column_gap');
        $post_num_to_show = widgetkit_pro_array_get($settings, 'post_num_to_show');
        $regular_post_style = widgetkit_pro_array_get($settings, 'regular_post_style');

        $featured_thumbnail_size = widgetkit_pro_array_get($settings, 'featured_thumbnail_size');
        $regular_thumbnail_size = widgetkit_pro_array_get($settings, 'regular_thumbnail_size');
        $title_word = widgetkit_pro_array_get($settings, 'title_word');




        ?>
    <?php
        include( plugin_dir_path( __FILE__ ) . '../layout/dynamic-header-tab/'.$layout.'.php');

        ?>

    <?php
    }
    protected function _content_template()
    {
    }
}
