<?php

class MapMarker
{
  public array $Position;
  public string $Text;
}

class MapPolyLine
{
  public array $Points = array();
  public string $Color = 'black';
}

class Map extends Model
{
  public array $Position;
  public int $Zoom;
  public string $Key;
  public $OnClickObject;
  public string $MarkerText;
  public array $Markers;
  public array $PolyLines;

  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Zoom = 2;
    $this->Position = array('Lat' => 40.178873, 'Lng' => 65.039062);
    $this->Key = '';
    $this->ShowMarker = false;
    $this->Markers = array();
    $this->PolyLines = array();
    $this->OnClickObject = null;
  }

  function Show(): string
  {
    return '';
  }
}

class MapGoogle extends Map
{
  function ShowPage(Page $Page): string
  {
    $Page->Load = 'initialize()';
    //$Page->Unload = 'google.maps.Unload()';
    $Page->BasicHTML = true;
    $Page->HideMenu = true;
    $Output = '<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key='.$this->Key.'"></script>';
    $Output .= '<script type="text/javascript">

    var map;
    var tinyIcon;

    function initialize()
    {


      //if (GBrowserIsCompatible())
      {
        map = new google.maps.Map(document.getElementById("map_canvas"));
        map.center = {lat: 45.518, lng: -122.672};
        map.zoom = 18;
        map.heading = 90;
        map.tilt = 45;
        map.mapTypeId = \'satellite\';
        map.zoom = '.$this->Zoom.';
        map.setCenter(new google.maps.LatLng('.$this->Position['Lat'].', '.$this->Position['Lng'].'), '.$this->Zoom.');
        //map.setUIToDefault();
        //map.addControl(new google.maps.OverviewMapControl(new google.maps.Size(128, 96)));
        ';
    if ($this->OnClickObject != null)
    {
      $Output .= 'google.maps.event.addListener(map,"click", function(overlay, point)
        {
          if (point)
          {
            set_return (point.lat() + ";" + point.lng(),"'.$this->OnClickObject.'");
            window.close();
          }
        });';
    }
    $Output .= '}';

    // Create our "tiny" marker icon
    //var tinyIcon = new google.maps.Icon();
    //tinyIcon.image = "'.$this->System->Link('/Modules/Map/point.gif').'";
    //tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    //tinyIcon.iconSize = new google.maps.Size(10, 10);
    //tinyIcon.shadowSize = new google.maps.Size(10, 10);
    //tinyIcon.iconAnchor = new google.maps.Point(5, 5);
    //tinyIcon.infoWindowAnchor = new google.maps.Point(5, 1);

    foreach ($this->Markers as $Marker)
    {
      $Output .= 'map.addOverlay(new google.maps.Marker(new google.maps.LatLng('.$Marker->Position['Lat'].', '.
      $Marker->Position['Lng'].'), {title: "'.$Marker->Text.'" }));';
    }
    $Output .= '}
    </script>';
    $Output .= '<div id="map_canvas" style="width: 100%; height: 98%;"></div>';
    return $Output;
  }
}

class MapSeznam extends Map
{

}

class MapOpenStreetMaps extends Map
{
  function GetPageHeader(): string
  {
    $Output = '<link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css"
    integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
    crossorigin=""/>';
    $Output .= '<script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"
    integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
    crossorigin=""></script>';
    return $Output;
  }

  function ShowPage(Page $Page): string
  {
    $this->System->PageHeaders[] = array($this, 'GetPageHeader');
    $Page->Load = 'initialize()';
    $Page->BasicHTML = true;
    $Page->HideMenu = true;
    $Output = '<script type="text/javascript">
    function initialize()
    {
      var mymap = L.map(\'mapid\').setView(['.$this->Position['Lat'].','.$this->Position['Lng'].'], '.$this->Zoom.');
      L.tileLayer(\'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}\', {
        attribution: \'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>\',
        maxZoom: 18,
        id: \'mapbox.streets\',
        accessToken: \'pk.eyJ1IjoiY2hyb25vc2N6IiwiYSI6ImNrMnRqMzh3dTByM2czYnB2dnhnaWY0bjMifQ.ONeadi5sPjG5arnvfhxxGQ\'
      }).addTo(mymap);
    ';

    foreach ($this->Markers as $Marker)
    {
      $Output .= 'var marker = L.marker(['.$Marker->Position['Lat'].', '.$Marker->Position['Lng'].'], { title: \''.$Marker->Text.'\'}).addTo(mymap);
      ';
    }
    foreach ($this->PolyLines as $PolyLine)
    {
      $Points = array();
      foreach ($PolyLine->Points as $Point)
        $Points[] = '['.$Point['Lat'].', '.$Point['Lng'].']';
      $Output .= 'var latlngs = ['.implode(', ', $Points).'];
        var polyline = L.polyline(latlngs, {color: \'red\'}).addTo(mymap);
      ';
    }
    if ($this->OnClickObject != null)
    {
      $Output .= 'function onMapClick(e)
      {
          if (e)
          {
            set_return (e.latlng.lat + ";" + e.latlng.lng,"'.$this->OnClickObject.'");
            window.close();
          }
        }
        mymap.on(\'click\', onMapClick);';
    }
    $Output .= '
    }
    </script>';
    $Output .= '<div id="mapid" style="width: 100%; height: 90%;"></div>';
    return $Output;
  }
}