<?php

class PZY_Map extends \Elementor\Widget_Base
{
    public function get_name()
    {
        return 'pzy_map';
    }

    public function get_title()
    {
        return esc_html__('نقشه ایران', 'pezeshkyar');
    }

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

    public function get_categories()
    {
        return ['pezeshkyar'];
    }

    public function get_keywords()
    {
        return ['pzy', 'map'];
    }

    public function get_script_depends()
    {
        return ['init-jquery', 'map.ir.env', 'map.ir.min','pzy-map.js'];
    }

    public function get_style_depends()
    {
        return ['map.ir.min', 'map.ir.fa'];
    }

    protected function register_controls()
    {
        $this->start_controls_section(
            'content',
            [
                'label' => esc_html__('محتوای ویجت نقشه', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control('lat', [
            'label' => esc_html__('عرض جغرافیایی موقعیت شما', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::NUMBER,
            'default' => 32,
        ]);
        $this->add_control('lng', [
            'label' => esc_html__('طول جغرافیایی موقعیت شما', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::NUMBER,
            'default' => 32,
        ]);
        $this->add_control('zoom', [
            'label' => esc_html__('بزرگنمایی نقشه', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::NUMBER,
            'default' => 6,
        ]);
        $this->add_control(
            'marker',
            [
                'label' => esc_html__('نشانگر نقشه', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'default' => [
                    'url' => '',
                ],
            ]
        );

        $this->add_control(
            'marker_dimensions',
            [
                'label' => esc_html__('ابعاد نشانگر', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::IMAGE_DIMENSIONS,
                'default' => [
                    'width' => '10',
                    'height' => '10',
                ],
            ]
        );

        $this->add_control(
            'marker_x',
            [
                'label' => esc_html__('موقعیت افقی نشانگر', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'default' => 60,
            ]
        );
        $this->add_control(
            'marker_y',
            [
                'label' => esc_html__('موقعیت عمودی نشانگر', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'default' => 14,
            ]
        );

        $this->add_control(
            'logo',
            [
                'label' => esc_html__('انتخاب لوگو جهت نمایش روی نقشه', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'default' => [
                    'url' => '',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Background::get_type(),
            [
                'name' => 'background',
                'types' => [ 'classic', 'gradient', 'video' ],
                'selector' => '{{WRAPPER}} .pzy-map-logo-popup .leaflet-popup-content-wrapper',
            ]
        );

        $this->add_control('address_text', [
            'label' => esc_html__('آدرس متنی موقعیت شما', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::TEXT,
        ]);
        $this->add_control('address_text_icon', [
            'label' => esc_html__('آیکن کنار آدرس', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::ICONS,
        ]);

        $icons = new \Elementor\Repeater();
        $icons->add_control('icon',[
            'label' => esc_html__('آیکن سیستم مربوطه را وارد کنید', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::MEDIA,
            'default' => [
                'url' => Theme_ASSETS_URL . '/images/map-icon.png',
            ],
        ]);
        $icons->add_control('link',[
            'label' => esc_html__('پیوند', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::URL,
            'default' => [
                'url' => '',
            ],
        ]);

        $this->add_control('icons', [
            'label' => esc_html__('سیستم های ثبت شده', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::REPEATER,
            'fields' => $icons->get_controls(),
            'prevent_empty' => false,
        ]);
        $this->end_controls_section();
    }

    public function getMapSettings()
    {
        $presets = [
            'latlng' => [
                'lat' => $this->get_settings_for_display('lat'),
                'lng' => $this->get_settings_for_display('lng'),
            ],
            'zoom' => $this->get_settings_for_display('zoom'),
        ];
        return [
            'element' => '#map-' . $this->getMapElementId(),
            'presets' => $presets,
            'apiKey'=> $GLOBALS['pzy_options']['pzy_mapir_key']??''
//            'apiKey' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImNhODcwNWIxYmI1NGUxZmZkNjE2NDI1ODUyYTQ4MmJhMDJmMTZjZDc4NDQyM2YzN2I5YzM5Yjg0NGVlOGVkY2I0NTcxZjIzN2ExYTQzMmIzIn0.eyJhdWQiOiIxMjE4OSIsImp0aSI6ImNhODcwNWIxYmI1NGUxZmZkNjE2NDI1ODUyYTQ4MmJhMDJmMTZjZDc4NDQyM2YzN2I5YzM5Yjg0NGVlOGVkY2I0NTcxZjIzN2ExYTQzMmIzIiwiaWF0IjoxNzEwMzY2NjQ2LCJuYmYiOjE3MTAzNjY2NDYsImV4cCI6MTcxMDk3MTQ0Niwic3ViIjoiIiwic2NvcGVzIjpbImJhc2ljIl19.DfxZrxoreTZOTC_k2ZSfZi4a34EIkoU1YVs0vyIoy8ePh0v9L22WCP9LdxwR-Dc7YED9LFDEMwNtaIaCHt-Tv9KonIVDd5ZrbqRPg9oKGBcR3ZvLkEFA-oZJgBuJY0gvNgMT4WNJgcvuIv9zgNPP2HkuU4n4_P0XA6a6Ilrtpt4fCnvQxTSJI9lFBoEgbEcd6Js78mUgcjiIXqsb74_5syUw_313JdYZeKoxrWtdRccsrlngzEUToH-1qwa_jpzfLHoEPRECvoA9UZc2p79uMJoS5iaKIKPsviGOaX1OVe9qlSgdhH-bfNx_-yV2gxgTZZ9yTl5XJEu2PYtXCGjWlw'
        ];
    }

    protected function getMapElementId()
    {
        return $this->get_id() . '_element';
    }

    protected function getMarkerSettings()
    {
        $settings = [];
        $marker = $this->get_settings_for_display('marker')['url'];
        if (!empty($marker)) {
            $settings = [
                'latlng' => [
                    'lat' => $this->get_settings_for_display('lat'),
                    'lng' => $this->get_settings_for_display('lng'),
                ],
                'icon' => [
                    'iconUrl' => $marker,
                    'iconSize' => [$this->get_settings_for_display('marker_dimensions')['width'], $this->get_settings_for_display('marker_dimensions')['height']],
                    'iconAnchor' => [$this->get_settings_for_display('marker_x'), $this->get_settings_for_display('marker_y')]
                ],
                'draggable' => false,
                'popup' => false,
            ];
        }
        return $settings;
    }

    protected function getPopupSettings()
    {
        $logo = $this->get_settings_for_display('logo')['url'];
        if (!empty($logo)) {
            return [
                'content' => "<div class='pzy-map-popup-content'><img class='pzy-map-logo' src='$logo'/></div>",
                'options' => [
                    'className' => 'pzy-map-logo-popup',
                    'maxWidth' => 100,
                    'closeButton' => false,
                ],
            ];
        }
        return [];
    }


    protected function render()
    {
        $settings = $this->get_settings_for_display();
        ?>
        <div class="pzy-map-box">

            <div id="<?php echo 'map-' .  esc_attr($this->getMapElementId()) ?>" class="mapir"
                 data-settings='<?php echo esc_attr(json_encode($this->getMapSettings())) ?>'
                <?php if (count($this->getMarkerSettings()) > 0): ?>
                    data-marker='<?php echo esc_attr(json_encode($this->getMarkerSettings())) ?>'
                <?php endif; ?>
                <?php if (count($this->getPopupSettings()) > 0): ?>
                    data-popup='<?php echo esc_attr(json_encode($this->getPopupSettings())) ?>'
                <?php endif; ?>
            ></div>
            <!--            <img class="pzy-map-screen" src="--><?php //echo $settings['location_screen']['url']
            ?><!--" alt="Location">-->

            <div class="pzy-map-text-address">
                <?php if (isset($settings['address_text_icon'])) Elementor\Icons_Manager::render_icon($settings['address_text_icon'], ['class' => 'pzy-address-icon']) ?>
                <address class="pzy-map-address"><?php echo $settings['address_text'] ?? '' ?></address>
            </div>
            <?php if (count($settings['icons'] ?? []) > 0): ?>
                <div class="pzy-map-icons">
                    <?php foreach ($settings['icons'] as $icon): ?>
                        <a href="<?php echo esc_url($icon['link']['url']) ?>">
                            <img src="<?php echo esc_url($icon['icon']['url']) ?>" alt="registered location">
                        </a>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
        <?php
    }
}