<?php

namespace Unlockafe_addons\Widgets;

use Unlockafe_addons\Traits\Utils as UnlockafeUtils;
use Unlockafe_addons\Traits\Widget_helper;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use \Elementor\Utils;
use \Elementor\Control_Media;
use \Elementor\Group_Control_Image_Size;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Group_Control_Text_Shadow;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Background;
use \Elementor\Repeater;

if (!defined('ABSPATH'))
	exit; // Exit if accessed directly

/**
 * Repeater Box Widget
 *
 * @since 1.0.0
 */
class Repeater_Box extends Widget_Base
{

	use Widget_helper, UnlockafeUtils;

	/**
	 * Retrieve the widget name.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return string Widget name.
	 */
	public function get_name()
	{
		return 'unlockafe-repeater-box';
	}

	/**
	 * Retrieve the widget title.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title()
	{
		return __('WPRealizer - Repeater Box', 'unlock-addons-for-elementor');
	}

	/**
	 * Retrieve the widget icon.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon()
	{
		return 'eicon-toggle ulicon';
	}

	/**
	 * Retrieve the list of categories the widget belongs to.
	 *
	 * Used to determine where to display the widget in the editor.
	 *
	 * Note that currently Elementor supports only one category.
	 * When multiple categories passed, Elementor uses the first one.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return array Widget categories.
	 */
	public function get_categories()
	{
		return ['unlockafe-addons'];
	}

	/**
	 * Retrieve the list of scripts the widget depended on.
	 *
	 * Used to set scripts dependencies required to run the widget.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return array Widget scripts dependencies.
	 */
	public function get_script_depends()
	{
		return [];
	}

	public function get_style_depends()
	{
		return ['unlockafe-global'];
	}

	/**
	 * Register the widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.0.0
	 *
	 * @access protected
	 */
	protected function register_controls()
	{
		$this->register_controls_section();
		$this->style_tab_content();
	}

