<?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

/**
 * Services Box Widget
 *
 * @since 1.0.0
 */
class Services_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-services-box';
	}

	/**
	 * Retrieve the widget title.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title()
	{
		return __('WPRealizer - Services 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-posts-grid 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'),
				],
				'default' => 'layout-1',
			]
		);

		$this->end_controls_section();

		// service content section
		$this->start_controls_section(
			'wpr_service_content_section',
			[
				'label' => __('Service Content', 'unlock-addons-for-elementor'),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);


		$this->add_control(
			'service_image',
			[
				'label' => esc_html__('Service Image', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::MEDIA,
				'default' => [
					'url' => Utils::get_placeholder_image_src(),
				],
				'condition' => [
					'wpr_design_style' => ['layout-1', 'layout-2'],
				]
			]
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			[
				'name' => 'thumbnail_size',
				'default' => 'full',
				'exclude' => [
					'custom'
				],
				'condition' => [
					'wpr_design_style' => ['layout-1', 'layout-2'],
				]
			]
		);

		$this->add_control(
			'wpr_service_box_number',
			[
				'label' => esc_html__('Service Number', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('01', 'unlock-addons-for-elementor'),
				'placeholder' => esc_html__('Enter Number', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'wpr_design_style' => ['layout-1', 'layout-2'],
				]
			]
		);

		$this->add_control(
			'wpr_service_subtitle',
			[
				'label' => esc_html__('Section Subtitle', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('Subtitle', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'wpr_design_style' => ['layout-10']
				]
			]
		);

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

		$this->add_control(
			'wpr_service_desc',
			[
				'label' => esc_html__('Description', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXTAREA,
				'default' => esc_html__('Nemo enim ipsam voluptatem quia voluptas.', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'wpr_design_style' => ['layout-10']
				]
			]
		);

		$this->add_control(
			'wpr_service_tags',
			[
				'label' => __('Tags', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::TEXTAREA,
				'description' => __('Enter tags separated by commas.', 'unlock-addons-for-elementor'),
				'default' => 'BRANDING, MOCKUP',
			]
		);

		$this->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_design_style' => ['layout-1', 'layout-2'],
				]
			]
		);

		// service link 
		$this->tp_render_links_controls('service_link');

		$this->add_control(
			'service_animation_delay',
			[
				'label' => esc_html__('Animation Delay', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('.2', 'unlock-addons-for-elementor'),
				'description' => $this->tp_get_allowed_html_desc('intermediate'),
				'label_block' => true,
				'condition' => [
					'wpr_design_style' => ['layout-10']
				]
			]
		);


		$this->end_controls_section();

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

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

		$this->add_control(
			'wpr_tag_icon_type',
			[
				'label' => esc_html__('Vedio Icon Type', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::CHOOSE,
				'label_block' => false,
				'options' => [
					'icon' => [
						'title' => esc_html__('Icon', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-nerd-wink',
					],
					'image' => [
						'title' => esc_html__('Image', 'unlock-addons-for-elementor'),
						'icon' => 'fa fa-image',
					],
					'svg' => [
						'title' => esc_html__('Svg', 'unlock-addons-for-elementor'),
						'icon' => 'fas fa-code',
					],
				],
				'default' => 'icon',
				'toggle' => false,
				'style_transfer' => true,
			]
		);

		$this->add_control(
			'image',
			[
				'label' => esc_html__('Image', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::MEDIA,
				'default' => [
					'url' => Utils::get_placeholder_image_src(),
				],
				'condition' => [
					'wpr_tag_icon_type' => 'image',
				],
			]
		);

		$this->add_control(
			'icon',
			[
				'label' => esc_html__('Icon', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::ICONS,
				'default' => [
					'value' => 'fas fa-play',
					'library' => 'solid',

				],
				'condition' => [
					'wpr_tag_icon_type' => 'icon',
				]
			]
		);

		$this->add_control(
			'svg',
			[
				'type' => Controls_Manager::TEXTAREA,
				'label_block' => true,
				'label' => __('Svg Code', 'unlock-addons-for-elementor'),
				'default' => __('Svg Code Here', 'unlock-addons-for-elementor'),
				'placeholder' => __('Type Svg Code here', 'unlock-addons-for-elementor'),
				'dynamic' => [
					'active' => true,
				],
				'condition' => [
					'wpr_tag_icon_type' => 'svg',
				],
			]
		);

		$this->add_control(
			'wpr_vedio_link',
			[
				'label' => esc_html__('Vedio Link', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('#', 'unlock-addons-for-elementor'),
				'label_block' => true,
			]
		);
		$this->end_controls_section();

		// additional info
		$this->start_controls_section(
			'wpr_add_info_sec',
			[
				'label' => esc_html__('Additional Info', 'unlock-addons-for-elementor'),
				'condition' => [
					'wpr_design_style' => ['layout-10']
				]
			]
		);

		$this->add_control(
			'wpr_shape_show',
			[
				'label' => esc_html__('Shape Image Show', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Show', 'unlock-addons-for-elementor'),
				'label_off' => esc_html__('Hide', 'unlock-addons-for-elementor'),
				'return_value' => 'yes',
				'default' => 'yes',
				'condition' => [
					'wpr_design_style' => ['layout-10']
				]
			]
		);

		$this->add_control(
			'wpr_slider_arrow',
			[
				'label' => esc_html__('Next/Prev Arrow Show', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Show', 'unlock-addons-for-elementor'),
				'label_off' => esc_html__('Hide', 'unlock-addons-for-elementor'),
				'return_value' => 'yes',
				'default' => 'yes',
			]
		);
		$this->end_controls_section();
	}

	protected function style_tab_content()
	{
		$this->tp_section_style_controls('services_el_section', 'Section - Style', '.wpr-el-section');
		$this->tp_basic_style_controls('service_el_title', 'Service Title', '.wpr-el-title');
		$this->tp_basic_style_controls('service_el_desc', 'Service Description', '.wpr-el-desc', ['layout-10']);
		$this->tp_basic_style_controls('service_el_number', 'Service Number', '.wpr-el-num, .service__item .item__number::before', ['layout-1', 'layout-3']);
		$this->tp_link_controls_style('project_tags_style', 'Service Tags', '.wpr-el-tags', ['layout-1', 'layout-2']);
		$this->tp_link_controls_style('service_btn1_style', 'Button', '.wpr-el-btn');

		$this->start_controls_section(
			'wpr_service_img_section',
			[
				'label' => esc_html__('Service Image', 'unlock-addons-for-elementor'),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_responsive_control(
			'wpr_service_img_w',
			[
				'type' => \Elementor\Controls_Manager::SLIDER,
				'label' => esc_html__('Image Width', 'unlock-addons-for-elementor'),
				'size_units' => ['px', '%'],
				'range' => [
					'px' => [
						'min' => 1,
						'max' => 2000,
					],
				],
				'selectors' => [
					'{{WRAPPER}} .wpr-el-img img' => 'min-width: {{SIZE}}{{UNIT}};',
				],
			]
		);
		$this->add_responsive_control(
			'wpr_service_img_h',
			[
				'type' => \Elementor\Controls_Manager::SLIDER,
				'label' => esc_html__('Image Height', 'unlock-addons-for-elementor'),
				'size_units' => ['px', '%'],
				'range' => [
					'px' => [
						'min' => 1,
						'max' => 2000,
					],
				],
				'selectors' => [
					'{{WRAPPER}} .wpr-el-img img' => 'min-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'):

			if (!empty($settings['service_image']['url'])) {
				$service_image = !empty($settings['service_image']['id']) ? wp_get_attachment_image_url($settings['service_image']['id']) : $settings['service_image']['url'];
				$service_image_alt = get_post_meta($settings["service_image"]["id"], "_wp_attachment_image_alt", true);
			}

			$attrs = $this->tp_get_repeater_links_attr($settings, 'service_link');
			extract($attrs);

			$links_attrs = [
				'href' => $link,
				'target' => $target,
				'rel' => $rel,
			];
		?>

			<!-- work area start  -->
			<div class="work-area wpr-el-section">
				<div class="work-area-inner">
					<div class="works-wrapper-box fade-anim">
						<div class="works-wrapper works-wrapper2">
							<div class="work-box-1 fade-anim">
								<div class="content">
									<?php if (!empty($settings['wpr_service_box_number'])) : ?>
										<span class="number wpr-el-num"><?php echo $this->tp_kses($settings['wpr_service_box_number']); ?><span class="shape">/</span></span>
									<?php endif; ?>

									<?php if (!empty($settings['wpr_service_title'])) : ?>
										<h3 class="title">
											<a class="wpr-el-title" <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
												<?php echo $this->tp_kses($settings['wpr_service_title']); ?>
											</a>
										</h3>
									<?php endif; ?>

									<?php if (!empty($settings['wpr_btn_text'])) : ?>
										<div class="t-btn-group">
											<a class="t-btn t-btn-circle wpr-el-btn" <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
												<i class="fa-solid fa-arrow-right"></i>
											</a>
											<a class="t-btn t-btn-primary wpr-el-btn" <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
												<?php echo $this->tp_kses($settings['wpr_btn_text']); ?>
											</a>
											<a class="t-btn t-btn-circle wpr-el-btn" <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
												<i class="fa-solid fa-arrow-right"></i>
											</a>
										</div>
									<?php endif; ?>
								</div>
								<div class="thumb">
									<?php
									$tags = $this->get_settings_for_display('wpr_service_tags');
									$tags_array = array_map('trim', explode(',', $tags));

									if (!empty($tags_array)) {
										echo '<div class="meta">';
										foreach ($tags_array as $tag) {
											echo '<span class="tag wpr-el-tags">' . esc_html($tag) . '</span>';
										}
										echo '</div>';
									} ?>
									<div class="img_anim_reveal">
										<a <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
											<img src="<?php echo esc_url($service_image); ?>" alt="<?php echo esc_attr($service_image_alt); ?>">
										</a>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<!-- work area end  -->

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

			if (!empty($settings['service_image']['url'])) {
				$service_image = !empty($settings['service_image']['id']) ? wp_get_attachment_image_url($settings['service_image']['id']) : $settings['service_image']['url'];
				$service_image_alt = get_post_meta($settings["service_image"]["id"], "_wp_attachment_image_alt", true);
			}

			$attrs = $this->tp_get_repeater_links_attr($settings, 'service_link');
			extract($attrs);

			$links_attrs = [
				'href' => $link,
				'target' => $target,
				'rel' => $rel,
			];
		?>

			<div class="service__item">
				<div class="service__content service__content--extend">

					<span class="wpr-el-num service-number fade_up_anim" data-delay=".3"><?php echo $this->tp_kses($settings['wpr_service_box_number']); ?></span>

					<div class="service__info">

						<?php if (!empty($settings['wpr_service_title'])): ?>
							<h2 class="h2 fade_up_anim wpr-el-title" data-delay=".35">
								<a <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
									<?php echo $this->tp_kses($settings['wpr_service_title']); ?>
								</a>
							</h2>
						<?php endif; ?>

						<?php if (!empty($settings['wpr_service_desc'])): ?>
							<p class="wpr-el-desc fade_up_anim" data-delay=".4">
								<?php echo $this->tp_kses($settings['wpr_service_desc']); ?>
							</p>
						<?php endif; ?>

						<div class="btn-group d-flex align-items-lg-center gap-3">
							<a <?php echo $this->tp_implode_html_attributes($links_attrs); ?> class="common-btn fade_up_anim wpr-el-btn">
								<span></span><?php echo $this->tp_kses($settings['wpr_btn_text']); ?></span>
								<svg xmlns="http://www.w3.org/2000/svg" width="21" height="12" viewBox="0 0 21 12" fill="none">
									<path d="M20.5303 6.53033C20.8232 6.23744 20.8232 5.76256 20.5303 5.46967L15.7574 0.696699C15.4645 0.403806 14.9896 0.403806 14.6967 0.696699C14.4038 0.989593 14.4038 1.46447 14.6967 1.75736L18.9393 6L14.6967 10.2426C14.4038 10.5355 14.4038 11.0104 14.6967 11.3033C14.9896 11.5962 15.4645 11.5962 15.7574 11.3033L20.5303 6.53033ZM0 6.75H20V5.25H0V6.75Z" fill="currentColor" />
								</svg>
							</a>
						</div>
					</div>
				</div>

				<div class="service__thumb service__thumb--extend fade_up_anim wpr-el-img" data-delay=".2">
					<a <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
						<img src="<?php echo esc_url($service_image); ?>" alt="<?php echo esc_attr($service_image_alt); ?>" />
					</a>
				</div>
			</div>

		<?php else:

			if (!empty($settings['service_image']['url'])) {
				$service_image = !empty($settings['service_image']['id']) ? wp_get_attachment_image_url($settings['service_image']['id']) : $settings['service_image']['url'];
				$service_image_alt = get_post_meta($settings["service_image"]["id"], "_wp_attachment_image_alt", true);
			}

			$attrs = $this->tp_get_repeater_links_attr($settings, 'service_link');
			extract($attrs);

			$links_attrs = [
				'href' => $link,
				'target' => $target,
				'rel' => $rel,
			];
		?>

			<!-- work area start  -->
			<div class="work-area wpr-el-section">
				<div class="work-area-inner">
					<div class="works-wrapper-box fade-anim">
						<div class="works-wrapper">
							<div class="work-box-1 fade-anim">
								<div class="thumb">
									<?php
									$tags = $this->get_settings_for_display('wpr_service_tags');
									$tags_array = array_map('trim', explode(',', $tags));

									if (!empty($tags_array)) {
										echo '<div class="meta">';
										foreach ($tags_array as $tag) {
											echo '<span class="tag wpr-el-tags">' . esc_html($tag) . '</span>';
										}
										echo '</div>';
									} ?>
									<div class="img_anim_reveal">
										<a <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
											<img src="<?php echo esc_url($service_image); ?>" alt="<?php echo esc_attr($service_image_alt); ?>">
										</a>
									</div>
								</div>

								<div class="content">
									<?php if (!empty($settings['wpr_service_box_number'])) : ?>
										<span class="number wpr-el-num"><?php echo $this->tp_kses($settings['wpr_service_box_number']); ?><span class="shape">/</span></span>
									<?php endif; ?>

									<?php if (!empty($settings['wpr_service_title'])) : ?>
										<h3 class="title wpr-el-title">
											<a <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
												<?php echo $this->tp_kses($settings['wpr_service_title']); ?>
											</a>
										</h3>
									<?php endif; ?>

									<?php if (!empty($settings['wpr_btn_text'])) : ?>
										<div class="t-btn-group">
											<a class="t-btn t-btn-circle wpr-el-btn" <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
												<i class="fa-solid fa-arrow-right"></i>
											</a>
											<a class="t-btn t-btn-primary wpr-el-btn" <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
												<?php echo $this->tp_kses($settings['wpr_btn_text']); ?>
											</a>
											<a class="t-btn t-btn-circle wpr-el-btn" <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
												<i class="fa-solid fa-arrow-right"></i>
											</a>
										</div>
									<?php endif; ?>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<!-- work area end  -->

<?php endif;
	}
}


