<?php

namespace Calio\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Circular progress widget based on the Progress Pie interaction model.
 */
class ProgressPieWidget extends BaseWidget
{
    public function get_name(): string
    {
        return 'calio-progress-pie';
    }

    public function get_title(): string
    {
        return esc_html__('Progress Pie', 'calio-addon');
    }

    public function get_icon(): string
    {
        return 'eicon-counter-circle';
    }

    public function get_style_depends(): array
    {
        return $this->resolve_widget_style_depends('calio-widget-progress-pie');
    }

    public function get_script_depends(): array
    {
        return $this->resolve_widget_script_depends('calio-widget-progress-pie');
    }

    public function get_keywords(): array
    {
        return array_merge(parent::get_keywords(), ['progress', 'pie', 'circle']);
    }

    protected function register_controls(): void
    {
        $this->start_controls_section(
            'section_content_layout',
            [
                'label' => esc_html__('Layout', 'calio-addon'),
            ]
        );

        $this->add_control(
            'percent',
            [
                'label' => esc_html__('Progress Value', 'calio-addon'),
                'type' => Controls_Manager::NUMBER,
                'default' => 75,
                'dynamic' => ['active' => true],
            ]
        );

        $this->add_control(
            'duration',
            [
                'label' => esc_html__('Duration(s)', 'calio-addon'),
                'type' => Controls_Manager::NUMBER,
                'default' => 1,
                'min' => 0.1,
                'step' => 0.1,
            ]
        );

        $this->add_control(
            'title',
            [
                'label' => esc_html__('Progress Pie Title', 'calio-addon'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => esc_html__('Your title text here', 'calio-addon'),
                'default' => esc_html__('Progress Pie Title', 'calio-addon'),
                'label_block' => true,
                'dynamic' => ['active' => true],
            ]
        );

        $this->add_control(
            'hide_title_divider',
            [
                'label' => esc_html__('Hide Title Divider', 'calio-addon'),
                'type' => Controls_Manager::SWITCHER,
                'return_value' => ' calio-no-divider',
                'condition' => [
                    'title!' => '',
                ],
            ]
        );

        $this->add_control(
            'before',
            [
                'label' => esc_html__('Before Text', 'calio-addon'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => esc_html__('Your before text here', 'calio-addon'),
                'label_block' => true,
                'dynamic' => ['active' => true],
            ]
        );

        $this->add_control(
            'text',
            [
                'label' => esc_html__('Middle Text', 'calio-addon'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => esc_html__('Your middle text here', 'calio-addon'),
                'label_block' => true,
                'dynamic' => ['active' => true],
            ]
        );

        $this->add_control(
            'after',
            [
                'label' => esc_html__('After Text', 'calio-addon'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => esc_html__('Your after text here', 'calio-addon'),
                'label_block' => true,
                'dynamic' => ['active' => true],
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_style_layout',
            [
                'label' => esc_html__('Style', 'calio-addon'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'progress_pie_heading',
            [
                'label' => esc_html__('Progress Pie', 'calio-addon'),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $this->add_control(
            'progress_background',
            [
                'label' => esc_html__('Pie Fill Color', 'calio-addon'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .calio-progress-pie-wrapper .calio-progress-pie svg ellipse' => 'stroke: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'progress_color',
            [
                'label' => esc_html__('Pie Bar Color', 'calio-addon'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .calio-progress-pie-wrapper .calio-progress-pie svg path' => 'stroke: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'before_text_color',
            [
                'label' => esc_html__('Before Text Color', 'calio-addon'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .calio-progress-pie-before' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'before!' => '',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'before_text_typography',
                'selector' => '{{WRAPPER}} .calio-progress-pie-before',
                'condition' => [
                    'before!' => '',
                ],
            ]
        );

        $this->add_control(
            'middle_text_color',
            [
                'label' => esc_html__('Middle Text Color', 'calio-addon'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .calio-progress-pie-text' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'text!' => '',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'middle_text_typography',
                'selector' => '{{WRAPPER}} .calio-progress-pie-text',
                'condition' => [
                    'text!' => '',
                ],
            ]
        );

        $this->add_control(
            'number_color',
            [
                'label' => esc_html__('Percentage Color', 'calio-addon'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .calio-progress-pie-number' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'text' => '',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'percentage_typography',
                'selector' => '{{WRAPPER}} .calio-progress-pie-number',
                'condition' => [
                    'text' => '',
                ],
            ]
        );

        $this->add_control(
            'after_text_color',
            [
                'label' => esc_html__('After Text Color', 'calio-addon'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .calio-progress-pie-after' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'after!' => '',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'after_text_typography',
                'selector' => '{{WRAPPER}} .calio-progress-pie-after',
                'condition' => [
                    'after!' => '',
                ],
            ]
        );

        $this->add_responsive_control(
            'progress_padding',
            [
                'label' => esc_html__('Padding', 'calio-addon'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .calio-progress-pie-wrapper .calio-progress-pie' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'line_width',
            [
                'label' => esc_html__('Line Width', 'calio-addon'),
                'type' => Controls_Manager::NUMBER,
                'default' => 8,
                'min' => 1,
            ]
        );

        $this->add_control(
            'line_cap',
            [
                'label' => esc_html__('Line Cap', 'calio-addon'),
                'type' => Controls_Manager::SELECT,
                'default' => 'round',
                'options' => [
                    'round' => esc_html__('Rounded', 'calio-addon'),
                    'square' => esc_html__('Square', 'calio-addon'),
                    'butt' => esc_html__('Butt', 'calio-addon'),
                ],
            ]
        );

        $this->add_control(
            'progress_title_heading',
            [
                'label' => esc_html__('Title', 'calio-addon'),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
                'condition' => [
                    'title!' => '',
                ],
            ]
        );

        $this->add_control(
            'title_background',
            [
                'label' => esc_html__('Background', 'calio-addon'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .calio-progress-pie-wrapper .calio-progress-pie-title' => 'background-color: {{VALUE}}; border-top: none;',
                ],
                'condition' => [
                    'title!' => '',
                ],
            ]
        );

        $this->add_control(
            'title_color',
            [
                'label' => esc_html__('Color', 'calio-addon'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .calio-progress-pie-wrapper .calio-progress-pie-title' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'title!' => '',
                ],
            ]
        );

        $this->add_responsive_control(
            'title_padding',
            [
                'label' => esc_html__('Padding', 'calio-addon'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .calio-progress-pie-wrapper .calio-progress-pie-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'condition' => [
                    'title!' => '',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'title_typography',
                'selector' => '{{WRAPPER}} .calio-progress-pie-wrapper .calio-progress-pie-title',
                'condition' => [
                    'title!' => '',
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render(): void
    {
        $id = $this->get_id();
        $settings = $this->get_settings_for_display();

        $percent = max(0, min(100, (int) ($settings['percent'] ?? 75)));
        $duration = max(0.1, (float) ($settings['duration'] ?? 1));
        $line_width = max(1, (int) ($settings['line_width'] ?? 8));
        $line_cap = (string) ($settings['line_cap'] ?? 'round');
        if (!in_array($line_cap, ['round', 'square', 'butt'], true)) {
            $line_cap = 'round';
        }

        $classes = [
            'calio-progress-pie',
            'calio-pp-lc-' . $line_cap,
        ];

        if (empty($settings['text'])) {
            $classes[] = 'calio-pp-percent';
        }

        $this->add_render_attribute(
            'pp-settings',
            [
                'id' => esc_attr($id),
                'class' => $classes,
                'role' => 'progressbar',
                'data-goal' => (string) $percent,
                'data-speed' => (string) ($duration * 15),
                'data-barsize' => (string) $line_width,
                'aria-valuemin' => '0',
                'aria-valuemax' => '100',
                'aria-valuenow' => '0',
            ]
        );

        $hide_title_divider = '';
        if (is_string($settings['hide_title_divider'] ?? null) && strpos((string) $settings['hide_title_divider'], 'calio-no-divider') !== false) {
            $hide_title_divider = ' calio-no-divider';
        }

        echo '<div id="' . esc_attr($id) . '_container" class="calio-progress-pie-wrapper">';
        echo '<div ';
        $this->print_render_attribute_string('pp-settings');
        echo '>';
        echo '<div class="calio-progress-pie-label">';

        if (!empty($settings['before'])) {
            echo '<div class="calio-progress-pie-before">' . esc_html((string) $settings['before']) . '</div>';
        }

        if (!empty($settings['text'])) {
            echo '<div class="calio-progress-pie-text">' . esc_html((string) $settings['text']) . '</div>';
        } else {
            echo '<div class="calio-progress-pie-number"></div>';
        }

        if (!empty($settings['after'])) {
            echo '<div class="calio-progress-pie-after">' . esc_html((string) $settings['after']) . '</div>';
        }

        echo '</div>';
        echo '</div>';

        if (!empty($settings['title'])) {
            echo '<h4 class="calio-progress-pie-title' . esc_attr($hide_title_divider) . '">' . esc_html((string) $settings['title']) . '</h4>';
        }

        echo '</div>';
    }
}
