<?php

namespace ConstructionArcElements\Modules\ProgressBar\Widgets;

// Elementor Classes
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Image_Size;
use Elementor\Icons_Manager;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Typography;
use Elementor\Core\Schemes\Typography;
use Elementor\Core\Schemes\Color;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Text_Stroke;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Css_Filter;
use Elementor\Utils;
use Elementor\Repeater;

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

class ProgressBar extends Widget_Base {

    /** Widget Name */
    public function get_name() {
        return 'constructionarc-progress-bar';
    }

    /** Widget Title */
    public function get_title() {
        return esc_html__('Progress Bar Block ', 'construction-arc');
    }

    /** Icon */
    public function get_icon() {
        return 'eicon-skill-bar';
    }

    public function get_keywords() {
		return [ 'progress', 'bar', 'design' ];
	}

	public function get_script_depends() {
		return [ 'imagesloaded' ];
	}

    /** Category */
    public function get_categories() {
        return ['constructionarc-elements'];
    }

    /** Controls */
    protected function register_controls() {

        $this->start_controls_section(
            'progressbar', [
                'label' => esc_html__('Progress Bars', 'construction-arc'),
            ]
        );

            $repeater = new Repeater();

            $repeater->add_control(
                'progressbar_title', [
                    'label' => esc_html__('Title', 'construction-arc'),
                    'type' => Controls_Manager::TEXT,
                    'label_block' => true,
                    'default' => esc_html__('Web Designer', 'construction-arc'),
                ]
            );

            $repeater->add_control(
                'title_html_tag', [
                    'label' => esc_html__('Title HTML Tag', 'construction-arc'),
                    'type' => Controls_Manager::SELECT,
                    'default' => 'span',
                    'options' => [
                        'h1' => esc_html__('H1', 'construction-arc'),
                        'h2' => esc_html__('H2', 'construction-arc'),
                        'h3' => esc_html__('H3', 'construction-arc'),
                        'h4' => esc_html__('H4', 'construction-arc'),
                        'h5' => esc_html__('H5', 'construction-arc'),
                        'h6' => esc_html__('H6', 'construction-arc'),
                        'div' => esc_html__('div', 'construction-arc'),
                        'span' => esc_html__('span', 'construction-arc'),
                        'p' => esc_html__('p', 'construction-arc')
                    ],
                ]
            );

            $repeater->add_control(
                'progressbar_percentage',[
                    'label' => esc_html__( 'Percentage', 'construction-arc' ),
                    'type' => Controls_Manager::SLIDER,
                    'default' => [
                        'size' => '90',
                        'unit' => '%',
                    ],
                ]
            );

            $repeater->add_control(
                'display_percentage',[
                    'label' => esc_html__( 'Display Percentage', 'construction-arc' ),
                    'type' => Controls_Manager::SWITCHER,
                    'label_on' => esc_html__( 'Show', 'construction-arc' ),
                    'label_off' => esc_html__( 'Hide', 'construction-arc' ),
                    'return_value' => 'show',
                    'default' => 'show',
                ]
            );

            $repeater->add_control(
                'progress_item_color',[
                    'label' => esc_html__( 'Color', 'construction-arc' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} {{CURRENT_ITEM}} .constructionarc-progress-bar-length' => 'background-color: {{VALUE}};',
                    ],
                ]
            );

            $this->add_control(
                'progressbar_block', [
                    'label' => esc_html__('Progress Bars Items', 'construction-arc'),
                    'type' => Controls_Manager::REPEATER,
                    'fields' => $repeater->get_controls(),
                    'default' => [
                        [
                            'progressbar_title' => esc_html__('Web Designer', 'construction-arc'),
                            'progressbar_percentage' => 80,
                            'display_percentage'    => 'show',
                            'progress_item_color' => '#000000'
                        ],
                        [
                            'progressbar_title' => esc_html__('WordPress', 'construction-arc'),
                            'progressbar_percentage' => 90,
                            'display_percentage'    => 'show',
                            'progress_item_color' => '#07F4D2'
                        ],
                        [
                            'progressbar_title' => esc_html__('HTML Design', 'construction-arc'),
                            'progressbar_percentage' => 55,
                            'display_percentage'    => 'show',
                            'progress_item_color' => '#4E9D06'
                        ],
                        [
                            'progressbar_title' => esc_html__('Planning', 'construction-arc'),
                            'progressbar_percentage' => 97,
                            'display_percentage'    => 'show',
                            'progress_item_color' => '#8224e3'
                        ],
                    ],
                    'title_field' => '{{{ progressbar_title }}}',
                ]
            );

        $this->end_controls_section();


        $this->start_controls_section(
            'progressbar_settings', [
                'label' => esc_html__('Settings', 'construction-arc'),
            ]
        );

            $this->add_responsive_control(
                'progressbar_spacing', [
                    'label' => esc_html__('Space Progress Bar Item', 'construction-arc'),
                    'type' => Controls_Manager::SLIDER,
                    'size_units' => [ 'px', 'em', 'rem', 'vw', 'custom' ],
                    'range' => [
                        'rem' => [
                            'min' => 0,
                            'max' => 10,
                            'step' => 1,
                        ]
                    ],
                    'default' => [
                        'unit' => 'rem',
                        'size' => '',
                    ],
                    'selectors' => [
                        '{{WRAPPER}} .constructionarc-progress-bar-wrapper' => 'gap: {{SIZE}}{{UNIT}};',
                    ],
                ]
            );

