<?php

namespace Unlockafe_addons\Widgets;

use Unlockafe_addons\Traits\Utils;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Repeater;
use Elementor\Widget_Base;
use Unlockafe_addons\Traits\Widget_helper;


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


/**
 * Pricing table style one
 *
 * @since 1.0.0
 */

class Progress_Bar extends Widget_Base
{
	use Widget_helper;
	use Utils;

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

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

	/**
	 * Retrieve the widget icon.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon()
	{
		return 'eicon-loading 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_style_depends()
    {
        return ['unlockafe-progress-bar-style'];
    }

	/**
	 * 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 ['unlockafe-progress-bar-script'];
	}


	/**
	 * 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()
	{

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

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

		$this->end_controls_section();

		/**
		 * 
		 * Start control for content tab
		 */
		$this->start_controls_section(
			'content_section',
			[
				'label' => esc_html__('Contents', 'unlock-addons-for-elementor'),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);

		// Create a repeater instance
		$repeater = new Repeater();

		// Progress Control
		$repeater->add_control(
			'progress',
			[
				'label' => esc_html__('Progress', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::NUMBER,
				'max' => 100,
				'default' => 75,
				'dynamic' => [
					'active' => true,
				],
			]
		);

		// Title Control
		$repeater->add_control(
			'title',
			[
				'label' => esc_html__('Title', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('Web Design', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'dynamic' => [
					'active' => true,
				],
			]
		);

		// Heading Tags Control
		$repeater->add_control(
			'heading_tags',
			[
				'label' => esc_html__('Title Tag', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::CHOOSE,
				'label_block' => true,
				'description' => esc_html__('You can change Heading Tags from here.', 'unlock-addons-for-elementor'),
				'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',
			]
		);

		// Description Control
		$repeater->add_control(
			'description',
			[
				'label' => esc_html__('Description', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXTAREA,
				'rows' => 10,
				'default' => esc_html__('Default description', 'unlock-addons-for-elementor'),
				'placeholder' => esc_html__('Type your description here', 'unlock-addons-for-elementor'),
				'dynamic' => [
					'active' => true,
				],
				'condition' => [
					'layout' => 'style-1',
				]
			]
		);

		// Link Control
		$repeater->add_control(
			'link',
			[
				'label' => esc_html__('Link', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::URL,
				'default' => [
					'url' => '#',
				],
				'label_block' => true,
				'dynamic' => [
					'active' => true,
				],
			]
		);

		// Add the repeater to the widget
		$this->add_control(
			'progress_cards',
			[
				'label' => esc_html__('Repeater List', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[
						'progress' => 90,
						'title' => esc_html__('Frontend Development', 'unlock-addons-for-elementor'),
						'heading_tags' => 'h3',
						'description' => esc_html__('Building modern, responsive websites.', 'unlock-addons-for-elementor'),
						'link' => ['url' => '#'],
					],
					[
						'progress' => 75,
						'title' => esc_html__('Backend Development', 'unlock-addons-for-elementor'),
						'heading_tags' => 'h3',
						'description' => esc_html__('Developing scalable backend systems.', 'unlock-addons-for-elementor'),
						'link' => ['url' => '#'],
					],
					[
						'progress' => 60,
						'title' => esc_html__('UI/UX Design', 'unlock-addons-for-elementor'),
						'heading_tags' => 'h3',
						'description' => esc_html__('Creating user-friendly interfaces.', 'unlock-addons-for-elementor'),
						'link' => ['url' => '#'],
					],
				],
				'title_field' => '{{{ title }}} - {{{ progress }}}%',
			]
		);


		$this->end_controls_section();

		// Style Tab
		$this->start_controls_section(
			'style_section',
			[
				'label' => esc_html__('Progress Bar Style', 'unlock-addons-for-elementor'),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'progress_color',
			[
				'label' => esc_html__('Progress Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .unlockafe-pie-chart .foreground' => 'stroke: {{VALUE}}',
				],
			]
		);

		$this->add_control(
			'track_color',
			[
				'label' => esc_html__('Track Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .unlockafe-pie-chart .background' => 'stroke: {{VALUE}}',
				],
			]
		);

		$this->add_control(
			'bar_thickness',
			[
				'label' => esc_html__('Bar Thickness', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::SLIDER,
				'size_units' => ['px'],
				'range' => [
					'px' => [
						'min' => 1,
						'max' => 20,
					],
				],
				'selectors' => [
					'{{WRAPPER}} .unlockafe-pie-chart-svg circle' => 'stroke-width: {{SIZE}}{{UNIT}}',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'counter_typography',
				'label' => esc_html__('Counter Typography', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .unlockafe-pie-chart-count',
			]
		);

		$this->add_control(
			'counter_color',
			[
				'label' => esc_html__('Counter Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .unlockafe-pie-chart-count' => 'color: {{VALUE}}',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'title_typography',
				'label' => esc_html__('Title Typography', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .unlockafe-pie-chart-title',
			]
		);

		$this->add_control(
			'title_color',
			[
				'label' => esc_html__('Title Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .unlockafe-pie-chart-title a' => 'color: {{VALUE}}',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'description_typography',
				'label' => esc_html__('Description Typography', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .unlockafe-pie-chart-description',
			]
		);

		$this->add_control(
			'description_color',
			[
				'label' => esc_html__('Description Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .unlockafe-pie-chart-description' => 'color: {{VALUE}}',
				],
			]
		);

		$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();
		if ('style-1' === $settings['layout']) : ?>
            <div class="unlockafe-grid lg-3 md-2">
                <?php foreach ($settings['progress_cards'] as $item) : ?>
                    <div class="unlockafe-progress-bar-item unlockafe-pie-chart-wrap elementor-repeater-item-<?php echo esc_attr($item['_id']); ?>">
                        <div class="unlockafe-pie-chart" data-percent="<?php echo esc_attr($item['progress']); ?>">
                            <div class="unlockafe-pie-chart-media">
                                <svg viewBox="0 0 32 32" class="unlockafe-pie-chart-svg">
                                    <circle class="background" cx="16" cy="16" r="14"></circle>
                                    <circle class="foreground" cx="16" cy="16" r="14"></circle>
                                </svg>
                                <?php if (!empty($item['progress'])) : ?>
                                    <span class="unlockafe-pie-chart-count">0%</span>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php if (!empty($item['title'])) : ?>
                            <<?php echo tag_escape($item['heading_tags']); ?> class="unlockafe-pie-chart-title">
                                <a href="<?php echo esc_url($item['link']['url']); ?>">
                                    <?php echo wp_kses_post($item['title']); ?>
                                </a>
                            </<?php echo tag_escape($item['heading_tags']); ?>>
                        <?php endif; ?>

                        <?php if (!empty($item['description'])) : ?>
                            <p class="unlockafe-pie-chart-description"><?php echo wp_kses_post($item['description']); ?></p>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>
		<?php elseif ('style-2' === $settings['layout']) : ?>
			<div class="unlockafe-progress-bar-horizontal-wrap">
				<?php foreach ($settings['progress_cards'] as $item) : ?>
					<div class="unlockafe-progress-bar-item elementor-repeater-item-<?php echo esc_attr($item['_id']); ?>">
						<div class="unlockafe-progress-bar-content">
							<?php if (!empty($item['title'])) : ?>
								<<?php echo tag_escape($item['heading_tags']); ?> class="unlockafe-progress-bar-title">
									<?php echo wp_kses_post($item['title']); ?>
								</<?php echo tag_escape($item['heading_tags']); ?>>
							<?php endif; ?>
							<span class="unlockafe-progress-bar-count">0%</span>
						</div>
						<div class="unlockafe-progress-bar" data-percent="<?php echo esc_attr($item['progress']); ?>">
							<div class="unlockafe-progress-bar-fill"></div>
						</div>
					</div>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	<?php
	}
}
