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

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

if(!function_exists('widgetkit_pro_widgetkit_pro_array_get')) {
    function widgetkit_pro_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_Tiles_Layout extends Widget_Base
{
    public $all_settings = [];
    public static $a = [];

    public function get_name()
    {
        return 'magmax-tiles-layout';
    }

    public function get_title()
    {
        return __('Tiles Layout', 'widgetkit-pro');
    }

    public function get_icon()
    {
        return 'eicon-inner-section';
    }

    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 [ 'imagesloaded' ];
        }
    */

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



        $this->start_controls_section(
            'section_tab',
            [
                'label' => __('Layout', 'widgetkit-pro'),
                'class' => __('magmax-tiles-layout', 'widgetkit-pro'),
            ]
        );
        $this->add_control(
            'layout_design',
            [
                'label' => __( 'Layout Design', 'widgetkit-pro' ),
                'type' => Controls_Manager::CHOOSE,
                'default' => '5',
                'options' => [
                    '1'    => [
                        'title' => __( '1', 'widgetkit-pro' ),
                        'icon' => 'eicon-counter',
                    ],
                    '2'    => [
                        'title' => __( '2', 'widgetkit-pro' ),
                        'icon' => 'icon-blocks',
                    ],
                    '3' => [
                        'title' => __( '3', 'widgetkit-pro' ),
                        'icon' => 'fa fa-align-center',
                    ],
                    '4' => [
                        'title' => __( '4', 'widgetkit-pro' ),
                        'icon' => 'fa fa-align-right',
                    ],
                    '5' => [
                        'title' => __( '5', 'widgetkit-pro' ),
                        'icon' => 'fa fa-align-right',
                    ],
                    '6' => [
                        'title' => __( '6', 'widgetkit-pro' ),
                        'icon' => 'fa fa-align-right',
                    ],
                    '7' => [
                        'title' => __( '7', 'widgetkit-pro' ),
                        'icon' => 'fa fa-align-right',
                    ],
                    '8' => [
                        'title' => __( '8', 'widgetkit-pro' ),
                        'icon' => 'fa fa-align-right',
                    ],
                    '9' => [
                        'title' => __( '9', 'widgetkit-pro' ),
                        'icon' => 'fa fa-align-right',
                    ],
                    '10' => [
                        'title' => __( '10', 'widgetkit-pro' ),
                        'icon' => 'fa fa-align-right',
                    ],
                    '11' => [
                        'title' => __( '11', 'widgetkit-pro' ),
                        'icon' => 'fa fa-align-right',
                    ],
                ],
            ]
        );
        $this->add_control(
            'cat_single_id',
            [
                'label' => __( 'Category List', 'widgetkit-pro' ),
                'type' => Controls_Manager::SELECT,
                'options' => $cat_names,
            ]
        );
        $this->add_control(
            'post_num_to_show',
            [
                'label'   => __( 'Number of Post to Show', 'widgetkit-pro' ),
                'type'    => Controls_Manager::NUMBER,
                'default' => 5,
                'min'     => -1,
                'max'     => 100,
                'step'    => 1,
            ]
        );

        $this->add_control(
            'grid-gap',
            [
                'label' => __( 'Grid Gap', 'widgetkit-pro' ),
                'type' => Controls_Manager::SLIDER,
                'default' => [
                    'size' => 1,
                ],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 30,
                        'step' => 1,
                    ],
                    '%' => [
                        'min' => 0,
                        'max' => 5,
                    ],
                ],
                'size_units' => [ 'px', '%' ],
                'selectors' => [
                    '{{WRAPPER}} .tiles-layout .tiles-layout-wrapper' => 'grid-gap: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'section-height',
            [
                'label' => __( 'Section Height', 'widgetkit-pro' ),
                'type' => Controls_Manager::SLIDER,
                'default' => [
                    'size' => 548,
                ],
                'range' => [
                    'px' => [
                        'min' => 300,
                        'max' => 720,
                        'step' => 1,
                    ],
                ],
                'size_units' => [ 'px' ],
                'selectors' => [
                    '{{WRAPPER}} .tiles-layout .tiles-layout-wrapper' => 'min-height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'show_excerpt',
            [
                'label' => __( 'Show Excerpt in highlighted article', 'widgetkit-pro' ),
                'type' => Controls_Manager::SWITCHER,
                'default' => '',
                'description' => 'Show excerpt in highlighted article not small others ',
                'label_on' => __( 'Show', 'widgetkit-pro' ),
                'label_off' => __( 'Hide', 'widgetkit-pro' ),
                'return_value' => 'yes',
            ]
        );
        $this->add_control(
            'excerpt_word_count',
            [
                'label'   => __( 'Excerpt Word count', 'widgetkit-pro' ),
                'description' => 'Show excerpt word in the article',
                'type'    => Controls_Manager::NUMBER,
                'default' => 10,
                'min'     => 10,
                'max'     => 200,
                'step'    => 1,
                'condition' => [
                    'show_excerpt' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'show_cat_tag',
            [
                'label' => __( 'Category Tag for article', 'widgetkit-pro' ),
                'type' => Controls_Manager::SWITCHER,
                'default' => '',
                'description' => 'Show category tag for article above the title',
                'label_on' => __( 'Show', 'widgetkit-pro' ),
                'label_off' => __( 'Hide', 'widgetkit-pro' ),
                'return_value' => 'yes',
            ]
        );
        $this->add_control(
            'show_full_article_link',
            [
                'label' => __( 'Show full article link', 'widgetkit-pro' ),
                'type' => Controls_Manager::SWITCHER,
                'default' => '',
                'description' => 'Show full article link in the bottom of the card',
                'label_on' => __( 'Show', 'widgetkit-pro' ),
                'label_off' => __( 'Hide', 'widgetkit-pro' ),
                'return_value' => 'yes',
            ]
        );

        $this->end_controls_section();


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


        $this->start_controls_section(
            'content_style',
            [
                'label' => __( 'Content', 'widgetkit-pro' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        $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(
            'title_color',
            [
                'label' => __( 'Title Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .single-column .post-title h3' => 'color: {{VALUE}}',
                ],
                'condition' => [
                    'custom_color' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'meta_color',
            [
                'label' => __( 'Meta Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .single-column span' => 'color: {{VALUE}}',
                ],
                'condition' => [
                    'custom_color' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'content_color',
            [
                'label' => __( 'Content Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .single-column .post-excerpt' => 'color: {{VALUE}}',
                ],
                'condition' => [
                    'custom_color' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'overlay_style',
            [
                'label'       => __( 'Overlay Style', 'widgetkit-pro' ),
                'type' => Controls_Manager::SELECT,
                'default' => 'bg-overlay',
                'options' => [
                    'bg-overlay'  => __( 'Overlay', 'widgetkit-pro' ),
                    'bg-gradient'  => __( 'Gradient', 'widgetkit-pro' ),
                ],
            ]
        );
        $this->add_control(
            'background_overlay',
            [
                'label' => __( 'Background Overlay', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .single-item-wrapper:before' => 'background-color: {{VALUE}}',
                ],
                'condition' => [
                    'overlay_style' => 'bg-overlay',
                ],
            ]
        );

        $this->add_control(
            'gradient_1',
            [
                'label' => __( 'Gradient 1', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .single-item-wrapper:before' => 'background: linear-gradient(180deg, rgba(0, 0, 0, 0.1), {{VALUE}})',
                ],
                'condition' => [
                    'overlay_style' => 'bg-gradient',
                ],
            ]
        );
        $this->end_controls_section();



        $this->start_controls_section(
            'link_style',
            [
                'label' => __( 'Link', 'widgetkit-pro' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'link_color',
            [
                'label' => __( 'Link Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .single-post-link a' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_control(
            'link_border_color',
            [
                'label' => __( 'Link Border Color', 'widgetkit-pro' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .single-post-link a:after' => 'background-color: {{VALUE}}',
                ],
            ]
        );
        $this->end_controls_section();

    }


    protected function render()
    {
        $settings = $this->get_settings();
        $layout_header_enable = widgetkit_pro_widgetkit_pro_array_get($settings, 'layout_header_enable');
        $show_cat_tag = widgetkit_pro_widgetkit_pro_array_get($settings, 'show_cat_tag');
        $layout_title = widgetkit_pro_widgetkit_pro_array_get($settings, 'layout_title');
        $cat_single_id = widgetkit_pro_widgetkit_pro_array_get($settings, 'cat_single_id');
        $post_num_to_show = widgetkit_pro_widgetkit_pro_array_get($settings, 'post_num_to_show');
        $selected_layout = widgetkit_pro_widgetkit_pro_array_get($settings, 'layout_design');
        $show_excerpt = widgetkit_pro_widgetkit_pro_array_get($settings, 'show_excerpt');
        $excerpt_word_count = widgetkit_pro_widgetkit_pro_array_get($settings, 'excerpt_word_count');
        $full_article_link = widgetkit_pro_widgetkit_pro_array_get($settings, 'show_full_article_link');
        /**
         * term meta value
         * ================
         */
        $tax_term = get_term_meta($cat_single_id);
        // $tax_image = widgetkit_pro_widgetkit_pro_array_get($tax_term, 'magmax_tax_image')[0];
        // $tax_tag_bg_color = widgetkit_pro_widgetkit_pro_array_get($tax_term, 'magmax_tax_tag_bg_color')[0];

    ?>

        <div id="tiles-layout">
            <?php
            include( plugin_dir_path( dirname(__FILE__) ) . 'layout/tiles/tiles-'.$selected_layout.'.php');
            ?>
        </div>
    <?php
    }
    protected function _content_template()
    {
    }
}
