<template>
  <LayoutContainer>
    <v-responsive :aspect-ratio="1" width="100%">
      <no-ssr>
        <l-map
          id="map"
          :zoom="zoom"
          :center="center"
          :bounds="bounds"
          :options="{scrollWheelZoom: false, zoomSnap: 0.25}"
          @ready="$emit('ready', $event, field)"
          @update="$emit('update', $event, field)"
        >
          <l-tile-layer
            url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
            attribution="&copy; <a href='http://osm.org/copyright'>OpenStreetMap</a> contributors"
          />
          <!-- <l-marker :lat-lng="[55.9464418,8.1277591]" />-->
        </l-map>
      </no-ssr>
    </v-responsive>

    <div class="advice">
      {{ $t("click_on_land_to_go_there") }}
    </div>

    <v-row class="map-shortcuts">
      <v-col v-for="shortcut in shortcuts" :key="shortcut.src" cols="2">
        <div @click="setMapBounds(shortcut.bounds)">
          <nuxt-img
            :src="shortcut.src"
            :alt="shortcut.alt"
          />
        </div>
      </v-col>
    </v-row>
  </LayoutContainer>
</template>

<script>
export default {

  data () {
    const defaultCenter = [46.71, 1.94]
    const defaultZoom = 5.75
    const defaultBounds = [[51.03, -5.78], [41.2, 9.70]]

    return {
      defaultCenter,
      defaultZoom,
      defaultBounds,
      center: defaultCenter,
      zoom: defaultZoom,
      bounds: defaultBounds,
      shortcuts: [
        { src: '/images/metropole.png', alt: 'Metropole', bounds: { x1: 51.03, y1: -5.78, x2: 41.2, y2: 9.70 } },
        { src: '/images/guadeloupe.png', alt: 'Guadeloupe', bounds: { x1: 16.62, y1: -62.03, x2: 15.74, y2: -60.97 } },
        { src: '/images/martinique.png', alt: 'Martinique', bounds: { x1: 14.95, y1: -61.43, x2: 14.28, y2: -60.60 } },
        { src: '/images/mayotte.png', alt: 'Mayotte', bounds: { x1: -12.51, y1: 44.86, x2: -13.19, y2: 45.45 } },
        { src: '/images/la_reunion.png', alt: 'La Réunion', bounds: { x1: -20.65, y1: 54.92, x2: -21.65, y2: 56.15 } },
        { src: '/images/guyane.png', alt: 'Guyane', bounds: { x1: 6.24, y1: -54.62, x2: 1.87, y2: -50.59 } }
      ]
    }
  },
  methods: {
    setMapBounds (bounds) {
      // const map = this.$el.querySelector('#map')
      // console.log(map)
      // map.$emit('boundsUpdated', [bounds.x1, bounds.y1, bounds.x2, bounds.y2])
      // this.bounds = latLngBounds([bounds.x1, bounds.y1], [bounds.x2, bounds.y2])
      this.bounds = [[bounds.x1, bounds.y1], [bounds.x2, bounds.y2]]
    }
  }
}
</script>

<style scoped>
  #map {
    height: 100%;
    width: 100%;
  }
  .advice {
    margin: 1rem 0;
    color: #262626;
    font-weight: 750;
    text-align: center;
    font-size: 0.9rem;
    width: 100%;
  }
  .map-shortcuts .col {
    padding: 12px 6px;
  }
  .map-shortcuts img {
      border: solid 1px #000;
      width: 75px;
      height: 75px;
    }
</style>