	protected function register_controls_section()
	{
		// layout Panel
		$this->start_controls_section(
			'wpr_layout',
			[
				'label' => esc_html__('Design Layout', 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'wpr_design_style',
			[
				'label' => esc_html__('Select Layout', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'layout-1' => esc_html__('Layout 1', 'unlock-addons-for-elementor'),
					'layout-2' => esc_html__('Layout 2', 'unlock-addons-for-elementor'),
					// 'layout-3' => esc_html__('Layout 3', 'unlock-addons-for-elementor'),
					// 'layout-4' => esc_html__('Layout 4', 'unlock-addons-for-elementor'),
				],
				'default' => 'layout-1',
			]
		);

		$this->end_controls_section();

		// Title/Content
		$this->start_controls_section(
			'wpr_section_heading_settings',
			[
				'label' => __('Section Heading', 'unlock-addons-for-elementor'),
				'tab' => Controls_Manager::TAB_CONTENT,
				'condition' => [
					'wpr_design_style' => ['layout-10']
				]
			]
		);

		$this->add_control(
			'wpr_repeater_subtitle',
			[
				'label' => esc_html__('Sub Title', 'unlock-addons-for-elementor'),
				'description' => $this->tp_get_allowed_html_desc('basic'),
				'type' => Controls_Manager::TEXT,
				'default' => 'Sub Title',
				'placeholder' => esc_html__('Type Before Heading Text', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'wpr_design_style' => ['layout-10']
				]
			]
		);

		$this->add_control(
			'wpr_repeater_title',
			[
				'label' => esc_html__('Title', 'unlock-addons-for-elementor'),
				'description' => $this->tp_get_allowed_html_desc('intermediate'),
				'type' => Controls_Manager::TEXTAREA,
				'default' => 'Your Section Title',
				'placeholder' => esc_html__('Type Heading Text', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'wpr_design_style' => ['layout-1', 'layout-2', 'layout-3']
				]
			]
		);

		$this->add_control(
			'wpr_repeater_title_tag',
			[
				'label' => esc_html__('Title HTML Tag', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'h1' => [
						'title' => esc_html__('H1', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-editor-h1'
					],
					'h2' => [
						'title' => esc_html__('H2', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-editor-h2'
					],
					'h3' => [
						'title' => esc_html__('H3', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-editor-h3'
					],
					'h4' => [
						'title' => esc_html__('H4', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-editor-h4'
					],
					'h5' => [
						'title' => esc_html__('H5', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-editor-h5'
					],
					'h6' => [
						'title' => esc_html__('H6', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-editor-h6'
					]
				],
				'default' => 'h2',
				'toggle' => false,
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'wpr_icon_box_section',
			[
				'label' => esc_html__('Box Contents', 'unlock-addons-for-elementor'),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'repeater_condition',
			[
				'label' => __('Field condition', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'style_1' => __('Style 1', 'unlock-addons-for-elementor'),
					'style_2' => __('Style 2', 'unlock-addons-for-elementor'),
					// 'style_3' => __('Style 3', 'unlock-addons-for-elementor'),
					// 'style_4' => __('Style 4', 'unlock-addons-for-elementor'),
				],
				'default' => 'style_1',
				'frontend_available' => true,
				'style_transfer' => true,
			]
		);

		$this->tp_render_icon_controls($repeater, 'icon_box');

		$repeater->add_control(
			'activate_repeat_box',
			[
				'label'        => __('Active', 'unlock-addons-for-elementor'),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __('Yes', 'unlock-addons-for-elementor'),
				'label_off'    => __('No', 'unlock-addons-for-elementor'),
				'return_value' => 'yes',
				'default'      => 'no',
				'condition' => [
					'repeater_condition' => ['style_10'],
				]
			]
		);

		$repeater->add_control(
			'wpr_rep_animation_delay',
			[
				'label' => esc_html__('Repeater Animation Delay', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('.25', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'repeater_condition' => ['style_2']
				]
			]
		);

		$repeater->add_control(
			'icon',
			[
				'label' => esc_html__('Icon', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::ICONS,
				'default' => [
					'value' => 'far fa-star',
					'library' => 'fa-solid',
				],
				'condition' => [
					'repeater_condition' => ['style_10']
				]
			]
		);

		$repeater->add_control(
			'wpr_rep_number',
			[
				'label' => esc_html__('Repeater Number', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('01', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'repeater_condition' => ['style_1']
				]
			]
		);

		$repeater->add_control(
			'wpr_rep_title',
			[
				'label' => esc_html__('Repeater Title', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('Repeater Title', 'unlock-addons-for-elementor'),
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'wpr_title_url',
			[
				'label' => esc_html__('URL', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXTAREA,
				'default' => 'Url Here',
				'label_block' => true,
				'description' => __("type url here", 'unlock-addons-for-elementor'),
				'condition' => [
					'repeater_condition' => ['style_10']
				]
			]
		);

		$repeater->add_control(
			'wpr_rep_cat',
			[
				'label' => esc_html__('Repeater Category', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('Repeater Category', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'repeater_condition' => ['style_10']
				]
			]
		);

		$repeater->add_control(
			'wpr_rep_date',
			[
				'label' => esc_html__('Repeater Date', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXTAREA,
				'default' => esc_html__('Repeater Date', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'repeater_condition' => ['style_10']
				]
			]
		);

		$repeater->add_control(
			'wpr_rep_tag',
			[
				'label' => esc_html__('Repeater Tag', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXTAREA,
				'default' => esc_html__('Repeater Tag', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'repeater_condition' => ['style_0', 'style_40']
				]
			]
		);

		$repeater->add_control(
			'process_steps_text',
			[
				'label' => __('Process Steps (Comma Separated)', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::TEXTAREA,
				'default' => __('Idea gathering & sketch, Wireframes, UI Design', 'unlock-addons-for-elementor'),
				'placeholder' => __('Enter steps separated by commas', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'repeater_condition' => ['style_10', 'style_40']
				]
			]
		);

		$repeater->add_control(
			'wpr_rep_desc',
			[
				'label' => esc_html__('Repeater Decription', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXTAREA,
				'default' => esc_html__('Repeater Decription', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'repeater_condition' => ['style_2']
				]
			]
		);

		$repeater->add_control(
			'wpr_btn_link_switcher',
			[
				'label' => esc_html__('Button SWITCHER', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Yes', 'unlock-addons-for-elementor'),
				'label_off' => esc_html__('No', 'unlock-addons-for-elementor'),
				'return_value' => 'yes',
				'default' => 'no',
				'separator' => 'before',
				'condition' => [
					'repeater_condition' => ['style_1']
				]
			]
		);

		$repeater->add_control(
			'wpr_btn_text',
			[
				'label' => esc_html__('Button Text', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('Button Text', 'unlock-addons-for-elementor'),
				'title' => esc_html__('Enter button text', 'unlock-addons-for-elementor'),
				'label_block' => false,
				'condition' => [
					'wpr_btn_link_switcher' => 'yes',
					'repeater_condition' => ['style_10']
				],
			]
		);
		$repeater->add_control(
			'wpr_btn_link_type',
			[
				'label' => esc_html__('Button Link Type', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::SELECT,
				'options' => [
					'1' => 'Custom Link',
					'2' => 'Internal Page',
				],
				'default' => '1',
				'condition' => [
					'wpr_btn_link_switcher' => 'yes',
					'repeater_condition' => ['style_1']
				]
			]
		);
		$repeater->add_control(
			'wpr_btn_link',
			[
				'label' => esc_html__('Button Link', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::URL,
				'dynamic' => [
					'active' => true,
				],
				'placeholder' => esc_html__('https://your-link.com', 'unlock-addons-for-elementor'),
				'show_external' => true,
				'default' => [
					'url' => '#',
					'is_external' => false,
					'nofollow' => false,
				],
				'condition' => [
					'wpr_btn_link_type' => '1',
					'wpr_btn_link_switcher' => 'yes',
					'repeater_condition' => ['style_1']
				]
			]
		);
		$repeater->add_control(
			'wpr_btn_page_link',
			[
				'label' => esc_html__('Select Button Link Page', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::SELECT2,
				'label_block' => true,
				'options' => $this->tp_get_all_pages(),
				'condition' => [
					'wpr_btn_link_type' => '2',
					'wpr_btn_link_switcher' => 'yes',
					'repeater_condition' => ['style_1']
				]
			]
		);


		$this->add_control(
			'wpr_slider_list',
			[
				'label' => esc_html__('Text List', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[
						'wpr_rep_title' => esc_html__('Needs Assessment', 'unlock-addons-for-elementor'),
					],
					[
						'wpr_rep_title' => esc_html__('Strategy Development', 'unlock-addons-for-elementor'),
					],
					[
						'wpr_rep_title' => esc_html__('Consultation Solution', 'unlock-addons-for-elementor'),
					],
				],
				'title_field' => '{{{ wpr_rep_title }}}',
			]
		);

		$this->add_responsive_control(
			'wpr_align',
			[
				'label' => esc_html__('Alignment', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'left' => [
						'title' => esc_html__('Left', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-text-align-left',
					],
					'center' => [
						'title' => esc_html__('Center', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-text-align-center',
					],
					'right' => [
						'title' => esc_html__('Right', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-text-align-right',
					],
				],
				'toggle' => false,
				'selectors' => [
					'{{WRAPPER}} .tp-align' => 'text-align: {{VALUE}};'
				]
			]
		);

		$this->end_controls_section();

		// animation
		$this->tp_creative_animation('layout-10');
	}


	// style_tab_content
	protected function style_tab_content()
	{
		$this->tp_section_style_controls('wpr-repeater_section', 'Section - Style', '.wpr-el-section');
		$this->tp_box_controls_style('wpr_repeater_box_style', 'Repeater Box - Style', '.wpr-el-box', ['layout-1', 'layout-3']);
		$this->tp_basic_style_controls('wpr_main_title', 'Section Title - Style', '.wpr-el-title', ['layout-10', 'layout-20']);
		$this->tp_basic_style_controls('wpr_repeater_title', 'Repeater Title - Style', '.wpr-el-rep-title');
		$this->tp_basic_style_controls('wpr_repeater_description', 'Repeater Description - Style', '.wpr-el-rep-desc',  ['layout-2']);
		$this->tp_basic_style_controls('wpr_repeater_cat', 'Repeater Category - Style', '.wpr-el-rep-cat', ['layout-10']);
		$this->tp_basic_style_controls('wpr_repeater_number', 'Repeater Number - Style', '.wpr-el-rep-number', ['layout-1', 'layout-30']);
		$this->tp_basic_style_controls('wpr_repeater_tag', 'Repeater Tag - Style', '.wpr-el-rep-tag', ['layout-2']);
		$this->tp_basic_style_controls('wpr_repeater_date', 'Repeater Date - Style', '.wpr-el-rep-date', ['layout-10']);
		$this->tp_box_controls_style('wpr_repeater_icon_box_style', 'Icon Box - Style', '.wpr-el-icon-box', ['layout-4']);

		$this->start_controls_section(
			'wpr_icon_box_style_section',
			[
				'label' => esc_html__('Icon Box', 'unlock-addons-for-elementor'),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
				'condition' => [
					'wpr_design_style' => ['layout-3', 'layout-4'],
				]
			]
		);

		$this->add_responsive_control(
			'wpr_icon_box_svg_width',
			[
				'label' => esc_html__('Width', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => ['px', '%'],
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 1000,
						'step' => 5,
					],
					'%' => [
						'min' => 0,
						'max' => 100,
					],
				],
				'default' => [
					'unit' => 'px',
					'size' => 20,
				],
				'selectors' => [
					'{{WRAPPER}} .wpr-el-icon svg' => 'width: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'wpr_icon_box_svg_height',
			[
				'label' => esc_html__('Height', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => ['px', '%'],
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 1000,
						'step' => 5,
					],
					'%' => [
						'min' => 0,
						'max' => 100,
					],
				],
				'default' => [
					'unit' => 'px',
					'size' => 20,
				],
				'selectors' => [
					'{{WRAPPER}} .wpr-el-icon svg' => 'height: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since 1.0.0
	 *
	 * @access protected
	 */
	protected function render()
	{
		$settings = $this->get_settings_for_display();
?>

		<?php if ($settings['wpr_design_style'] == 'layout-2'): ?>

			<div class="row gx-0 gy-4">
				<?php foreach ($settings['wpr_slider_list'] as $key => $item):

				?>
				<div class="col-md-6 col-xl-3 fade-anim" data-delay="<?php echo $item['wpr_rep_animation_delay']; ?>">
				  <div class="process-5__item">
					<div class="item-icon">
						<?php $this->tp_render_signle_icon_html($item, 'icon_box'); ?>
					</div>

					<div class="item-content">
						<?php if (!empty($item['wpr_rep_title'])): ?>
						<h4 class="item-title wpr-el-rep-title">
							<?php echo $item['wpr_rep_title']; ?>
						</h4>
						<?php endif; ?>

						<?php if (!empty($item['wpr_rep_desc'])): ?>
						<p class="wpr-el-rep-desc">
							<?php echo $item['wpr_rep_desc']; ?>
						</p>
						<?php endif; ?>
					</div>
				  </div>
				</div>
				<?php endforeach; ?>
			</div>


		<?php elseif ($settings['wpr_design_style'] == 'layout-3'): ?>

		<?php elseif ($settings['wpr_design_style'] == 'layout-4'): ?>

		<?php else: ?>

			<div class="what-do__right-content wpr-el-section">
				<ul>
					<?php foreach ($settings['wpr_slider_list'] as $key => $item):

						// btn Link
						if ('2' == $item['wpr_btn_link_type']) {
							$link = get_permalink($item['wpr_btn_page_link']);
							$target = '_self';
							$rel = 'nofollow';
						} else {
							$link = !empty($item['wpr_btn_link']['url']) ? $item['wpr_btn_link']['url'] : '';
							$target = !empty($item['wpr_btn_link']['is_external']) ? '_blank' : '';
							$rel = !empty($item['wpr_btn_link']['nofollow']) ? 'nofollow' : '';
						}

					?>
						<li class="fade-anim wpr-el-box" data-delay="0.45">
							<span class="expertise-number wpr-el-rep-number"><?php echo $item['wpr_rep_number']; ?></span>
							<h6 class="title wpr-el-rep-title">
								<a href="<?php echo esc_url($link); ?>"><?php echo $item['wpr_rep_title']; ?></a>
							</h6>
							<?php if (!empty($item['wpr_btn_link_switcher'])): ?>
								<a href="<?php echo esc_url($link); ?>" class="icon">
									<i class="fa-light fa-arrow-up-right"></i>
								</a>
							<?php endif; ?>
						</li>
					<?php endforeach; ?>
				</ul>
			</div>

		<?php endif; ?>

<?php
	}
}