            $this->add_responsive_control(
                'progressbar_spacing_content', [
                    'label' => esc_html__('Space Content', 'construction-arc'),
                    'type' => Controls_Manager::SLIDER,
                    'size_units' => [ 'px', 'em', 'rem', 'vw', 'custom' ],
                    'range' => [
                        'px' => [
                            'min' => 0,
                            'max' => 100,
                            'step' => 1,
                        ]
                    ],
                    'default' => [
                        'unit' => 'px',
                        'size' => '5',
                    ],
                    'selectors' => [
                        '{{WRAPPER}} .constructionarc-progress-item' => 'gap: {{SIZE}}{{UNIT}};',
                    ],
                ]
            );


        $this->end_controls_section();

        $this->start_controls_section(
			'section_progress_style',[
				'label' => esc_html__( 'Progress Bar', 'construction-arc' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

            $this->add_control(
                'progress_color',[
                    'label' => esc_html__( 'Color', 'construction-arc' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .constructionarc-progress-bar-length' => 'background-color: {{VALUE}};',
                    ],
                ]
            );

            $this->add_control(
                'progress_bg_color',[
                    'label' => esc_html__( 'Background Color', 'construction-arc' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .constructionarc-progress-bar' => 'background-color: {{VALUE}};',
                    ],
                ]
            );

            $this->add_responsive_control(
                'progress_height',[
                    'label' => esc_html__( 'Height', 'construction-arc' ),
                    'type' => Controls_Manager::SLIDER,
                    'size_units' => [ 'px', 'em', 'rem', 'custom' ],
                    'selectors' => [
                        '{{WRAPPER}} .constructionarc-progress-bar' => 'height: {{SIZE}}{{UNIT}};',
                    ],
                ]
            );

            $this->add_responsive_control(
                'progress_border_radius',[
                    'label' => esc_html__( 'Border Radius', 'construction-arc' ),
                    'type' => Controls_Manager::SLIDER,
                    'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
                    'selectors' => [
                        '{{WRAPPER}} .constructionarc-progress-bar' => 'border-radius: {{SIZE}}{{UNIT}};',
                    ],
                ]
            );

            $this->add_control(
                'inner_text_heading',[
                    'label' => esc_html__( 'Percentage Text', 'construction-arc' ),
                    'type' => Controls_Manager::HEADING,
                    'separator' => 'before',
                ]
            );

            $this->add_control(
                'progress_inline_color',[
                    'label' => esc_html__( 'Color', 'construction-arc' ),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .constructionarc-progress-bar-length span' => 'color: {{VALUE}};',
                    ],
                ]
            );

            $this->add_group_control(
                Group_Control_Typography::get_type(),[
                    'name' => 'progress_inner_typography',
                    'selector' => '{{WRAPPER}} .constructionarc-progress-bar-length span',
                    'exclude' => [
                        'line_height',
                    ],
                ]
            );
        
        $this->end_controls_section();


        $this->start_controls_section(
            'title_style', [
                'label' => esc_html__('Title', 'construction-arc'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

            $this->add_control(
                'progress_title_color', [
                    'label' => esc_html__('Color', 'construction-arc'),
                    'type' => Controls_Manager::COLOR,
                    'default' => '',
                    'selectors' => [
                        '{{WRAPPER}} .constructionarc-progress-title' => 'color: {{VALUE}}',
                    ],
                ]
            );

            $this->add_group_control(
                Group_Control_Typography::get_type(), [
                    'name' => 'progress_title_typography',
                    'label' => esc_html__('Typography', 'construction-arc'),
                    'selector' => '{{WRAPPER}} .constructionarc-progress-title',
                ]
            );

            $this->add_group_control(
                Group_Control_Text_Stroke::get_type(),[
                    'name' => 'progress_title_stroke',
                    'selector' => '{{WRAPPER}} .constructionarc-progress-title',
                ]
            );

            $this->add_group_control(
                Group_Control_Text_Shadow::get_type(),[
                    'name' => 'progress_title_shadow',
                    'selector' => '{{WRAPPER}} .constructionarc-progress-title',
                ]
            );

        $this->end_controls_section();

    }

    /** Render Layout */
    protected function render() {

        $settings = $this->get_settings_for_display();

        $progressbars = $settings['progressbar_block'];

        $wrapper_tag = 'div';

        

        $this->add_render_attribute('wrapper', 'class', [
                'constructionarc-progress-bar-wrapper',
            ]
        );
        $this->add_render_attribute( 'title', 'class', ['constructionarc-progress-title'] );

        ?>
        <<?php Utils::print_validated_html_tag( $wrapper_tag ); ?> <?php $this->print_render_attribute_string( 'wrapper' ); ?>>
            <?php 
                foreach ($progressbars as $key => $progressbar) { 
                    
                    $title_tag = Utils::validate_html_tag( $progressbar['title_html_tag'] );
            ?>
                <div class="constructionarc-progress-item elementor-repeater-item-<?php echo esc_attr( $progressbar['_id'] ); ?>">
                    <?php if ( ! empty( $progressbar['progressbar_title'] ) ) : ?>
                        <<?php Utils::print_validated_html_tag( $title_tag ); ?> <?php $this->print_render_attribute_string( 'title' ); ?>>
                            <?php echo esc_html( $progressbar['progressbar_title'] ); ?>
                        </<?php Utils::print_validated_html_tag( $title_tag ); ?>>
                    <?php endif; ?>

                    <div class="constructionarc-progress-bar" data-width="<?php echo absint( $progressbar['progressbar_percentage']['size'] ); ?>">
                        <div class="constructionarc-progress-bar-length">
                            <?php if( !empty( $progressbar['display_percentage'] && $progressbar['display_percentage'] == 'show' ) ){ ?>
                                <span><?php echo absint( $progressbar['progressbar_percentage']['size'] ) . "%"; ?></span>
                            <?php } ?>
                        </div>
                    </div>
                </div>
            <?php } ?>
        </<?php Utils::print_validated_html_tag( $wrapper_tag ); ?>>
        <?php
    }

}