<?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\Group_Control_Typography;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Repeater;

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

/**
 * Link Text Widget
 *
 * @since 1.0.0
 */
class Link_Text 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-link-text';
	}

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

	/**
	 * Retrieve the widget icon.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon()
	{
		return 'eicon-editor-link 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();

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

		$this->add_control(
			'wpr_text_first_name',
			[
				'label' => esc_html__('First Name', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('NUVO', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'wpr_design_style' => ['layout-1']
				]
			]
		);

		$this->add_control(
			'wpr_text_last_name',
			[
				'label' => esc_html__('Last Name', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('HOVERTON', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'wpr_design_style' => ['layout-1']
				]
			]
		);

		$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-10'],
				]
			]
		);

		// text link 
		$this->tp_render_links_controls('text_link');

		$this->end_controls_section();

		$this->start_controls_section(
			'wpr_text_section_marquee',
			[
				'label' => esc_html__('Marquee Contents', 'unlock-addons-for-elementor'),
				'tab' => Controls_Manager::TAB_CONTENT,
				'condition' => ['wpr_design_style' => ['layout-2']],
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'wpr_marquee_image',
			[
				'label' => esc_html__('Image', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::MEDIA,
				'default' => [
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				],
			]
		);

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

		$repeater->add_control(
			'wpr_rep_link_marquee',
			[
				'label' => esc_html__('Link', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::URL,
				'placeholder' => esc_html__('https://your-link.com', 'unlock-addons-for-elementor'),
				'default' => [
					'url' => '',
					'is_external' => false,
					'nofollow' => false,
				],
				'label_block' => true,
			]
		);

		$this->add_control(
			'wpr_slider_list_marquee',
			[
				'label' => esc_html__('Text List', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[
						'wpr_rep_title_marquee' => esc_html__('Creativity', 'unlock-addons-for-elementor'),
					],
					[
						'wpr_rep_title_marquee' => esc_html__('Skilled Experts', 'unlock-addons-for-elementor'),
					],
					[
						'wpr_rep_title_marquee' => esc_html__('Magical World', 'unlock-addons-for-elementor'),
					],
				],
				'title_field' => '{{{ wpr_rep_title_marquee }}}',
			]
		);

		$this->end_controls_section();

		// title/content
		$this->start_controls_section(
			'section_content',
			[
				'label' => __('Title', 'unlock-addons-for-elementor'),
				'condition' => [
					'wpr_design_style' => ['layout-30']
				]
			]
		);

		$this->add_control(
			'wpr_link_text_title',
			[
				'label' => esc_html__('Title Front', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::TEXTAREA,
				'default' => esc_html__('let\'s start with us', 'unlock-addons-for-elementor'),
				'placeholder' => esc_html__('Your Text', 'unlock-addons-for-elementor'),
				'label_block' => true,
			]
		);

		$this->add_control(
			'wpr_link_text_title_back',
			[
				'label' => esc_html__('Title Back', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::TEXTAREA,
				'default' => esc_html__('let\'s start with us', 'unlock-addons-for-elementor'),
				'placeholder' => esc_html__('Your Text', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'wpr_design_style' => ['layout-1']
				]
			]
		);

		$this->add_control(
			'wpr_link_text_icon_show',
			[
				'label' => esc_html__('Add Icon ?', 'unlock-addons-for-elementor'),
				'type' => \Elementor\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' => 'no',
				'condition' => [
					'wpr_design_style' => ['layout-10']
				]
			]
		);

		$this->tp_single_icon_control('linK_btn', 'wpr_link_text_icon_show', 'yes');

		$this->add_control(
			'wpr_link_text_icon_position',
			[
				'label' => esc_html__('Choose Icon Position', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::CHOOSE,
				'options' => [
					'left' => [
						'title' => esc_html__('Left', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-h-align-left',
					],
					'right' => [
						'title' => esc_html__('Right', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-h-align-right',
					],
				],
				'default' => 'right',
				'toggle' => true,
				'condition' => [
					'wpr_link_text_icon_show' => 'yes'
				],
			]
		);

		$this->tp_render_links_controls('link_text');

		$this->end_controls_section();

		// animation
		// $this->tp_creative_animation();
	}

	protected function style_tab_content()
	{

		$this->tp_box_controls_style('wpr_rep_box_style', 'Box - Style', '.wpr-el-box', ['layout-10']);
		$this->tp_basic_style_controls('wpr_main_title', 'Title', '.wpr-el-title',  ['layout-1']);
		$this->tp_basic_style_controls('wpr_repeater_title', 'Repeater Title', '.wpr-el-rep-title',  ['layout-2']);
		$this->tp_box_controls_style('wpr_rep_icon_style', 'Icon - Style', '.text-slider-item-3:hover .icon', ['layout-2']);
	}

	/**
	 * 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'):
			$attrs = [];

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

			$links_attrs = [
				'href' => $link,
				'rel' => $rel,
				'class' => 'wpr-el-link-text marquee-text d-block',
				'data-direction' => 'left',
				'data-speed' => '100',
			];

		?>

			<!-- text slider area start  -->
			<div class="text-slider-area-3 wpr-el-box">
				<div class="text-slider fade-anim">
					<div class="swiper text-slider-active">
						<div class="swiper-wrapper">
							<?php foreach ($settings['wpr_slider_list_marquee'] as $key => $item):
								$link = !empty($item['wpr_rep_link_marquee']['url']) ? $item['wpr_rep_link_marquee']['url'] : '#';
								$target = !empty($item['wpr_rep_link_marquee']['is_external']) ? ' target="_blank"' : '';
								$nofollow = !empty($item['wpr_rep_link_marquee']['nofollow']) ? ' rel="nofollow"' : '';

							?>
								<div class="swiper-slide">
									<div class="text-slider-item-3">
										<a href="<?php echo esc_url($link); ?>">
											<h2 class="title wpr-el-rep-title">
												<?php if (!empty($item['wpr_marquee_image'])): ?>
													<span class="icon">
														<?php echo wp_get_attachment_image($item['wpr_marquee_image']['id'], 'full', '', ['class' => '']) ?>
													</span>
												<?php endif; ?>
												<span class="text"><?php echo $this->tp_kses($item['wpr_rep_title_marquee']); ?></span>
											</h2>
										</a>
									</div>
								</div>
							<?php endforeach; ?>
						</div>
					</div>
				</div>
			</div>
			<!-- text slider area end -->

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

			$attrs = [];

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

			$links_attrs = [
				'href' => $link,
				'rel' => $rel,
				'class' => 'wpr-el-link-text marquee-text d-block',
				'data-direction' => 'left',
				'data-speed' => '100',
			];
		?>



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

			$attrs = [];

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

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

		?>

			<div class="overflow-hidden">
				<h2 class="section__big-title marquee-text wpr-el-link-text">
					<a <?php echo $this->tp_implode_html_attributes($links_attrs); ?>>
						<?php echo $settings['wpr_link_text_title']; ?>
						<span class="text-stroke"><?php echo $settings['wpr_link_text_title']; ?></span>
					</a>
				</h2>
			</div>

		<?php else:
			$attrs = [];

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

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

			<h1 class="header-3__left-content">
				<?php if (!empty($settings['wpr_text_first_name'])) : ?>
				<a <?php echo $this->tp_implode_html_attributes($links_attrs); ?> class="wpr-el-title">
					<?php echo $settings['wpr_text_first_name']; ?>
				</a>
				<?php endif; ?>
				<span class="line"></span>
				<?php if (!empty($settings['wpr_text_last_name'])) : ?>
				<a <?php echo $this->tp_implode_html_attributes($links_attrs); ?> class="wpr-el-title">
					<?php echo $settings['wpr_text_last_name']; ?>
				</a>
				<?php endif; ?>
			</h1>


<?php endif;
	}
}


