<?php
namespace Sifency\Traits;

if ( ! defined( 'ABSPATH' ) ) {
	exit;  }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
use Elementor\Repeater;
use Elementor\Icons_Manager;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Sticky_Stack;
use Sifency\Traits\Sifency_Addons_Floating_Count_Controls;

trait Sifency_Services_Controls {
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Sticky_Stack;
	use Sifency_Addons_Floating_Count_Controls;

	protected function add_sifency_services_controls( $args = array() ) {
		// --- Content Section ---
		$this->start_controls_section( 'section_content', array( 'label' => 'Services' ) );

		$this->add_control(
			'source',
			array(
				'label'   => 'Source',
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'custom' => 'Custom',
					'posts'  => 'Posts (Requires "service" CPT)',
				),
				'default' => 'custom',
			)
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'icon_type',
			array(
				'label'   => 'Icon Type',
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'icon'  => array(
						'title' => 'Icon',
						'icon'  => 'eicon-star',
					),
					'image' => array(
						'title' => 'Image',
						'icon'  => 'eicon-image',
					),
				),
				'default' => 'icon',
			)
		);

		$repeater->add_control(
			'icon',
			array(
				'label'     => 'Icon',
				'type'      => Controls_Manager::ICONS,
				'condition' => array( 'icon_type' => 'icon' ),
			)
		);

		$repeater->add_control(
			'image',
			array(
				'label'     => 'Image',
				'type'      => Controls_Manager::MEDIA,
				'condition' => array( 'icon_type' => 'image' ),
			)
		);

		$repeater->add_control(
			'title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Service Title',
			)
		);
		$repeater->add_control(
			'excerpt',
			array(
				'label'   => 'Description',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Lorem Ipsum available, but the majority have suffered alteration.',
			)
		);
		$repeater->add_control(
			'additional_content',
			array(
				'label' => 'Additional Content',
				'type'  => Controls_Manager::WYSIWYG,
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label' => 'Link',
				'type'  => Controls_Manager::URL,
			)
		);

		$this->add_control(
			'service_list',
			array(
				'label'       => 'Service Items',
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array( array( 'title' => 'Web Design' ), array( 'title' => 'Web Development' ), array( 'title' => 'Marketing' ) ),
				'title_field' => '{{{ title }}}',
				'condition'   => array( 'source' => 'custom' ),
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'      => 'thumbnail',
				'default'   => 'medium',
				'condition' => array( 'source' => 'custom' ),
			)
		);
		$this->add_control(
			'show_image',
			array(
				'label'     => 'Show Image',
				'type'      => Controls_Manager::SWITCHER,
				'condition' => array( 'source' => 'posts' ),
				'default'   => 'yes',
			)
		);
		$this->add_control(
			'show_count',
			array(
				'label'   => 'Show Count Number',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_sifency_addons_floating_count_controls();
		$this->add_control(
			'show_excerpt',
			array(
				'label'   => 'Show Excerpt',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_additional_content',
			array(
				'label'   => 'Show Additional Content',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_control(
			'show_button',
			array(
				'label'   => 'Show Button',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->end_controls_section();

		$this->add_sifency_arg_controls(
			array(
				'default_post' => 'service',
				'condition'    => array( 'source' => 'posts' ),
			)
		);

		$this->start_controls_section( 'section_layout', array( 'label' => 'Layout' ) );

		$this->add_control(
			'layout_style',
			array(
				'label'   => 'Layout',
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'grid'     => 'Grid',
					'carousel' => 'Carousel',
					'list'     => 'List',
				),
				'default' => 'grid',
			)
		);

		$this->add_responsive_control(
			'columns',
			array(
				'label'          => 'Columns',
				'type'           => Controls_Manager::SELECT,
				'default'        => '3',
				'tablet_default' => '2',
				'mobile_default' => '1',
				'options'        => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
				),
				'selectors'      => array( '{{WRAPPER}} .sifency-services-grid' => 'grid-template-columns: repeat({{VALUE}}, 1fr);' ),
				'condition'      => array( 'layout_style' => 'grid' ),
			)
		);

		$this->add_responsive_control(
			'gap',
			array(
				'label'     => 'Gap',
				'type'      => Controls_Manager::SLIDER,
				'default'   => array( 'size' => 30 ),
				'selectors' => array( '{{WRAPPER}} .sifency-services-grid' => 'gap: {{SIZE}}px;' ),
			)
		);

		$this->add_control(
			'read_more_text',
			array(
				'label'   => 'Button Text',
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Read More', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'button_icon',
			array(
				'label'     => esc_html__( 'Button Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'icon-right-open-big',
					'library' => 'sifency-icons',
				),
				'condition' => array( 'read_more_text!' => '' ),
			)
		);
		$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',
					),
				),
				'default'   => 'center',
				'toggle'    => true,
				'selectors' => array(
					'{{WRAPPER}} .sifency-service-item' => 'text-align: {{VALUE}};',
					'{{WRAPPER}} .sifency-service-item' => 'align-items: {{VALUE}};',
				),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'wrap_flex_',
				'label'    => esc_html__( 'Wrapper Flexbox', 'sifency-addons' ),
				'selector' => '.sifency-service-item',
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'flex_',
				'label'    => esc_html__( 'Content Flexbox', 'sifency-addons' ),
				'selector' => '.sifency-service-content-wrap',
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'    => 'title_para_flex_',
				'label'     => esc_html__( 'Title & Paragraph Flexbox', 'sifency-addons' ),
				'selector'  => '.title-and-para',
				'condition' => array( 'show_excerpt' => 'yes' ),
			)
		);
				// Existing controls ...
		// Add control for sticky last item delay
		$this->add_control(
			'sticky_last_item_delay',
			array(
				'label'     => esc_html__( 'Last Item Delay (seconds)', 'sifency-addons' ),
				'type'      => Controls_Manager::NUMBER,
				'min'       => 0,
				'step'      => 0.1,
				'default'   => 0.3,
				'condition' => array(
					'enable_sticky_stack' => 'yes',
				),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_count_style',
			array(
				'label'     => esc_html__( 'Count Number Style', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'show_count' => 'yes',
				),
			)
		);
		$this->add_responsive_control(
			'count_size_wh',
			array(
				'label'      => esc_html__( 'Fixed Width/Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 200,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-service-count' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}}; display: flex; align-items: center; justify-content: center; position: absolute;',
				),
			)
		);

		$this->add_control(
			'heading_count_position',
			array(
				'label'     => esc_html__( 'Positioning', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'count_h_position',
			array(
				'label'   => esc_html__( 'Horizontal Orientation', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'default' => 'right',
				'options' => array(
					'left'  => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-left',
					),
					'right' => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'toggle'  => false,
			)
		);

		$this->add_responsive_control(
			'count_h_offset_left',
			array(
				'label'      => esc_html__( 'Horizontal Offset', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -100,
						'max' => 500,
					),
					'%'  => array(
						'min' => -50,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 0,
				),
				'condition'  => array(
					'count_h_position' => 'left',
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-service-count' => 'left: {{SIZE}}{{UNIT}}; right: auto;',
				),
			)
		);

		$this->add_responsive_control(
			'count_h_offset_right',
			array(
				'label'      => esc_html__( 'Horizontal Offset', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -100,
						'max' => 500,
					),
					'%'  => array(
						'min' => -50,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 0,
				),
				'condition'  => array(
					'count_h_position' => 'right',
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-service-count' => 'right: {{SIZE}}{{UNIT}}; left: auto;',
				),
			)
		);

		$this->add_control(
			'count_v_position',
			array(
				'label'   => esc_html__( 'Vertical Orientation', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'default' => 'top',
				'options' => array(
					'top'    => array(
						'title' => esc_html__( 'Top', 'sifency-addons' ),
						'icon'  => 'eicon-v-align-top',
					),
					'bottom' => array(
						'title' => esc_html__( 'Bottom', 'sifency-addons' ),
						'icon'  => 'eicon-v-align-bottom',
					),
				),
				'toggle'  => false,
			)
		);

		$this->add_responsive_control(
			'count_v_offset_top',
			array(
				'label'      => esc_html__( 'Vertical Offset', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -100,
						'max' => 500,
					),
					'%'  => array(
						'min' => -50,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 0,
				),
				'condition'  => array(
					'count_v_position' => 'top',
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-service-count' => 'top: {{SIZE}}{{UNIT}}; bottom: auto;',
				),
			)
		);

		$this->add_responsive_control(
			'count_v_offset_bottom',
			array(
				'label'      => esc_html__( 'Vertical Offset', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -100,
						'max' => 500,
					),
					'%'  => array(
						'min' => -50,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 0,
				),
				'condition'  => array(
					'count_v_position' => 'bottom',
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-service-count' => 'bottom: {{SIZE}}{{UNIT}}; top: auto;',
				),
			)
		);

		$this->end_controls_section();

		$this->add_sifency_addons_floating_count_style_controls();
		$this->add_sifency_addons_sticky_stack();
	}
}
