<?php
defined('ABSPATH') || exit;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

class Willro_Reviews_Elementor extends Widget_Base {

    public function get_name() { return 'willro_reviews'; }
    public function get_title() { return __( 'Willro Reviews', 'willro-reviews' ); }
    public function get_icon() { return 'eicon-star'; }
    public function get_categories() { return [ 'general' ]; }

    protected function register_controls() {

        $this->start_controls_section( 'settings', [ 'label' => __( 'Settings', 'willro-reviews' ) ] );

        $this->add_control( 'template', [
            'label' => __( 'Template', 'willro-reviews' ),
            'type' => Controls_Manager::SELECT,
            'options' => [
                '1' => __( 'All Reviews', 'willro-reviews' ),
                '2' => __( 'Reviews with Rating', 'willro-reviews' ),
                '3' => __( 'Video Reviews', 'willro-reviews' ),
            ],
            'default' => '1'
        ] );


        $this->add_control( 'mediaType', [
            'label' => __( 'Media Type', 'willro-reviews' ),
            'type' => Controls_Manager::SELECT,
            'options' => [
                'ALL' => __( 'All', 'willro-reviews' ),
                'TEXT' => __( 'Text', 'willro-reviews' ),
                'VIDEO' => __( 'Video', 'willro-reviews' ),
                'IMAGE' => __( 'Image', 'willro-reviews' ),
            ],
            'default' => 'ALL',
        ] );

        $this->add_control( 'sortType', [
            'label' => __( 'Sort Type', 'willro-reviews' ),
            'type' => Controls_Manager::SELECT,
            'options' => [
                'MOST_RELEVANT' => __( 'Most Relevant', 'willro-reviews' ),
                'HIGHEST_REVIEWS' => __( 'Highest Reviews', 'willro-reviews' ),
                'LOWEST_REVIEWS' => __( 'Lowest Reviews', 'willro-reviews' ),
                'RECENT_REVIEWS' => __( 'Recent Reviews', 'willro-reviews' ),
                'RECENT_WITH_MIN_ONE_REVIEW' => __( 'Recent With Min One Review', 'willro-reviews' ),
            ],
            'default' => 'MOST_RELEVANT',
        ] );

        $this->add_control( 'rating', [
            'label' => __( 'Rating', 'willro-reviews' ),
            'type' => Controls_Manager::SELECT,
            'options' => [
                'ALL' => __( 'All', 'willro-reviews' ),
                '5' => __( '5 Stars', 'willro-reviews' ),
                '4' => __( '4 Stars', 'willro-reviews' ),
                '3' => __( '3 Stars', 'willro-reviews' ),
                '2' => __( '2 Stars', 'willro-reviews' ),
                '1' => __( '1 Star', 'willro-reviews' ),
            ],
            'default' => 'ALL',
        ] );


        $this->add_control( 'limit', [
            'label' => __( 'Limit', 'willro-reviews' ),
            'type' => Controls_Manager::NUMBER,
            'default' => 10
        ] );

        $this->end_controls_section();
    }

    protected function render() {
        $html = Willro_Reviews_Renderer::render( $this->get_settings_for_display() );
        $html = preg_replace( '/<!--[\s\S]*?-->/', '', $html );

        echo wp_kses( $html, Willro_Reviews_Renderer::allowed_html() );
    }
}
