<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Repeater;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_FunFact extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-funfact',
			'title'          => 'Sifency Counter Fun Fact',
			'icon'           => 'eicon-counter',
			'category'       => 'sifency-addons-widgets',
			'keywords'       => array( 'counter', 'fact', 'stat', 'number' ),
			'script_depends' => array( 'sifency-counter-script' ),
			'style_depends'  => array( 'sifency-typo-style', 'sifency-icon', 'sifency-component-style' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section( 'section_content', array( 'label' => 'Counters' ) );
		$repeater = new Repeater();
		$repeater->add_control(
			'title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Happy Clients',
			)
		);
		$repeater->add_control(
			'subtitle',
			array(
				'label' => 'Subtitle',
				'type'  => Controls_Manager::TEXT,
			)
		);
		$repeater->add_control(
			'icon',
			array(
				'label' => 'Icon',
				'type'  => Controls_Manager::ICONS,
			)
		);
		$repeater->add_control(
			'ending_number',
			array(
				'label'   => 'Ending Number',
				'type'    => Controls_Manager::NUMBER,
				'default' => 1200,
			)
		);
		$repeater->add_control(
			'prefix',
			array(
				'label' => 'Number Prefix',
				'type'  => Controls_Manager::TEXT,
			)
		);
		$repeater->add_control(
			'suffix',
			array(
				'label'   => 'Number Suffix',
				'type'    => Controls_Manager::TEXT,
				'default' => '+',
			)
		);
		$this->add_control(
			'counters',
			array(
				'label'       => 'Counter Items',
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'title'         => 'Happy Clients',
						'subtitle'      => 'Our customers love us',
						'ending_number' => 1200,
						'suffix'        => '+',
					),
					array(
						'title'         => 'Projects Done',
						'subtitle'      => 'Successful delivery',
						'ending_number' => 850,
						'suffix'        => 'K',
					),
				),
				'title_field' => '{{{ title }}}',
			)
		);
		$this->add_responsive_control(
			'columns',
			array(
				'label'     => 'Columns',
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
					'5' => '5',
					'6' => '6',
				),
				'default'   => '2',
				'selectors' => array( '{{WRAPPER}} .sifency-fun-facts' => 'grid-template-columns: repeat({{VALUE}}, 1fr);' ),
			)
		);
		$this->add_responsive_control(
			'gap',
			array(
				'label'     => 'Gap',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-fun-facts' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_control(
			'duration',
			array(
				'label'   => 'Animation Duration (ms)',
				'type'    => Controls_Manager::NUMBER,
				'default' => 2000,
				'min'     => 100,
			)
		);
		$this->add_control(
			'text_align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'toggle'    => true,
				'selectors' => array(
					'label'                              => esc_html__( 'Content Flex', 'sifency-addons' ),
					'{{WRAPPER}} .sifency-fun-fact-item' => 'text-align: {{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'number_width',
			array(
				'label'      => esc_html__( 'Width', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'em', 'rem', 'vw' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-fun-fact-number' => 'width: {{SIZE}}{{UNIT}}; margin-left:auto; margin-right:auto;' ),
			)
		);
		$this->add_responsive_control(
			'number_min_width',
			array(
				'label'      => esc_html__( 'Min Width', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'em', 'rem', 'vw' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-fun-fact-number' => 'min-width: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'selector' => '.sifency-fun-fact-content',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'exclude'  => array( 'transition', 'tabs' ),
				'label'    => 'Item Wrapper',
				'selector' => '.sifency-fun-fact-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'title_style',
				'label'          => 'Title Style',
				'selector'       => '.sifency-heading-style',
				'show_max_width' => true,
				'exclude'        => array( 'transition', 'shadow', 'tabs', 'bg' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'subtitle_style',
				'label'          => 'Subtitle Style',
				'selector'       => '.sifency-fun-fact-subtitle',
				'show_max_width' => true,
				'exclude'        => array( 'transition', 'shadow', 'tabs', 'bg' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'number_style',
				'label'    => 'Number Style',
				'selector' => '.sifency-number-style',
				'exclude'  => array( 'transition', 'shadow', 'tabs', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'content_style',
				'label'    => 'Prefix / Suffix Style',
				'selector' => '.sifency-content-style',
				'exclude'  => array( 'transition', 'shadow', 'tabs', 'bg' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls();
		$this->add_Sifency_Addons_Animation_Controls();
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( empty( $settings['counters'] ) ) {
			return; }
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-fun-facts' );
		$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['counters'] as $item ) : ?>
				<?php
				$item_classes = array( 'sifency-fun-fact-item' );
				$item_attrs   = $animation_manager->get_item_attributes( $item_classes );
				?>
				<div <?php echo wp_kses_post( $item_attrs ); ?>>
					<?php if ( ! empty( $item['icon']['value'] ) ) : ?>
						<div class="sifency-fun-fact-icon sifency-icon-style">
							<?php \Elementor\Icons_Manager::render_icon( $item['icon'] ); ?>
						</div>
					<?php endif; ?>
					<div class="sifency-fun-fact-content">
						<div class="sifency-fun-fact-number">
							<?php if ( ! empty( $item['prefix'] ) ) : ?>
								<span class="sifency-fun-fact-prefix sifency-content-style"><?php echo esc_html( $item['prefix'] ); ?></span>
							<?php endif; ?>
							<span class="sifency-counter sifency-number-style" data-to-value="<?php echo esc_attr( $item['ending_number'] ); ?>" data-duration="<?php echo esc_attr( $settings['duration'] ); ?>">0</span>
							<?php if ( ! empty( $item['suffix'] ) ) : ?>
								<span class="sifency-fun-fact-suffix sifency-content-style"><?php echo esc_html( $item['suffix'] ); ?></span>
							<?php endif; ?>
						</div>
						<?php if ( ! empty( $item['title'] ) ) : ?>
							<h3 class="sifency-fun-fact-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></h3>
						<?php endif; ?>
						<?php if ( ! empty( $item['subtitle'] ) ) : ?>
							<div class="sifency-fun-fact-subtitle"><?php echo esc_html( $item['subtitle'] ); ?></div>
						<?php endif; ?>
					</div>
				</div>
			<?php endforeach; ?>
		</div>
		<?php
	}
}
