<?php
namespace Sifency\Traits;

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

use Elementor\Controls_Manager;
use Elementor\Repeater;

trait Sifency_Reels_Controls {

	protected function add_sifency_reels_controls() {
		$this->start_controls_section( 'section_reels_content', array( 'label' => 'Reels Content' ) );

		$repeater = new Repeater();

		$repeater->add_control(
			'video_type',
			array(
				'label'   => 'Video Source',
				'type'    => Controls_Manager::SELECT,
				'default' => 'self',
				'options' => array(
					'self'     => 'Self Hosted (MP4)',
					'external' => 'External URL',
				),
			)
		);

		$repeater->add_control(
			'video_url',
			array(
				'label'       => 'Video File / URL',
				'type'        => Controls_Manager::MEDIA,
				'media_types' => array( 'video' ),
				'condition'   => array( 'video_type' => 'self' ),
			)
		);

		$repeater->add_control(
			'external_url',
			array(
				'label'       => 'Direct Video Link',
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'https://example.com/video.mp4',
				'condition'   => array( 'video_type' => 'external' ),
			)
		);

		$repeater->add_control(
			'poster',
			array(
				'label' => 'Thumbnail (Poster)',
				'type'  => Controls_Manager::MEDIA,
			)
		);

		$repeater->add_control( 'hr_user', array( 'type' => Controls_Manager::DIVIDER ) );

		$repeater->add_control(
			'user_name',
			array(
				'label'   => 'Username',
				'type'    => Controls_Manager::TEXT,
				'default' => '@sifency_dev',
			)
		);
		$repeater->add_control(
			'user_avatar',
			array(
				'label' => 'Avatar',
				'type'  => Controls_Manager::MEDIA,
			)
		);
		$repeater->add_control(
			'caption',
			array(
				'label'   => 'Caption',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Enjoy this amazing reel! #sifency #elementor',
			)
		);

		$repeater->add_control(
			'like_count',
			array(
				'label'   => 'Likes (Text)',
				'type'    => Controls_Manager::TEXT,
				'default' => '1.2k',
			)
		);
		$repeater->add_control(
			'comment_count',
			array(
				'label'   => 'Comments (Text)',
				'type'    => Controls_Manager::TEXT,
				'default' => '450',
			)
		);

		$this->add_control(
			'reels_list',
			array(
				'label'       => 'Reel Items',
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => '{{{ user_name }}} - {{{ caption }}}',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section( 'section_reels_settings', array( 'label' => 'Reel System Settings' ) );

		$this->add_control(
			'autoplay_viewport',
			array(
				'label'       => 'Autoplay in Viewport',
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'yes',
				'description' => 'Automatically plays the video when it is the main reel on screen.',
			)
		);

		$this->add_control(
			'loop_video',
			array(
				'label'   => 'Loop Video',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->add_control(
			'mute_by_default',
			array(
				'label'   => 'Mute by Default',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->add_responsive_control(
			'reel_height',
			array(
				'label'      => 'Reel Height',
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'vh', 'px' ),
				'default'    => array(
					'unit' => 'vh',
					'size' => 80,
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-reel-item' => 'height: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->end_controls_section();
	}
}
