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

use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Repeater;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Box_Shadow;
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_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Progressbar extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	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-progressbar',
			'title'          => 'Sifency Progress Bar',
			'icon'           => 'eicon-skill-bar',
			'category'       => 'sifency-addons-widgets',
			'keywords'       => array( 'progress', 'bar', 'skill', 'percentage' ),
			'style_depends'  => array( 'sifency-typo-style', 'sifency-frontend-style', 'sifency-component-style', 'sifency-icon' ),
			'script_depends' => array( 'sifency-progressbar-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section( 'section_content', array( 'label' => 'Progress Layout' ) );
		$this->add_control(
			'skin',
			array(
				'label'        => esc_html__( 'Layout Skin', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'standard',
				'options'      => array(
					'standard' => 'Standard Flat',
					'striped'  => 'Animated Striped',
					'thin'     => 'Thin Line',
					'internal' => 'Internal Text (Inside Bar)',
					'bubble'   => 'Bubble Tooltip',
				),
				'prefix_class' => 'sifency-progress-skin-',
			)
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Web Design',
			)
		);
		$repeater->add_control(
			'percent',
			array(
				'label'   => 'Percentage',
				'type'    => Controls_Manager::SLIDER,
				'default' => array( 'size' => 75 ),
			)
		);

		$repeater->add_control(
			'item_color',
			array(
				'label'     => 'Custom Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} {{CURRENT_ITEM}} .sifency-progress-bar__fill' => 'background-color: {{VALUE}};',
					'{{WRAPPER}} {{CURRENT_ITEM}} .sifency-progress-percent.bubble:after' => 'border-top-color: {{VALUE}};',
					'{{WRAPPER}} {{CURRENT_ITEM}} .sifency-progress-percent.bubble' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'progress_bars',
			array(
				'label'       => 'Items',
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'title'   => 'Web Design',
						'percent' => array( 'size' => 85 ),
					),
					array(
						'title'   => 'Development',
						'percent' => array( 'size' => 92 ),
					),
				),
				'title_field' => '{{{ title }}}',
			)
		);

		$this->add_control(
			'display_percentage',
			array(
				'label'   => 'Display Percentage',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();

		$this->start_controls_section(
			'section_style_progressbar',
			array(
				'label' => __( 'Bar Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'bar_fill_bg',
				'label'    => 'Fill Color',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .sifency-progress-bar__fill',
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'bar_bg_color',
				'label'    => 'Track (Background) Color',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .sifency-progress-bar',
			)
		);
		$this->add_responsive_control(
			'bar_height',
			array(
				'label'      => __( 'Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 50,
					),
				),
				'default'    => array( 'size' => 10 ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-progress-bar' => 'height: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}}.sifency-progress-skin-internal .sifency-progress-bar' => 'height: {{SIZE}}px; line-height: {{SIZE}}px;', // For internal text vertical align
				),
			)
		);
		$this->add_control(
			'bar_border_radius',
			array(
				'label'      => __( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-progress-bar, {{WRAPPER}} .sifency-progress-bar__fill' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'bar_shadow',
				'selector' => '{{WRAPPER}} .sifency-progress-bar',
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'Heading Style',
				'selector' => '.sifency-heading-style',
				'exclude'  => array( 'transition', 'shadow', 'tabs', 'bg', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'number_style',
				'label'    => 'Percentage Style',
				'selector' => '.sifency-number-style',
				'exclude'  => array( 'transition', 'tabs', 'alignment' ),
			)
		);

		$this->add_Sifency_Addons_Animation_Controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( empty( $settings['progress_bars'] ) ) {
			return;
		}

		$skin              = $settings['skin'];
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-progress-wrapper', 'skin-' . $skin );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php foreach ( $settings['progress_bars'] as $index => $item ) : ?>
				<?php
				$item_classes = array( 'sifency-progress-item', 'elementor-repeater-item-' . $item['_id'] );
				$item_attrs   = $animation_manager->get_item_attributes( $item_classes );
				$percent_val  = (int) $item['percent']['size'];
				?>
				<div <?php echo wp_kses_post( $item_attrs ); ?>>
					
					<?php if ( 'internal' !== $skin && 'bubble' !== $skin ) : ?>
						<div class="sifency-progress-header">
							<span class="sifency-progress-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></span>
							
							<?php if ( 'yes' === $settings['display_percentage'] ) : ?>
								<span class="sifency-progress-percent sifency-number-style"><?php echo esc_html( $percent_val ); ?>%</span>
							<?php endif; ?>
						</div>
					<?php elseif ( 'bubble' === $skin ) : ?>
						 <div class="sifency-progress-header">
							<span class="sifency-progress-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></span>
						</div>
					<?php elseif ( 'internal' === $skin ) : ?>
						 <div class="sifency-progress-header">
							<span class="sifency-progress-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></span>
						</div>
					<?php endif; ?>


					<div class="sifency-progress-bar">
						<div class="sifency-progress-bar__fill" data-percent="<?php echo esc_html( $percent_val ); ?>">
							<?php if ( 'internal' === $skin && 'yes' === $settings['display_percentage'] ) : ?>
								<span class="sifency-progress-percent internal sifency-number-style"><?php echo esc_html( $percent_val ); ?>%</span>
							<?php endif; ?>
							<?php if ( 'bubble' === $skin && 'yes' === $settings['display_percentage'] ) : ?>
								<span class="sifency-progress-percent bubble sifency-number-style" style="left: <?php echo esc_html( $percent_val ); ?>%;">
									<?php echo esc_html( $percent_val ); ?>%
								</span>
							<?php endif; ?>
						</div>
					</div>
				</div>
			<?php endforeach; ?>
		</div>
		<?php
	}
}
