<?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_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Addons_Widget_Charts extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-advanced-charts',
			'title'          => 'Sifency Charts',
			'icon'           => 'eicon-chart-bar',
			'category'       => 'sifency-business-widgets',
			'keywords'       => array( 'charts', 'data', 'visual', 'graph' ),
			'style_depends'  => array( 'sifency-chart-style' ),
			'script_depends' => array( 'chart', 'sifency-chart-script' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_config',
			array( 'label' => esc_html__( 'Chart Type & Data', 'sifency-addons' ) )
		);
		$this->add_control(
			'chart_type',
			array(
				'label'   => esc_html__( 'Chart Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'bar',
				'options' => array(
					'bar'       => 'Bar Chart',
					'line'      => 'Line Chart',
					'area'      => 'Area Chart (Filled Line)',
					'radar'     => 'Radar Chart',
					'pie'       => 'Pie Chart',
					'doughnut'  => 'Doughnut Chart',
					'polarArea' => 'Polar Area',
				),
			)
		);
		$this->add_control(
			'style_preset',
			array(
				'label'   => esc_html__( 'Visual Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'modern',
				'options' => array(
					'modern'   => '1. Modern Flat',
					'gradient' => '2. Gradient Fill',
					'minimal'  => '3. Minimal (Thin)',
					'neon'     => '4. Neon / Dark Mode',
					'glass'    => '5. Glassmorphism',
				),
			)
		);
		$this->add_control( 'hr_data', array( 'type' => Controls_Manager::DIVIDER ) );
		$this->add_control(
			'data_source',
			array(
				'label'   => esc_html__( 'Data Source', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'manual',
				'options' => array(
					'manual' => 'Manual Entry',
					'csv'    => 'CSV URL / Upload',
					'json'   => 'JSON URL',
					'gsheet' => 'Google Sheet (Published CSV)',
				),
			)
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'label',
			array(
				'label'   => 'Label',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Item',
			)
		);
		$repeater->add_control(
			'value',
			array(
				'label'   => 'Value',
				'type'    => Controls_Manager::NUMBER,
				'default' => 50,
			)
		);
		$repeater->add_control(
			'color',
			array(
				'label' => 'Color (Optional)',
				'type'  => Controls_Manager::COLOR,
			)
		);
		$this->add_control(
			'chart_data',
			array(
				'label'     => esc_html__( 'Data Points', 'sifency-addons' ),
				'type'      => Controls_Manager::REPEATER,
				'fields'    => $repeater->get_controls(),
				'default'   => array(
					array(
						'label' => 'Jan',
						'value' => 30,
					),
					array(
						'label' => 'Feb',
						'value' => 50,
					),
					array(
						'label' => 'Mar',
						'value' => 80,
					),
				),
				'condition' => array( 'data_source' => 'manual' ),
			)
		);
		$this->add_control(
			'external_url',
			array(
				'label'       => esc_html__( 'File URL (CSV/JSON)', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'https://example.com/data.csv',
				'description' => 'For Google Sheets: File > Share > Publish to web > CSV.',
				'condition'   => array( 'data_source' => array( 'csv', 'json', 'gsheet' ) ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section( 'section_options', array( 'label' => 'Chart Options' ) );
		$this->add_control(
			'show_legend',
			array(
				'label'   => 'Show Legend',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_grid',
			array(
				'label'     => 'Show Grid Lines',
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'chart_type' => array( 'bar', 'line', 'area' ) ),
			)
		);
		$this->add_control(
			'curved_line',
			array(
				'label'     => 'Curved Lines (Tension)',
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'chart_type' => array( 'line', 'area', 'radar' ) ),
			)
		);
		$this->add_control(
			'animation_duration',
			array(
				'label'   => 'Animation Speed (ms)',
				'type'    => Controls_Manager::NUMBER,
				'default' => 1000,
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_chart',
			array(
				'label' => 'Chart Style',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'base_color',
			array(
				'label'   => 'Primary Color',
				'type'    => Controls_Manager::COLOR,
				'default' => '#007BFF',
			)
		);
		$this->add_control(
			'secondary_color',
			array(
				'label'   => 'Secondary Color',
				'type'    => Controls_Manager::COLOR,
				'default' => '#00c6fb',
			)
		);
		$this->add_responsive_control(
			'chart_height',
			array(
				'label'     => 'Height',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 200,
						'max' => 800,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-chart-container' => 'height: {{SIZE}}px;' ),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'chart_bg',
				'selector' => '{{WRAPPER}} .sifency-chart-wrapper',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'chart_border',
				'selector' => '{{WRAPPER}} .sifency-chart-wrapper',
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'chart_shadow',
				'selector' => '{{WRAPPER}} .sifency-chart-wrapper',
			)
		);
		$this->add_responsive_control(
			'chart_padding',
			array(
				'label'     => 'Padding',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( '{{WRAPPER}} .sifency-chart-wrapper' => 'padding: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ),
			)
		);
		$this->end_controls_section();
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$settings          = $this->get_settings_for_display();
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-chart-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-chart-style-' . $settings['style_preset'] );
		$js_config = array(
			'type'     => ( $settings['chart_type'] === 'area' ) ? 'line' : $settings['chart_type'],
			'style'    => $settings['style_preset'],
			'fill'     => ( $settings['chart_type'] === 'area' ),
			'tension'  => ( $settings['curved_line'] === 'yes' ) ? 0.4 : 0,
			'legend'   => ( $settings['show_legend'] === 'yes' ),
			'grid'     => ( $settings['show_grid'] === 'yes' ),
			'duration' => $settings['animation_duration'],
			'colors'   => array( $settings['base_color'], $settings['secondary_color'] ),
			'source'   => $settings['data_source'],
			'url'      => $settings['external_url'],
		);

		if ( 'manual' === $settings['data_source'] ) {
			$labels    = array();
			$values    = array();
			$bg_colors = array();
			foreach ( $settings['chart_data'] as $item ) {
				$labels[]    = $item['label'];
				$values[]    = $item['value'];
				$bg_colors[] = ! empty( $item['color'] ) ? $item['color'] : $settings['base_color'];
			}
			$js_config['manual_data'] = array(
				'labels'   => $labels,
				'datasets' => array(
					array(
						'label'           => 'Dataset 1',
						'data'            => $values,
						'backgroundColor' => $bg_colors,
					),
				),
			);
		}
		$this->add_render_attribute( 'canvas', 'class', 'sifency-chart-canvas' );
		$this->add_render_attribute( 'canvas', 'data-chart-settings', wp_json_encode( $js_config ) ); ?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			<div class="sifency-chart-container">
				<canvas <?php echo wp_kses_post( $this->get_render_attribute_string( 'canvas' ) ); ?>></canvas>
			</div>
		</div>
		<?php
	}
}
