<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Toggle_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Content_Toggle extends Widget_Base {

    use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Toggle_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
    use \Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;

	public function __construct( $data = [], $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = [
        'name'       => 'sifency-content-toggle',
        'title'      => 'Sifency Content Toggle',
        'icon'       => 'eicon-adjust',
        'category' => [ 'sifency-advance-widgets' ],
        'script_depends'    => [ 'sifency-toggle-script' ],
        'style_depends'     => [ 'sifency-toggle-style', 'sifency-typo-style' ],
		'keywords'      => ['toggle', 'switch', 'content switcher', 'pricing toggle', 'tab switch'],
    ];}

	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_settings',
			[ 'label' => esc_html__( 'Toggle Settings', 'sifency-addons' ) ]
		);
        $this->add_control(
			'layout_type',
			[
				'label' => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'round',
				'options' => [
					'round'   => esc_html__( '1. Round Switch (Standard)', 'sifency-addons' ),
					'square'  => esc_html__( '2. Square / Sharp', 'sifency-addons' ),
					'pill'    => esc_html__( '3. Pill Tabs', 'sifency-addons' ),
					'neon'    => esc_html__( '4. Neon / Dark Mode', 'sifency-addons' ),
					'minimal' => esc_html__( '5. Minimal Text', 'sifency-addons' ),
				],
			]
		);
        $this->add_control(
			'switch_effect',
			[
				'label' => esc_html__( 'Content Animation', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'fade',
				'options' => [
					'fade'  => esc_html__( 'Fade', 'sifency-addons' ),
					'slide' => esc_html__( 'Slide Horizontal', 'sifency-addons' ),
					'flip'  => esc_html__( '3D Flip', 'sifency-addons' ),
				],
			]
		);
        $this->add_responsive_control(
			'alignment',
			[
				'label' => esc_html__( 'Alignment', 'sifency-addons' ),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'flex-start' => [ 'title' => 'Left', 'icon' => 'eicon-h-align-left' ],
					'center' => [ 'title' => 'Center', 'icon' => 'eicon-h-align-center' ],
					'flex-end' => [ 'title' => 'Right', 'icon' => 'eicon-h-align-right' ],
				],
                'default' => 'center',
				'selectors' => [
					'{{WRAPPER}} .sifency-toggle-nav' => 'justify-content: {{VALUE}};',
				],
			]
		);

        $this->end_controls_section();

        $this->start_controls_section( 'section_content_a', [ 'label' => 'Content A (Primary)' ] );
        
        $this->add_control( 'label_a', [ 'label' => 'Switch Label', 'type' => Controls_Manager::TEXT, 'default' => 'Monthly' ] );
        
        $this->add_control(
			'source_a',
			[
				'label' => esc_html__( 'Source', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'editor',
				'options' => [ 'editor' => 'Editor', 'template' => 'Elementor Template' ],
			]
		);
        $this->add_control( 'editor_a', [ 'label' => 'Content', 'type' => Controls_Manager::WYSIWYG, 'default' => 'Content A goes here.', 'condition' => ['source_a'=>'editor'] ] );
        
        $this->add_control(
			'template_a',
			[
				'label' => esc_html__( 'Select Template', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'default' => '0',
				'options' => function_exists('sifency_addons_get_elementor_templates') ? sifency_addons_get_elementor_templates() : [],
				'condition' => [ 'source_a' => 'template' ],
			]
		);

        $this->end_controls_section();

        $this->start_controls_section( 'section_content_b', [ 'label' => 'Content B (Secondary)' ] );
        
        $this->add_control( 'label_b', [ 'label' => 'Switch Label', 'type' => Controls_Manager::TEXT, 'default' => 'Yearly' ] );
        
        $this->add_control(
			'source_b',
			[
				'label' => esc_html__( 'Source', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'editor',
				'options' => [ 'editor' => 'Editor', 'template' => 'Elementor Template' ],
			]
		);
        $this->add_control( 'editor_b', [ 'label' => 'Content', 'type' => Controls_Manager::WYSIWYG, 'default' => 'Content B goes here.', 'condition' => ['source_b'=>'editor'] ] );
        
        $this->add_control(
			'template_b',
			[
				'label' => esc_html__( 'Select Template', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'default' => '0',
				'options' => function_exists('sifency_addons_get_elementor_templates') ? sifency_addons_get_elementor_templates() : [],
				'condition' => [ 'source_b' => 'template' ],
			]
		);
        $this->end_controls_section();
		$this->add_sifency_addons_help_controls();
        $this->add_sifency_addons_toggle_style_controls();
        $this->add_sifency_addons_master_typography_controls([
            'prefix'   => 'label_style',
            'label'    => 'Switch Label Style',
            'selector' => '.sifency-label-style',
			'color_only' => true,
			'exclude' => ['alignment', 'transition', 'bg', 'radius', 'shadow', 'stroke', 'border'],
        ]);
        $this->add_sifency_addons_master_typography_controls([
            'prefix'   => 'content_style',
            'label'    => 'Editor Content Style',
            'selector' => '.sifency-content-style',
			'color_only' => true,
			'exclude' => [ 'alignment', 'transition', 'tabs', 'bg', 'radius', 'shadow', 'stroke'],
			'condition' => ['source_a'=>'editor','source_b'=>'editor'],
        ]);
        $this->add_sifency_addons_item_style_controls([
			'label'    => 'Content Wrapper',
			'exclude' => ['tabs', 'transition'],
			'selector' => '.sifency-toggle-content-wrap',
		]);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->add_Sifency_Addons_Custom_CSS_Controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$this->sifency_addons_render_custom_css();
        $this->add_render_attribute( 'wrapper', 'class', 'sifency-toggle-wrapper' );
        $this->add_render_attribute( 'wrapper', 'class', 'sifency-toggle-layout-' . $settings['layout_type'] );
        $this->add_render_attribute( 'wrapper', 'class', 'sifency-toggle-effect-' . $settings['switch_effect'] );

		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
            
            <div class="sifency-toggle-nav">
                
                <span class="sifency-content-toggle-label sifency-label-style label-a active" data-target="a"><?php echo esc_html($settings['label_a']); ?></span>
                
                <label class="sifency-content-toggle-switch">
                    <input type="checkbox" class="sifency-content-toggle-checkbox">
                    <span class="toggle-slider round"></span>
                </label>

                <span class="sifency-content-toggle-label sifency-label-style label-b" data-target="b"><?php echo esc_html($settings['label_b']); ?></span>

            </div>
            <div class="sifency-toggle-content-container">
                
                <div class="sifency-toggle-content-wrap content-a active">
                    <?php 
                    if ( 'template' === $settings['source_a'] && ! empty( $settings['template_a'] ) ) {
                         // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
						echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $settings['template_a'] );
                    } else {
                        echo wp_kses_post( $settings['editor_a'] );
                    }
                    ?>
                </div>

                <div class="sifency-toggle-content-wrap sifency-content-style content-b">
                    <?php 
                    if ( 'template' === $settings['source_b'] && ! empty( $settings['template_b'] ) ) {
                         // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
						echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $settings['template_b'] );
                    } else {
                        echo wp_kses_post( $settings['editor_b'] );
                    }
                    ?>
                </div>

            </div>

		</div>
		<?php
	}
}