<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class IDSC_Elementor_Map extends \Elementor\Widget_Base {

    public function get_name() {
        return 'idsc_map';
    }

    public function get_title() {
        return __( 'Map', 'idsc' );
    }

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

    public function get_categories() {
        return array( 'idsc' );
    }

    public function get_keywords() {
        return array( 'map', 'google', 'openstreetmap', 'location' );
    }

    protected function register_controls() {
        $this->start_controls_section( 'section_content', array(
            'label' => __( 'Map', 'idsc' ),
        ) );

        $this->add_control( 'address', array(
            'label' => __( 'Address', 'idsc' ),
            'type'  => \Elementor\Controls_Manager::TEXT,
        ) );

        $this->add_control( 'height', array(
            'label'   => __( 'Height (px)', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SLIDER,
            'range'   => array( 'px' => array( 'min' => 100, 'max' => 800 ) ),
            'default' => array( 'size' => 400 ),
        ) );

        $this->add_control( 'zoom', array(
            'label'   => __( 'Zoom Level', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SLIDER,
            'range'   => array( 'px' => array( 'min' => 1, 'max' => 20 ) ),
            'default' => array( 'size' => 14 ),
        ) );

        $this->add_control( 'provider', array(
            'label'   => __( 'Map Provider', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'openstreetmap',
            'options' => array(
                'openstreetmap' => __( 'OpenStreetMap', 'idsc' ),
                'google'        => __( 'Google Maps', 'idsc' ),
            ),
        ) );

        $this->end_controls_section();
    }

    protected function render() {
        $s = $this->get_settings_for_display();

        $atts  = ' address="' . esc_attr( $s['address'] ) . '"';
        $atts .= ' height="' . esc_attr( $s['height']['size'] ?? 400 ) . '"';
        $atts .= ' zoom="' . esc_attr( $s['zoom']['size'] ?? 14 ) . '"';
        $atts .= ' provider="' . esc_attr( $s['provider'] ) . '"';

        echo do_shortcode( '[idsc_map' . $atts . ']' );
    }
}
