<?php

namespace ThemesflatAddons\Widgets;

use Elementor\Repeater;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Scheme_Color;
use Elementor\Group_Control_Typography;
use Elementor\Scheme_Typography;
use Elementor\Utils;
use Elementor\Icons_Manager;

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


class TFL_Tabs_Widget extends TFL_Widget_Base {

    public function get_name() {
        return 'tfl-tabs';
    }

    public function get_title() {
        return __('Tabs', 'themesflat-addons');
    }

    public function get_icon() {
        return 'tfl-icon-tabs2';
    }

    public function get_categories() {
        return array('themesflat-addons');
    }

    public function get_script_depends() {
        return [
            'tfl-frontend-scripts',
            'tfl-tabs-scripts'
        ];
    }

    protected function register_controls() {

        $this->start_controls_section(
            'section_tabs',
            [
                'label' => __('Tabs', 'themesflat-addons'),
            ]
        );


        $repeater = new Repeater();



        $repeater->add_control(
            'list_content_type',
            [
                'label' => esc_html__( 'Content type', 'themesflat-addons' ),
                'type' => Controls_Manager::SELECT,
                'default' => 'content',
                'options' => [
                    'content'  => esc_html__( 'Content', 'themesflat-addons' ),
                    'template' => esc_html__( 'Template', 'themesflat-addons' ),
                ],
            ]
        );

        $repeater->add_control(
            'list_content_template',
            [
                'label' => esc_html__( 'Template', 'themesflat-addons' ),
                'type' => Controls_Manager::SELECT,
                'options' => tf_get_template_elementor(),
                'condition' => [
                    'list_content_type' => 'template',
                ],
            ]
        );


        $repeater->add_control(
            'tab_id',
            [

                'label' => __('Tab ID', 'themesflat-addons'),
                'description' => __('The Tab ID is required to link to a tab. It must be unique across the page, must begin with a letter and may be followed by any number of letters, digits, hyphens or underscores.', 'themesflat-addons'),
                'type' => Controls_Manager::TEXT,
            ]
        );

        $repeater->add_control(
            'icon_type',
            [

                'label' => __('Tab Icon Type', 'themesflat-addons'),
                'type' => Controls_Manager::SELECT,
                'default' => 'none',
                'options' => [
                    'none' => __('None', 'themesflat-addons'),
                    'icon' => __('Icon', 'themesflat-addons'),
                    'icon_image' => __('Icon Image', 'themesflat-addons'),
                ],
            ]
        );

        $repeater->add_control(
            'icon_image',
            [

                'label' => __('Tab Image', 'themesflat-addons'),
                'type' => Controls_Manager::MEDIA,
                'default' => [
                    'url' => Utils::get_placeholder_image_src(),
                ],
                'label_block' => true,
                'condition' => [
                    'icon_type' => 'icon_image',
                ],
            ]
        );

        $repeater->add_control(
            'selected_icon',

            [

                'label' => __('Tab Icon', 'themesflat-addons'),
                'type' => Controls_Manager::ICONS,
                'label_block' => true,
                'default' => [
                    'value' => 'fas fa-home',
                    'library' => 'fa-solid',
                ],
                'condition' => [
                    'icon_type' => 'icon',
                ],
                'fa4compatibility' => 'icon',
            ]
        );

        $repeater->add_control(
            'tab_title',
            [

                'label' => __('Tab Title & Content', 'themesflat-addons'),
                'type' => Controls_Manager::TEXT,
                'default' => __('Tab Title', 'themesflat-addons'),
                'label_block' => true,
                'dynamic' => [
                    'active' => true,
                ],
            ]
        );

        $repeater->add_control(
            'tab_content',
            [

                'label' => __('Tab Content', 'themesflat-addons'),
                'type' => Controls_Manager::WYSIWYG,
                'default' => __('Tabs Content', 'themesflat-addons'),
                'show_label' => false,
                'dynamic' => [
                    'active' => true,
                ],
                'condition' => [
                    'list_content_type' => 'content',
                ],
            ]
        );

        $this->add_control(
            'tabs',
            [
                'label' => __('Tab Panes', 'themesflat-addons'),
                'type' => Controls_Manager::REPEATER,
                'separator' => 'before',
                'default' => [
                    [
                        'tab_title' => __('Tab #1', 'themesflat-addons'),
                        'tab_content' => __('I am tab content. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'themesflat-addons'),
                    ],
                    [
                        'tab_title' => __('Tab #2', 'themesflat-addons'),
                        'tab_content' => __('I am tab content. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'themesflat-addons'),
                    ],
                    [
                        'tab_title' => __('Tab #3', 'themesflat-addons'),
                        'tab_content' => __('I am tab content. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'themesflat-addons'),
                    ],
                ],
                'fields' => $repeater->get_controls(),
                'title_field' => '{{{ tab_title }}}',
            ]
        );

        $this->end_controls_section();


        $this->start_controls_section(
            'section_tab_title',
            [
                'label' => __('Tab Title', 'themesflat-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'title_color',
            [
                'label' => __('Tab Title Color', 'themesflat-addons'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-nav .tfl-tab a .tfl-tab-title' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'active_title_color',
            [
                'label' => __('Active Tab Title Color', 'themesflat-addons'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-nav .tfl-tab.tfl-active a .tfl-tab-title' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'hover_title_color',
            [
                'label' => __('Hover Tab Title Color', 'themesflat-addons'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-nav .tfl-tab:hover a .tfl-tab-title' => 'color: {{VALUE}};',
                ],
            ]
        );



        $this->add_control(
            'title_spacing',
            [
                'label' => __('Tab Title Padding', 'themesflat-addons'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-nav .tfl-tab a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'isLinked' => false
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'title_typography',
                'selector' => '{{WRAPPER}} .tfl-tabs .tfl-tab-nav .tfl-tab .tfl-tab-title',
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_tab_content',
            [
                'label' => __('Tab Content', 'themesflat-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'content_spacing',
            [
                'label' => __('Tab Content Padding', 'themesflat-addons'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-panes .tfl-tab-pane' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'isLinked' => false
            ]
        );
        $this->add_control(
            'content_color',
            [
                'label' => __('Color', 'themesflat-addons'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-panes' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'content_typography',
                'selector' => '{{WRAPPER}} .tfl-tabs .tfl-tab-panes .tfl-tab-pane',
            ]
        );


        $this->end_controls_section();

        $this->start_controls_section(
            'section_icon_styling',
            [
                'label' => __('Icons', 'themesflat-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );


        $this->add_control(
            'icon_size',
            [
                'label' => __('Icon or Icon Image size in pixels', 'themesflat-addons'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', '%', 'em'],
                'range' => [
                    'px' => [
                        'min' => 10,
                        'max' => 256,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-nav .tfl-tab .tfl-image-wrapper img' => 'width: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-nav .tfl-tab .tfl-icon-wrapper i' => 'font-size: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'icon_color',
            [
                'label' => __('Icon Color', 'themesflat-addons'),
                'type' => Controls_Manager::COLOR,
                'default' => '',
                'selectors' => [
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-nav .tfl-tab .tfl-icon-wrapper i' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'active_icon_color',
            [
                'label' => __('Active Tab Icon Color', 'themesflat-addons'),
                'type' => Controls_Manager::COLOR,
                'default' => '',
                'selectors' => [
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-nav .tfl-tab.tfl-active .tfl-icon-wrapper i' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'hover_icon_color',
            [
                'label' => __('Hover Tab Icon Color', 'themesflat-addons'),
                'type' => Controls_Manager::COLOR,
                'default' => '',
                'selectors' => [
                    '{{WRAPPER}} .tfl-tabs .tfl-tab-nav .tfl-tab:hover a .tfl-icon-wrapper i' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->end_controls_section();
    }


    protected function render() {

        $settings = $this->get_settings_for_display();

        $settings = apply_filters('tfl_tabs_' . $this->get_id() . '_settings', $settings);

        $args['settings'] = $settings;

        $args['widget_instance'] = $this;

        tfl_get_template_part("addons/tabs/loop", $args);

    }

    protected function content_template() {

    }

}