<?php

use Elementor\Controls_Manager;
use Elementor\Repeater;

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

class Bikys_Chart extends Elementor\Widget_Base {

    public function get_name() {
        return 'bikys-chart';
    }

    public function get_title() {
        return esc_html__('Bikys Chart', 'bikys');
    }

    public function get_categories() {
        return array('bikys-addons');
    }

    public function get_icon() {
        return 'eicon-integration';
    }

    public function get_script_depends() {
        return ['bikys-elementor-chart', 'chart'];
    }

    protected function register_controls() {
        $this->start_controls_section(
            'section_chart',
            [
                'label' => esc_html__('Radar Chart', 'bikys'),
            ]
        );

        $repeater = new Repeater();

        $repeater->add_control(
            'title',
            [
                'label' => esc_html__('Title', 'bikys'),
                'type'  => Controls_Manager::TEXT,
            ]
        );

        $repeater->add_control(
            'size',
            [
                'label'      => esc_html__('Size', 'bikys'),
                'type'       => Controls_Manager::SLIDER,
                'range'      => [
                    'px' => [
                        'min'  => 0,
                        'max'  => 10,
                        'step' => 0.1,
                    ],
                ],
                'size_units' => [
                    'px'
                ]
            ]
        );

        $this->add_control(
            'charts',
            [
                'label'       => '',
                'type'        => Controls_Manager::REPEATER,
                'fields'      => $repeater->get_controls(),
                'default'     => [
                    [
                        'title' => esc_html__('STRENGTH', 'bikys'),
                        'size' => [
                                'units'=> 'px',
                                'size'=> 3
                        ]
                    ],
                    [
                        'title' => esc_html__('SPEED', 'bikys'),
                        'size' => [
                            'units'=> 'px',
                            'size'=> 3
                        ]
                    ],
                    [
                        'title' => esc_html__('LIGHTWEIGHT', 'bikys'),
                        'size' => [
                            'units'=> 'px',
                            'size'=> 3
                        ]
                    ],
                    [
                        'title' => esc_html__('ACCELERATIOM', 'bikys'),
                        'size' => [
                            'units'=> 'px',
                            'size'=> 3
                        ]
                    ],
                    [
                        'title' => esc_html__('CONTROL', 'bikys'),
                        'size' => [
                            'units'=> 'px',
                            'size'=> 3
                        ]
                    ],
                    [
                        'title' => esc_html__('AGILITY', 'bikys'),
                        'size' => [
                            'units'=> 'px',
                            'size'=> 3
                        ]
                    ],
                ],
                'title_field' => '{{{ title }}}',
            ]
        );

        $this->end_controls_section();

    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $labels   = [];
        $datasets = [];
        foreach ($settings['charts'] as $index => $item) {
            $labels[] = $item['title'];
            if(!empty($item['size']['size'])) {
                $datasets[] = round($item['size']['size'],2);
            }else {
                $datasets[] = 0;
            }

        }

        ?>
        <canvas data-labels="<?php echo esc_attr(json_encode($labels));?>" data-asets="<?php echo esc_attr(json_encode($datasets));?>" class="myChart"></canvas>
        <?php
    }
}

$widgets_manager->register(new Bikys_Chart());
