<?php
namespace Sifency\Traits;

use Elementor\Controls_Manager;

if ( ! defined( 'ABSPATH' ) ) {
	exit; }

trait Sifency_Addons_Floating_Count_Controls {

	protected function add_sifency_addons_floating_count_controls( $args = array() ) {

		$defaults = array(
			'prefix'    => 'floating_count_',
			'condition' => array(),
		);

		$args   = wp_parse_args( $args, $defaults );
		$prefix = $args['prefix'];

		$this->add_control(
			'show_floating_count',
			array(
				'label'   => 'Show Floating Count Number',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);

		$this->add_control(
			'floating_count_prefix',
			array(
				'label'       => 'Prefix Text',
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => 'e.g. 0 or Project',
				'condition'   => array(
					'show_floating_count' => 'yes',
				),
			)
		);

		$this->add_control(
			'floating_count_position',
			array(
				'label'     => 'Floating Count Position',
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'top_left'      => 'Top Left',
					'top_center'    => 'Top Center',
					'top_right'     => 'Top Right',
					'left_center'   => 'Left Center',
					'right_center'  => 'Right Center',
					'bottom_left'   => 'Bottom Left',
					'bottom_center' => 'Bottom Center',
					'bottom_right'  => 'Bottom Right',
				),
				'default'   => 'top_left',
				'condition' => array(
					'show_floating_count' => 'yes',
				),
			)
		);
	}

	protected function render_sifency_floating_count( $settings, $extra_class = '' ) {
		if ( 'yes' !== ( $settings['show_floating_count'] ?? 'no' ) ) {
			return;
		}
		$prefix   = $settings['floating_count_prefix'] ?? '';
		$position = $settings['floating_count_position'] ?? 'top_left';

		$span_classes = 'sifency-floating-count-num';
		if ( ! empty( $extra_class ) ) {
			$span_classes .= ' ' . esc_attr( $extra_class );
		}
		?>
		<div class="sifency-floating-count-container">
			<div class="sifency-floating-count-inner <?php echo esc_attr( $position ); ?>">
				<span class="<?php echo esc_attr( $span_classes ); ?>" data-prefix="<?php echo esc_attr( $prefix ); ?>"><?php echo esc_html( $prefix . '1' ); ?></span>
			</div>
		</div>
		<?php
	}

	protected function add_sifency_addons_floating_count_style_controls( $args = array() ) {

		$defaults = array(
			'prefix'    => 'floating_count_style',
			'condition' => array(
				'show_floating_count' => 'yes',
			),
		);

		$args = wp_parse_args( $args, $defaults );

		$this->start_controls_section(
			'section_floating_count_style',
			array(
				'label'     => esc_html__( 'Floating Count Style', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => $args['condition'],
			)
		);

		$this->add_responsive_control(
			'floating_count_size_wh',
			array(
				'label'      => esc_html__( 'Width/Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 300,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-floating-count-inner' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'floating_count_offset_x',
			array(
				'label'      => esc_html__( 'Horizontal Offset (X)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -100,
						'max' => 500,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-floating-count-inner.top_left' => 'left: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-floating-count-inner.bottom_left' => 'left: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-floating-count-inner.left_center' => 'left: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-floating-count-inner.top_right' => 'right: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-floating-count-inner.bottom_right' => 'right: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-floating-count-inner.right_center' => 'right: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'floating_count_offset_y',
			array(
				'label'      => esc_html__( 'Vertical Offset (Y)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'vh' ),
				'range'      => array(
					'px' => array(
						'min' => -100,
						'max' => 500,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-floating-count-inner.top_left' => 'top: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-floating-count-inner.top_center' => 'top: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-floating-count-inner.top_right' => 'top: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-floating-count-inner.bottom_left' => 'top: calc(100vh - {{SIZE}}{{UNIT}});',
					'{{WRAPPER}} .sifency-floating-count-inner.bottom_center' => 'top: calc(100vh - {{SIZE}}{{UNIT}});',
					'{{WRAPPER}} .sifency-floating-count-inner.bottom_right' => 'top: calc(100vh - {{SIZE}}{{UNIT}});',
				),
			)
		);

		$this->add_control(
			'floating_count_style_divider',
			array(
				'type' => Controls_Manager::DIVIDER,
			)
		);

		if ( method_exists( $this, 'add_sifency_addons_master_typography_controls' ) ) {
			$this->add_sifency_addons_master_typography_controls(
				array(
					'prefix'          => 'floating_count_typo_',
					'label'           => 'Typography & Color',
					'selector'        => '{{WRAPPER}} .sifency-floating-count-num',
					'exclude'         => array( 'tabs', 'transition', 'wrapper', 'alignment' ),
					'show_blend_mode' => true,
				)
			);
		}

		$this->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'floating_count_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-floating-count-inner',
			)
		);

		$this->end_controls_section();
	}
}
