<?php 
use Elementor\Plugin;
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;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Controls_Stack;

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

class Widgetkit_Pro_Smart_Toggle extends Widget_Base
{

    public function get_name()
    {
        return 'widgetkit-smart-toggle';
    }

    public function get_title()
    {
        return __('Smart Toggle', 'widgetkit-pro');
    }

    public function get_icon()
    {
        return 'eicon-post-navigation';
    }

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

    /**
	 * A list of style that the widgets is depended in
	 **/
	public function get_style_depends() {
        return [
            'widgetkit_bs',
            'widgetkit_pro_default',
        ];
    }
	/**
	 * A list of scripts that the widgets is depended in
	 **/
	public function get_script_depends() {
		return [ 
			'layout',
		 ];
	}

    protected function register_controls()
    {
        $args = [
            'post_type' => 'elementor_library',
            'posts_per_page' => -1,
        ];
        $page_templates = get_posts($args);
        $saved_templates = array();

        if (!empty($page_templates) && !is_wp_error($page_templates)) {
            foreach ($page_templates as $post) {
                $saved_templates[$post->ID] = $post->post_title;
            }
        }

#   start primary config region                
        /**
         * Primary Column
         */
        $this->start_controls_section(
            'section_control_toggle_primary',
            [
                'label' => __('Primary', 'widgetkit-pro'),
            ]
        );

            $this->add_control(
                'smart_toggle_primary_title',
                [
                    'label'       => __( 'Primary', 'widgetkit-pro' ),
                    'type'        => Controls_Manager::TEXT,
                    'default'     => __( 'Primary', 'widgetkit-pro' ),
                    'placeholder' => __( 'Type your primary text here', 'widgetkit-pro' ),
                ]
            );
            $this->add_control(
                'primary_content_type',
                [
                    'label'       => __( 'Type', 'widgetkit-pro' ),
                    'type' => Controls_Manager::SELECT,
                    'default' => 'content',
                    'options' => [
                        'content'  => __( 'Content', 'widgetkit-pro' ),
                        'saved_template'  => __( 'Saved Template', 'widgetkit-pro' ),
                    ],
                ]
            );
            $this->add_control(
                'primary_content',
                [
                    'label'       => __( 'Content', 'widgetkit-pro' ),
                    'type' => Controls_Manager::WYSIWYG,
                    'default' => esc_html__('In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.', 'widgetkit-pro'),
                    'condition' => [
                        'primary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_control(
                'primary_saved_template',
                [
                    'label'       => __( 'Saved Template', 'widgetkit-pro' ),
                    'type' => Controls_Manager::SELECT,
                    'options' => $saved_templates,
                    'condition' => [
                        'primary_content_type' => 'saved_template',
                    ],
                ]
            );
        $this->end_controls_section();
#   end primary config region         

#   start secondary config region        
        /**
         * Secondary Column
         */
        $this->start_controls_section(
            'section_control_toggle_secondary',
            [
                'label' => __('Secondary', 'widgetkit-pro'),
            ]
        );
            $this->add_control(
                'smart_toggle_secondary_title',
                [
                    'label'       => __( 'Secondary', 'widgetkit-pro' ),
                    'type'        => Controls_Manager::TEXT,
                    'default'     => __( 'Secondary', 'widgetkit-pro' ),
                    'placeholder' => __( 'Type your secondary text here', 'widgetkit-pro' ),
                ]
            );
            $this->add_control(
                'secondary_content_type',
                [
                    'label'       => __( 'Type', 'widgetkit-pro' ),
                    'type' => Controls_Manager::SELECT,
                    'default' => 'content',
                    'options' => [
                        'content'  => __( 'Content', 'widgetkit-pro' ),
                        'saved_template'  => __( 'Saved Template', 'widgetkit-pro' ),
                    ],
                ]
            );
            $this->add_control(
                'secondary_content',
                [
                    'label'       => __( 'Content', 'widgetkit-pro' ),
                    'type' => Controls_Manager::WYSIWYG,
                    'default' => esc_html__('Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna.', 'widgetkit-pro'),
                    'condition' => [
                        'secondary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_control(
                'secondary_saved_template',
                [
                    'label'       => __( 'Saved Template', 'widgetkit-pro' ),
                    'type' => Controls_Manager::SELECT,
                    'options' => $saved_templates,
                    'condition' => [
                        'secondary_content_type' => 'saved_template',
                    ],
                ]
            );
        
        $this->end_controls_section();
#   end secondary config region        
        
#   start toggle style region 
        /**
         * header
         */
        $this->start_controls_section(
            'section_style_header',
            [
                'label' => __( 'Toggle', 'widgetkit-pro' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
            $this->add_responsive_control(
                'toggle_alignment',
                [
                    'label'  => esc_html__( 'Alignment', 'widgetkit-pro' ),
                    'type'  => Controls_Manager::CHOOSE,
                    'default' => 'center',
                    'options' => [
                        'flex-start' => [
                            'title' => esc_html__( 'Left', 'widgetkit-pro' ),
                            'icon'  => 'fa fa-align-left',
                        ],
                        'center' => [
                            'title' => esc_html__( 'Center', 'widgetkit-pro' ),
                            'icon'  => 'fa fa-align-center',
                        ],
                        'flex-end' => [
                            'title' => esc_html__( 'Right', 'widgetkit-pro' ),
                            'icon'  => 'fa fa-align-right',
                        ],
                    ],
                    'selectors' => [
                        '{{WRAPPER}}  .smart-toggle-content .toggle-wrapper' => 'justify-content: {{VALUE}};',
                    ],
                ]
            );
            $this->add_control(
                'toggle_circle_color',
                [
                    'label' => __( 'Circle', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .toggle .switch' => 'background: {{VALUE}}',
                    ],
                ]
            );
            $this->add_control(
                'toggle_background_color_for_primary',
                [
                    'label' => __( 'Primary Backgroud', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .toggle' => 'background: {{VALUE}} !important',
                    ],
                ]
            );
            $this->add_control(
                'toggle_background_color_for_secondary',
                [
                    'label' => __( 'Secondary Backgroud', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .toggle .check:checked ~ .track' => 'box-shadow: inset 0 0 0 20px {{VALUE}} !important',
                    ],
                ]
            );
            $this->add_control(
                'toggle_border_color_for_secondary',
                [
                    'label' => __( 'Border', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .toggle .track' => 'box-shadow: inset 0 0 0 2px {{VALUE}} !important',
                    ],
                ]
            );
            $this->add_responsive_control(
                'toggle_spacing',
                [
                    'label' => esc_html__( 'Space between Title', 'widgetkit-pro' ),
                    'type'  => Controls_Manager::DIMENSIONS,
                    'size_units' => [ 'px' ],
                    'selectors'  => [
                        '{{WRAPPER}} .smart-toggle-content .toggle' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );
            $this->add_control(
                'toggle_spacing_bottom',
                [
                    'label' => __( 'Spacing Bottom', 'widgetkit-pro' ),
                    'type' => Controls_Manager::SLIDER,
                    'default' => [
                        'size' =>20,
                    ],
                    'range' => [
                        'px' => [
                            'min' => -50,
                            'max' => 100,
                            'step' => 1,
                        ],
                    ],
                    'size_units' => [ 'px' ],
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .toggle-wrapper' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                    ],
                ]
            );
        $this->end_controls_section();
#   end toggle style region

#   start primary style region
        /**
         * Primary Column style
         */
        $this->start_controls_section(
            'section_style_primary_column',
            [
                'label' => __( 'Primary', 'widgetkit-pro' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
            /**
             * Title
             */
            $this->add_control(
                'smart_toggle_primary_title_style',
                [
                'label' => __( 'Title', 'widgetkit-pro' ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
                ]
            );
            $this->add_control(
                'primary_title_color',
                [
                    'label' => __( 'Color', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .primary-title' => 'color: {{VALUE}}',
                    ],
                ]
            );
            $this->add_group_control(
                Group_Control_Typography::get_type(),
                [
                    'label' => __( 'Typography', 'widgetkit-pro' ),
                    'name' => 'primary_title_typography',
                    'selector' => '{{WRAPPER}} .smart-toggle-content .primary-title',
                ]
            );
            $this->add_control(
                'primary_title_background_color',
                [
                    'label' => __( 'Background', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .primary-title' => 'background: {{VALUE}}',
                    ],
                ]
            );
            $this->add_group_control(
                Group_Control_Border::get_type(), 
                [
                    'name'          => 'primary_title_border',
                    'selector'      => '{{WRAPPER}} .smart-toggle-content .primary-title',
                ]
            );
            $this->add_control(
                'primary_title_border_radius',
                [
                    'label'   => esc_html__( 'Border Radius', 'widgetkit-pro' ),
                    'type'    => Controls_Manager::SLIDER,
                    'range'  => [
                        'px' => [
                            'min' => 0,
                            'max' => 100,
                        ],
                    ],
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .primary-title' => 'border-radius: {{SIZE}}{{UNIT}};',
                    ],
                ]
            );
            $this->add_responsive_control(
                'primary_title_padding',
                [
                    'label' => esc_html__( 'Padding', 'widgetkit-pro' ),
                    'type'  => Controls_Manager::DIMENSIONS,
                    'size_units' => [ 'px', '%' ],
                    'selectors'  => [
                        '{{WRAPPER}} .smart-toggle-content .primary-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );
            /**
             * content
             */
            $this->add_control(
                'primary_content_style',
                [
                'label' => __( 'Content', 'widgetkit-pro' ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
                'condition' => [
                    'primary_content_type' => 'content',
                ],
                ]
            );
            $this->add_responsive_control(
                'primary_content_alignment',
                [
                    'label'  => esc_html__( 'Alignment', 'widgetkit-pro' ),
                    'type'  => Controls_Manager::CHOOSE,
                    'default' => 'center',
                    'options' => [
                        'left' => [
                            'title' => esc_html__( 'Left', 'widgetkit-pro' ),
                            'icon'  => 'fa fa-align-left',
                        ],
                        'center' => [
                            'title' => esc_html__( 'Center', 'widgetkit-pro' ),
                            'icon'  => 'fa fa-align-center',
                        ],
                        'right' => [
                            'title' => esc_html__( 'Right', 'widgetkit-pro' ),
                            'icon'  => 'fa fa-align-right',
                        ],
                    ],
                    'selectors' => [
                        '{{WRAPPER}}  .smart-toggle-content .active-primary' => 'text-align: {{VALUE}};',
                    ],
                    'condition' => [
                        'primary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_control(
                'primary_content_color',
                [
                    'label' => __( 'Color', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .primary-content' => 'color: {{VALUE}}',
                    ],
                    'condition' => [
                        'primary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_group_control(
                Group_Control_Typography::get_type(),
                [
                    'label' => __( 'Typography', 'widgetkit-pro' ),
                    'name' => 'primary_content_typography',
                    'selector' => '{{WRAPPER}} .smart-toggle-content .primary-content',
                    'condition' => [
                        'primary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_control(
                'primary_content_background_color',
                [
                    'label' => __( 'Background', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .primary-content' => 'background: {{VALUE}}',
                    ],
                    'condition' => [
                        'primary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_group_control(
                Group_Control_Border::get_type(), 
                [
                    'name'          => 'primary_content_border',
                    'selector'      => '{{WRAPPER}} .smart-toggle-content .primary-content',
                    'condition' => [
                        'primary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_responsive_control(
                'primary_content_padding',
                [
                    'label' => esc_html__( 'Padding', 'widgetkit-pro' ),
                    'type'  => Controls_Manager::DIMENSIONS,
                    'size_units' => [ 'px', '%' ],
                    'selectors'  => [
                        '{{WRAPPER}} .smart-toggle-content .primary-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                    'condition' => [
                        'primary_content_type' => 'content',
                    ],
                ]
            );
        $this->end_controls_section();
#   end primary style region        

#   start secondary style region
        /**
         * Secondary Column style
         */
        $this->start_controls_section(
            'section_style_secondary_column',
            [
                'label' => __( 'Secondary', 'widgetkit-pro' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
            /**
             * Title
             */
            $this->add_control(
                'secondary_title_style',
                [
                'label' => __( 'Title', 'widgetkit-pro' ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
                ]
            );
            $this->add_control(
                'secondary_title_color',
                [
                    'label' => __( 'Color', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .secondary-title' => 'color: {{VALUE}}',
                    ],
                ]
            );
            $this->add_group_control(
                Group_Control_Typography::get_type(),
                [
                    'label' => __( 'Typography', 'widgetkit-pro' ),
                    'name' => 'secondary_title_typography',
                    'selector' => '{{WRAPPER}} .smart-toggle-content .secondary-title',
                ]
            );
            $this->add_control(
                'secondary_title_background_color',
                [
                    'label' => __( 'Background', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .secondary-title' => 'background: {{VALUE}}',
                    ],
                ]
            );
            $this->add_group_control(
                Group_Control_Border::get_type(), 
                [
                    'name'          => 'secondary_title_border',
                    'selector'      => '{{WRAPPER}} .smart-toggle-content .secondary-title',
                ]
            );
            $this->add_control(
                'secondary_title_border_radius',
                [
                    'label'   => esc_html__( 'Border Radius', 'widgetkit-pro' ),
                    'type'    => Controls_Manager::SLIDER,
                    'range'  => [
                        'px' => [
                            'min' => 0,
                            'max' => 100,
                        ],
                    ],
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .secondary-title' => 'border-radius: {{SIZE}}{{UNIT}};',
                    ],
                ]
            );
            $this->add_responsive_control(
                'secondary_title_padding',
                [
                    'label' => esc_html__( 'Padding', 'widgetkit-pro' ),
                    'type'  => Controls_Manager::DIMENSIONS,
                    'size_units' => [ 'px', '%' ],
                    'selectors'  => [
                        '{{WRAPPER}} .smart-toggle-content .secondary-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );
            /**
             * content
             */
            $this->add_control(
                'secondary_content_style',
                [
                'label' => __( 'Content', 'widgetkit-pro' ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
                'condition' => [
                    'secondary_content_type' => 'content',
                ],
                ]
            );
            $this->add_responsive_control(
                'secondary_content_alignment',
                [
                    'label'  => esc_html__( 'Alignment', 'widgetkit-pro' ),
                    'type'  => Controls_Manager::CHOOSE,
                    'default' => 'center',
                    'options' => [
                        'left' => [
                            'title' => esc_html__( 'Left', 'widgetkit-pro' ),
                            'icon'  => 'fa fa-align-left',
                        ],
                        'center' => [
                            'title' => esc_html__( 'Center', 'widgetkit-pro' ),
                            'icon'  => 'fa fa-align-center',
                        ],
                        'right' => [
                            'title' => esc_html__( 'Right', 'widgetkit-pro' ),
                            'icon'  => 'fa fa-align-right',
                        ],
                    ],
                    'selectors' => [
                        '{{WRAPPER}}  .smart-toggle-content .active-secondary' => 'text-align: {{VALUE}};',
                    ],
                    'condition' => [
                        'secondary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_control(
                'secondary_content_color',
                [
                    'label' => __( 'Color', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .secondary-content' => 'color: {{VALUE}}',
                    ],
                    'condition' => [
                        'secondary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_group_control(
                Group_Control_Typography::get_type(),
                [
                    'label' => __( 'Typography', 'widgetkit-pro' ),
                    'name' => 'secondary_content_typography',
                    'selector' => '{{WRAPPER}} .smart-toggle-content .secondary-content',
                    'condition' => [
                        'secondary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_control(
                'secondary_content_background_color',
                [
                    'label' => __( 'Background', 'widgetkit-pro' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .smart-toggle-content .secondary-content' => 'background: {{VALUE}}',
                    ],
                    'condition' => [
                        'secondary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_group_control(
                Group_Control_Border::get_type(), 
                [
                    'name'          => 'secondary_content_border',
                    'selector'      => '{{WRAPPER}} .smart-toggle-content .secondary-content',
                    'condition' => [
                        'secondary_content_type' => 'content',
                    ],
                ]
            );
            $this->add_responsive_control(
                'secondary_content_padding',
                [
                    'label' => esc_html__( 'Padding', 'widgetkit-pro' ),
                    'type'  => Controls_Manager::DIMENSIONS,
                    'size_units' => [ 'px', '%' ],
                    'selectors'  => [
                        '{{WRAPPER}} .smart-toggle-content .secondary-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                    'condition' => [
                        'secondary_content_type' => 'content',
                    ],
                ]
            );
        $this->end_controls_section();
#   end secondary style region        

    }


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

        ?>

        <div 
            id="wk-smart-toggle-<?php echo $this->get_id(); ?>" 
            class="wk-smart-toggle-<?php echo $this->get_id(); ?> wk-smart-toggle"
        >
            <?php include( plugin_dir_path( __FILE__ ) . '../layout/smart-toggle.php'); ?>
        </div>
    <?php
    }
    protected function content_template()
    {
    }
}
