<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Toggle_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;

class Sifency_Addons_Widget_Pricing_Calculator extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Toggle_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-pricing-calculator',
			'title'          => 'Sifency Pricing Calculator',
			'icon'           => 'eicon-price-table',
			'category'       => 'sifency-business-widgets',
			'keywords'       => array( 'pricing', 'calculator', 'price', 'table', 'sifency' ),
			'style_depends'  => array( 'sifency-pricing-calculator-style', 'sifency-typo-style' ),
			'script_depends' => array( 'sifency-pricing-calculator-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_config',
			array(
				'label' => esc_html__( 'Calculator Config', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'classic',
				'options' => array(
					'classic' => esc_html__( '1. Classic Card', 'sifency-addons' ),
					'side'    => esc_html__( '2. Split / Side-by-Side', 'sifency-addons' ),
					'glass'   => esc_html__( '3. Glassmorphism', 'sifency-addons' ),
					'neon'    => esc_html__( '4. Dark / Neon', 'sifency-addons' ),
					'soft'    => esc_html__( '5. Soft / Neumorphism', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'currency_symbol',
			array(
				'label'   => esc_html__( 'Currency Symbol', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => '$',
			)
		);
		$this->add_control(
			'base_price',
			array(
				'label'   => esc_html__( 'Base Price (Starting)', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 0,
			)
		);

		// --- REPEATER FOR INPUTS ---
		$repeater = new Repeater();
		$repeater->add_control(
			'field_label',
			array(
				'label'   => esc_html__( 'Label', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Item Name',
			)
		);
		$repeater->add_control(
			'field_type',
			array(
				'label'   => esc_html__( 'Input Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'slider',
				'options' => array(
					'slider' => esc_html__( 'Range Slider', 'sifency-addons' ),
					'switch' => esc_html__( 'Toggle Switch', 'sifency-addons' ),
					'number' => esc_html__( 'Quantity Input', 'sifency-addons' ),
				),
			)
		);
		$repeater->add_control(
			'cost_value',
			array(
				'label'       => esc_html__( 'Cost (Per Unit / Fixed)', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'default'     => 10,
				'description' => 'Slider/Number: Price per unit. Switch: Fixed price to add.',
			)
		);

		// Range Settings
		$repeater->add_control(
			'min_val',
			array(
				'label'     => 'Min',
				'type'      => Controls_Manager::NUMBER,
				'default'   => 0,
				'condition' => array( 'field_type' => 'slider' ),
			)
		);
		$repeater->add_control(
			'max_val',
			array(
				'label'     => 'Max',
				'type'      => Controls_Manager::NUMBER,
				'default'   => 100,
				'condition' => array( 'field_type' => 'slider' ),
			)
		);
		$repeater->add_control(
			'step_val',
			array(
				'label'     => 'Step',
				'type'      => Controls_Manager::NUMBER,
				'default'   => 1,
				'condition' => array( 'field_type' => 'slider' ),
			)
		);
		$repeater->add_control(
			'default_val',
			array(
				'label'   => 'Default Value',
				'type'    => Controls_Manager::NUMBER,
				'default' => 0,
			)
		);

		$this->add_control(
			'items',
			array(
				'label'       => esc_html__( 'Calculator Fields', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'field_label' => 'Number of Pages',
						'field_type'  => 'slider',
						'cost_value'  => 50,
						'max_val'     => 20,
						'default_val' => 5,
					),
					array(
						'field_label' => 'SEO Optimization',
						'field_type'  => 'switch',
						'cost_value'  => 100,
					),
				),
				'title_field' => '{{{ field_label }}}',
			)
		);
		$this->add_control( 'hr_btn', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_control(
			'btn_text',
			array(
				'label'   => 'Button Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Purchase Now',
			)
		);
		$this->add_control(
			'btn_link',
			array(
				'label' => 'Link',
				'type'  => Controls_Manager::URL,
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- STYLE SECTION ---
		$this->start_controls_section(
			'style_inputs',
			array(
				'label' => esc_html__( 'Inputs & Sliders', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'accent_color',
			array(
				'label'     => 'Accent Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => '#101010',
				'selectors' => array(
					'{{WRAPPER}} .sifency-pc-range::-webkit-slider-thumb' => 'background: {{VALUE}};',
					'{{WRAPPER}} .sifency-pc-range::-moz-range-thumb' => 'background: {{VALUE}};',
					'{{WRAPPER}} .sifency-pc-switch input:checked + .slider' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'wrapper_style',
				'label'    => 'Wrapper Area',
				'selector' => '.sifency-pc-wrapper',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'price_area_style',
				'label'    => 'Total Price Area',
				'selector' => '.sifency-pc-result-box',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'pc_item_style',
				'label'    => 'Calculator Item',
				'selector' => '.sifency-pricing-calculator-item',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'heading_style',
				'label'      => 'Label',
				'selector'   => '.sifency-heading-style',
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius', 'alignment' ),
				'color_only' => true,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'label_style',
				'label'      => 'Value',
				'selector'   => '.sifency-label-style',
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius', 'alignment' ),
				'color_only' => true,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Total Text',
				'selector'   => '.sifency-content-style',
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius', 'alignment' ),
				'color_only' => true,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'number_style',
				'label'      => 'Price Text',
				'selector'   => '.sifency-number-style',
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius', 'alignment' ),
				'color_only' => true,
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'    => 'button_style',
				'label'     => 'Button Style',
				'selector'  => '.sifency-pc-btn',
				'exclude'   => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius', 'alignment' ),
				'condition' => array( 'btn_text!' => '' ),
			)
		);
		$this->add_sifency_addons_toggle_style_controls(
			array(
				'prefix'    => 'pricing_toggle_',
				'label'     => esc_html__( 'Toggle Switch Design', 'sifency-addons' ),
				'selector'  => '.sifency-pc-switch',
				'selectors' => array(
					'nav'          => '.sifency-pc-control',
					'track'        => '.sifency-pc-switch .slider',
					'thumb'        => '.sifency-pc-switch .slider::before',
					'active_track' => '.sifency-pc-input:checked + .slider',
					'active_thumb' => '.sifency-pc-input:checked + .slider::before',
					'hover_track'  => '.sifency-pc-switch:hover .slider',
					'hover_thumb'  => '.sifency-pc-switch:hover .slider::before',
				),
			)
		);

		$this->start_controls_section(
			'section_pc_style',
			array(
				'label' => esc_html__( 'Price Controller', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'pc_input_bg',
			array(
				'label'     => esc_html__( 'Input Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-pc-input' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'pc_input_color',
			array(
				'label'     => esc_html__( 'Input Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-pc-input' => 'color: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'pc_input_typography',
				'selector' => '{{WRAPPER}} .sifency-pc-input',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'pc_input_border',
				'selector' => '{{WRAPPER}} .sifency-pc-input',
			)
		);
		$this->add_responsive_control(
			'pc_input_radius',
			array(
				'label'     => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array(
					'{{WRAPPER}} .sifency-pc-input' =>
						'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'pc_input_padding',
			array(
				'label'     => esc_html__( 'Padding', 'sifency-addons' ),
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array(
					'{{WRAPPER}} .sifency-pc-input' =>
						'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'pc_input_shadow',
				'selector' => '{{WRAPPER}} .sifency-pc-input',
			)
		);
		$this->add_control(
			'pc_input_focus_color',
			array(
				'label'     => esc_html__( 'Focus Border Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-pc-input:focus' => 'border-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'pc_range_track_color',
			array(
				'label'     => esc_html__( 'Track Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-pc-range' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'pc_range_height',
			array(
				'label'     => esc_html__( 'Track Height', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 2,
						'max' => 20,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-pc-range' => 'height: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_control(
			'pc_range_thumb_color',
			array(
				'label'     => esc_html__( 'Thumb Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-pc-range::-webkit-slider-thumb' => 'background-color: {{VALUE}};',
					'{{WRAPPER}} .sifency-pc-range::-moz-range-thumb'     => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'pc_range_thumb_size',
			array(
				'label'     => esc_html__( 'Thumb Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 10,
						'max' => 30,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-pc-range::-webkit-slider-thumb' =>
						'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-pc-range::-moz-range-thumb' =>
						'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id              = esc_attr( $context['dom_id'] );
		$settings                = $this->get_settings_for_display();
		$animation_manager       = new Sifency_Addons_Animation_Manager( $settings );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-pc-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-pc-layout-' . $settings['layout_type'] );
		$this->add_render_attribute( 'wrapper', 'data-base-price', $settings['base_price'] );
		$this->add_render_attribute( 'wrapper', 'data-currency', $settings['currency_symbol'] );

		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . $animation_wrapper_attrs ); ?>>
			
			<div class="sifency-pc-inputs">
				<?php
				foreach ( $settings['items'] as $index => $item ) :
					$cost                 = $item['cost_value'];
					$default              = $item['default_val'];
					$animation_item_attrs = $animation_manager->get_item_attributes();

					?>
					<div class="sifency-pricing-calculator-item" <?php echo wp_kses_post( $animation_item_attrs ); ?> >
						<div class="sifency-pc-header">
							<span class="sifency-pc-label sifency-heading-style"><?php echo esc_html( $item['field_label'] ); ?></span>
							<?php if ( $item['field_type'] === 'slider' ) : ?>
								<span class="sifency-pc-value-display">
									<span class="sifency-val sifency-label-style"><?php echo esc_html( $default ); ?></span>
								</span>
							<?php endif; ?>
						</div>

						<div class="sifency-pc-control">
							
							<?php if ( 'slider' === $item['field_type'] ) : ?>
								<input type="range" class="sifency-pc-input sifency-pc-range" 
									min="<?php echo esc_attr( $item['min_val'] ); ?>" 
									max="<?php echo esc_attr( $item['max_val'] ); ?>" 
									step="<?php echo esc_attr( $item['step_val'] ); ?>" 
									value="<?php echo esc_attr( $default ); ?>" 
									data-cost="<?php echo esc_attr( $cost ); ?>"
									data-type="slider">
							
							<?php elseif ( 'switch' === $item['field_type'] ) : ?>
								<label class="sifency-pc-switch">
									<input type="checkbox" class="sifency-pc-input" 
										data-cost="<?php echo esc_attr( $cost ); ?>" 
										data-type="switch" 
										<?php echo $default > 0 ? 'checked' : ''; ?>>
									<span class="slider round"></span>
								</label>

							<?php elseif ( 'number' === $item['field_type'] ) : ?>
								<input type="number" class="sifency-pc-input sifency-pc-number" 
									min="0" 
									value="<?php echo esc_attr( $default ); ?>" 
									data-cost="<?php echo esc_attr( $cost ); ?>"
									data-type="number">
							<?php endif; ?>

						</div>
					</div>
				<?php endforeach; ?>
			</div>

			<div class="sifency-pc-result-box">
				<div class="sifency-pc-total-label sifency-content-style">Total Estimate</div>
				<div class="sifency-pc-total-amount sifency-number-style">
					<span class="sifency-sym"><?php echo esc_html( $settings['currency_symbol'] ); ?></span><span class="sifency-total">0</span>
				</div>
				
				<?php if ( ! empty( $settings['btn_text'] ) ) : ?>
					<a href="<?php echo esc_url( $settings['btn_link']['url'] ?? '#' ); ?>" class="sifency-pc-btn sifency-button-style sifency-addons-primary-button">
						<?php echo esc_html( $settings['btn_text'] ); ?>
					</a>
				<?php endif; ?>
			</div>

		</div>
		<?php
	}
}
