<?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_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\Group_Control_Image_Size;
use \Elementor\Repeater;

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

/**
 * Price Box Widget
 *
 * @since 1.0.0
 */
class Price_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-price-box';
	}

	/**
	 * Retrieve the widget title.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title()
	{
		return __('WPRealizer - Price 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-price-table 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
	 */

	private static function get_currency_symbol($symbol_name)
	{
		$symbols = [
			'baht' => '&#3647;',
			'bdt' => '&#2547;',
			'dollar' => '&#36;',
			'euro' => '&#128;',
			'franc' => '&#8355;',
			'guilder' => '&fnof;',
			'indian_rupee' => '&#8377;',
			'pound' => '&#163;',
			'peso' => '&#8369;',
			'peseta' => '&#8359',
			'lira' => '&#8356;',
			'ruble' => '&#8381;',
			'shekel' => '&#8362;',
			'rupee' => '&#8360;',
			'real' => 'R$',
			'krona' => 'kr',
			'won' => '&#8361;',
			'yen' => '&#165;',
		];

		return isset($symbols[$symbol_name]) ? $symbols[$symbol_name] : '';
	}

	private static function get_currency_symbol_text($symbol_text)
	{
		$symbols = [
			'baht' => 'THB',
			'bdt' => 'BDT',
			'dollar' => 'USD',
			'euro' => 'EUR',
			'franc' => 'EUR',
			'guilder' => 'GLD',
			'indian_rupee' => 'INR',
			'pound' => 'GBP',
			'peso' => 'MXN',
			'lira' => 'TRY',
			'ruble' => 'RUB',
			'shekel' => 'ILS',
			'real' => 'BRL',
			'krona' => 'KR',
			'won' => 'KRW',
			'yen' => 'JPY',
		];

		return isset($symbols[$symbol_text]) ? $symbols[$symbol_text] : '';
	}


	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'),
				],
				'default' => 'layout-1',
			]
		);

		$this->end_controls_section();



		$this->start_controls_section(
			'_section_pricing',
			[
				'label' => __('Pricing', 'unlock-addons-for-elementor'),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'currency',
			[
				'label' => __('Currency', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'label_block' => false,
				'options' => [
					'' => __('None', 'unlock-addons-for-elementor'),
					'baht' => '&#3647; ' . _x('Baht', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'bdt' => '&#2547; ' . _x('BD Taka', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'dollar' => '&#36; ' . _x('Dollar', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'euro' => '&#128; ' . _x('Euro', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'franc' => '&#8355; ' . _x('Franc', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'guilder' => '&fnof; ' . _x('Guilder', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'krona' => 'kr ' . _x('Krona', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'lira' => '&#8356; ' . _x('Lira', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'peso' => '&#8369; ' . _x('Peso', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'pound' => '&#163; ' . _x('Pound Sterling', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'real' => 'R$ ' . _x('Real', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'ruble' => '&#8381; ' . _x('Ruble', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'indian_rupee' => '&#8377; ' . _x('Rupee (Indian)', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'shekel' => '&#8362; ' . _x('Shekel', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'won' => '&#8361; ' . _x('Won', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'yen' => '&#165; ' . _x('Yen/Yuan', 'Currency Symbol', 'unlock-addons-for-elementor'),
					'custom' => __('Custom', 'unlock-addons-for-elementor'),
				],
				'default' => 'dollar',
			]
		);

		$this->add_control(
			'currency_custom',
			[
				'label' => __('Custom Symbol', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'condition' => [
					'currency' => 'custom',
				],
				'dynamic' => [
					'active' => true,
				]
			]
		);

		$this->add_control(
			'price_duration',
			[
				'label' => __('Duration', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => '/mo',
				'dynamic' => [
					'active' => true
				]
			]
		);

		$this->add_control(
			'price_main',
			[
				'label' => __('Price', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => '59.99',
				'dynamic' => [
					'active' => true
				]
			]
		);


		$this->end_controls_section();

		$this->start_controls_section(
			'wpr_pricing_header',
			[
				'label' => esc_html__('Header Controls', 'unlock-addons-for-elementor'),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'wpr_pricing_active_switcher',
			[
				'label' => esc_html__('Is Active Price?', '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' => 'no',
			]
		);

		$this->add_control(
			'wpr_pricing_header_subtitle',
			[
				'label' => esc_html__('Subtitle', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('This is subtitle', '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_pricing_header_title',
			[
				'label' => esc_html__('Title', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('Gold Membership', 'unlock-addons-for-elementor'),
				'placeholder' => esc_html__('Your Gold Membership Text', 'unlock-addons-for-elementor'),
				'label_block' => true,
			]
		);

		$this->end_controls_section();


		$this->start_controls_section(
			'_section_features',
			[
				'label' => __('Features', 'unlock-addons-for-elementor'),
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'tp_feature_switcher',
			[
				'label' => esc_html__('Active Feature', 'unlock-addons-for-elementor'),
				'type' => 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' => '0',
			]
		);
		$repeater->add_control(
			'tp_features_icon_type',
			[
				'label' => esc_html__('Select Icon Type', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'default' => 'icon',
				'options' => [
					'image' => esc_html__('Image', 'unlock-addons-for-elementor'),
					'icon' => esc_html__('Icon', 'unlock-addons-for-elementor'),
					'svg' => esc_html__('SVG', 'unlock-addons-for-elementor'),
				],
				'condition' => [
					'wpr_design_style' => 'layout-1',
				],
			]
		);

		$repeater->add_control(
			'tp_features_image',
			[
				'label' => esc_html__('Upload Icon Image', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::MEDIA,
				'default' => [
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				],
				'condition' => [
					'tp_features_icon_type' => 'image',
				]

			]
		);

		$repeater->add_control(
			'tp_features_icon_svg',
			[
				'show_label' => false,
				'type' => Controls_Manager::TEXTAREA,
				'label_block' => true,
				'placeholder' => esc_html__('SVG Code Here', 'unlock-addons-for-elementor'),
				'condition' => [
					'tp_features_icon_type' => 'svg'
				]
			]
		);

		$repeater->add_control(
			'tp_features_icon',
			[
				'label' => esc_html__('Icon', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::ICONS,
				'default' => [
					'value' => 'fas fa-smile',
					'library' => 'fa-solid',
				],
				'recommended' => [
					'fa-solid' => [
						'circle',
						'dot-circle',
						'square-full',
					],
					'fa-regular' => [
						'circle',
						'dot-circle',
						'square-full',
					],
				],
				'condition' => [
					'tp_features_icon_type' => 'icon',
					'wpr_design_style' => 'layout-10',
				],
			]
		);

		$repeater->add_control(
			'feature_text',
			[
				'label' => __('Feature Text', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXTAREA,
				'default' => __('Exciting Feature', 'unlock-addons-for-elementor'),
				'dynamic' => [
					'active' => true
				]
			]
		);

		$this->add_control(
			'features_list',
			[
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'show_label' => false,
				'default' => [
					[
						'feature_text' => __('Standard Feature', 'unlock-addons-for-elementor'),
						'tp_check_icon' => 'fa fa-check',
					],
					[
						'feature_text' => __('Another Great Feature', 'unlock-addons-for-elementor'),
						'tp_check_icon' => 'fa fa-check',
					],
					[
						'feature_text' => __('Obsolete Feature', 'unlock-addons-for-elementor'),
						'tp_check_icon' => 'fa fa-close',
					],
				],
				'title_field' => '{{{ feature_text }}}',
			]
		);

		$this->end_controls_section();

		// price_btn
		$this->tp_link_render_controls('price_btn', 'Button');

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

	protected function style_tab_content()
	{
		$this->tp_section_style_controls('section', 'Section', '.wpr-el-section');
		$this->tp_basic_style_controls('pricing_heading_title', 'Heading Title', '.wpr-el-title');
		$this->tp_basic_style_controls('pricing_heading_desc', 'Heading Description', '.wpr-el-desc');

		$this->tp_basic_style_controls('pricing_heading_symbol', 'Heading Symbol', '.wpr-el-symbol', 'layout-10');
		$this->tp_basic_style_controls('pricing_heading_price', 'Heading Price', '.wpr-el-price');
		$this->tp_basic_style_controls('pricing_heading_month', 'Heading Month', '.wpr-el-month');

		// feature item

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

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'wpr_pricing_f_tab_typo',
				'label' => esc_html__('Active Typography', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .wpr-el-f-title',
			]
		);
		$this->start_controls_tabs(
			'wpr_pricing_f_tabs',
		);

		$this->start_controls_tab(
			'wpr_pricing_f_tab_active',
			[
				'label' => esc_html__('Active', 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'wpr_pricing_f_tab_active_color',
			[
				'label' => esc_html__('Text Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpr-el-f-title' => 'color: {{VALUE}}',
				],
			]
		);

		$this->add_control(
			'hr',
			[
				'type' => \Elementor\Controls_Manager::DIVIDER,
			]
		);

		$this->add_control(
			'wpr_pricing_check_mark_color',
			[
				'label' => esc_html__('Check Mark Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .pricing-2__item .package__body .package-list ul li::before' => 'color: {{VALUE}}',
				],
			]
		);


		$this->end_controls_tab();

		// inactive 
		$this->start_controls_tab(
			'wpr_pricing_f_tab_inactive',
			[
				'label' => esc_html__('Inactive', 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'wpr_pricing_f_tab_inactive_color',
			[
				'label' => esc_html__('Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpr-el-f-title.inactive' => 'color: {{VALUE}}',
				],
			]
		);
		$this->end_controls_tab();


		$this->end_controls_tabs();

		$this->end_controls_section();


		$this->tp_link_controls_style('pricing_btn', 'Button', '.wpr-el-price-btn');
	}

	/**
	 * 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'):
			$control_id = 'price_btn';

			$animation = $this->tp_animation_show($settings);

			if ($settings['currency'] === 'custom') {
				$currency = $settings['currency_custom'];
			} else {
				$currency = self::get_currency_symbol($settings['currency']);
			}

			$is_active_price = $settings['wpr_pricing_active_switcher'] == 'yes' ? true : false;
			$active_class = $is_active_price ? 'active' : '';

			$btn_class = $is_active_price ? 'wpr-btn-4' : 'wpr-btn-4';

			$this->tp_link_attributes_render('price_btn', '' . $btn_class . '  wpr-el-price-btn common-btn buy-btn', $this->get_settings());
		?>

			<div class="pricing__package item-popup wpr-el-section <?php echo esc_attr($active_class); ?>">
				<div class="package-head">
					<?php if (!empty($settings['wpr_pricing_header_title'])): ?>
					<h5 class="title wpr-el-title"><?php echo $this->tp_kses($settings['wpr_pricing_header_title']); ?></h5>
					<?php endif; ?>

					<?php if (!empty($settings['price_main'])): ?>
					<h2 class="price wpr-el-price"><?php echo esc_html($currency); ?><?php echo $this->tp_kses($settings['price_main']); ?>
					</h2>
					<?php endif; ?>

					<?php if (!empty($settings['price_duration'])): ?>
					<p class="wpr-el-month type"><?php echo $this->tp_kses($settings['price_duration']); ?></p>
					<?php endif; ?>
				</div>

				<?php if (!empty($settings['tp_' . $control_id . '_text']) && $settings['tp_' . $control_id . '_button_show'] == 'yes'): ?>
				<div class="package-btn">
					<a <?php echo $this->get_render_attribute_string('tp-button-arg' . $control_id . ''); ?>>
						<?php echo $settings['tp_' . $control_id . '_text']; ?>
						<i class="fa-solid fa-arrow-right"></i>
					</a>
				</div>
				<?php endif; ?>

				<div class="package-featchers">
					<ul>
						<?php foreach ($settings['features_list'] as $key => $item):
							$is_active_feature = $item['tp_feature_switcher'] == 'yes' ? true : false;
							$active_feature_class = $is_active_feature ? 'inactive' : '';
						?>
							<li class="wpr-el-f-title <?php echo esc_attr($active_feature_class); ?>"><?php echo $this->tp_kses($item['feature_text']); ?></li>
						<?php endforeach; ?>
					</ul>
				</div>
			</div>


		<?php else:

			$control_id = 'price_btn';

			$animation = $this->tp_animation_show($settings);

			if ($settings['currency'] === 'custom') {
				$currency = $settings['currency_custom'];
			} else {
				$currency = self::get_currency_symbol($settings['currency']);
			}

			$is_active_price = $settings['wpr_pricing_active_switcher'] == 'yes' ? true : false;
			$active_class = $is_active_price ? 'active' : '';

			$btn_class = $is_active_price ? '' : '';

			$this->tp_link_attributes_render('price_btn', '' . $btn_class . '  wpr-el-price-btn common-btn package-purches', $this->get_settings());
		?>

			<!-- pricing section start  -->
			<div class="pricing-2__item item-popup wpr-el-section <?php echo esc_attr($active_class); ?>">
				<div class="package__head">
					<?php if (!empty($settings['wpr_pricing_header_title'])): ?>
						<h5 class="package-title wpr-el-title"><?php echo $this->tp_kses($settings['wpr_pricing_header_title']); ?></h5>
					<?php endif; ?>
					<?php if (!empty($settings['wpr_pricing_header_subtitle'])): ?>
						<p class="package-info wpr-el-desc"><?php echo $this->tp_kses($settings['wpr_pricing_header_subtitle']); ?></p>
					<?php endif; ?>
					<h2 class="package-price wpr-el-price"><?php echo esc_html($currency); ?><?php echo $this->tp_kses($settings['price_main']); ?>/ <span class="wpr-el-month"><?php echo $this->tp_kses($settings['price_duration']); ?></span></h2>

					<?php if (!empty($settings['tp_' . $control_id . '_text']) && $settings['tp_' . $control_id . '_button_show'] == 'yes'): ?>
						<a <?php echo $this->get_render_attribute_string('tp-button-arg' . $control_id . ''); ?>>
							<?php echo $settings['tp_' . $control_id . '_text']; ?>
						</a>
					<?php endif; ?>
				</div>
				<div class="package__body">
					<div class="package-list">
						<ul>
							<?php foreach ($settings['features_list'] as $key => $item):
								$is_active_feature = $item['tp_feature_switcher'] == 'yes' ? true : false;
								$active_feature_class = $is_active_feature ? 'inactive' : '';
							?>
								<li class="wpr-el-f-title <?php echo esc_attr($active_feature_class); ?>"><?php echo $this->tp_kses($item['feature_text']); ?></li>
							<?php endforeach; ?>
						</ul>
					</div>
				</div>
			</div>
			<!-- pricing section end  -->

<?php endif;
	}
}


