# Load default set of map backgrounds
class LoadDefaultMapBackgrounds < ActiveRecord::Migration[4.2]
  MAP_BACKGROUNDS = [
    { name: 'OpenStreetMap Mapnik', url: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', reference_name: 'open_street_map.mapnik', attribution: '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>' },
    { name: 'OpenStreetMap Black and white', url: 'http://{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png', reference_name: 'open_street_map.black_and_white', attribution: '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>' },
    { name: 'OpenStreetMap De', url: 'http://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png', reference_name: 'open_street_map.de', attribution: '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>' },
    { name: 'OpenStreetMap Hot', url: 'http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', reference_name: 'open_street_map.hot', attribution: '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Tiles courtesy of <a href="http://hot.openstreetmap.org/" target="_blank">Humanitarian OpenStreetMap Team</a>', enabled: true, by_default: true },
    { name: 'Thunderforest Outdoors', url: 'https://{s}.tile.thunderforest.com/outdoors/{z}/{x}/{y}.png', reference_name: 'thunderforest.outdoors', attribution: 'Maps © <a href="http://www.thunderforest.com">Thunderforest</a>, Data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>', subdomains: 'abc' },
    { name: 'Thunderforest Cycle', url: 'https://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png', reference_name: 'thunderforest.cycle', attribution: 'Maps © <a href="http://www.thunderforest.com">Thunderforest</a>, Data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>', subdomains: 'abc' },
    { name: 'Thunderforest Transport', url: 'https://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}.png', reference_name: 'thunderforest.transport', attribution: 'Maps © <a href="http://www.thunderforest.com">Thunderforest</a>, Data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>', subdomains: 'abc' },
    { name: 'Thunderforest Transport dark', url: 'https://{s}.tile.thunderforest.com/transport-dark/{z}/{x}/{y}.png', reference_name: 'thunderforest.transport_dark', attribution: 'Maps © <a href="http://www.thunderforest.com">Thunderforest</a>, Data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>', subdomains: 'abc' },
    { name: 'Thunderforest Landscape', url: 'https://{s}.tile.thunderforest.com/landscape/{z}/{x}/{y}.png', reference_name: 'thunderforest.landscape', attribution: 'Maps © <a href="http://www.thunderforest.com">Thunderforest</a>, Data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>', subdomains: 'abc', enabled: true },
    { name: 'OpenMapSurfer Roads', url: 'http://korona.geog.uni-heidelberg.de/tiles/roads/x={x}&y={y}&z={z}', reference_name: 'open_map_surfer.roads', attribution: 'Imagery from <a href="http://giscience.uni-hd.de/">GIScience Research Group @ University of Heidelberg</a> &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>' },
    { name: 'OpenMapSurfer Grayscale', url: 'http://korona.geog.uni-heidelberg.de/tiles/roadsg/x={x}&y={y}&z={z}', reference_name: 'open_map_surfer.grayscale', attribution: 'Imagery from <a href="http://giscience.uni-hd.de/">GIScience Research Group @ University of Heidelberg</a> &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>' },
    { name: 'MapQuestOpen', url: 'http://otile{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.jpg', reference_name: 'map_quest_open.default', attribution: 'Tiles Courtesy of <a href="http://www.mapquest.com/">MapQuest</a> &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>', subdomains: '1234' },
    { name: 'MapQuestOpen Aerial', url: 'http://otile{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg', reference_name: 'map_quest_open.aerial', attribution: 'Tiles Courtesy of <a href="http://www.mapquest.com/">MapQuest</a> &mdash; Portions Courtesy NASA/JPL-Caltech and U.S. Depart. of Agriculture, Farm Service Agency', subdomains: '1234', max_zoom: 11 },
    { name: 'Stamen Toner', url: 'http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png', reference_name: 'stamen.toner', attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>', subdomains: 'abcd', min_zoom: 0, max_zoom: 17 },
    { name: 'Stamen Toner background', url: 'http://{s}.tile.stamen.com/toner-background/{z}/{x}/{y}.png', reference_name: 'stamen.toner_background', attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>', subdomains: 'abcd', min_zoom: 0, max_zoom: 17 },
    { name: 'Stamen Toner lite', url: 'http://{s}.tile.stamen.com/toner-lite/{z}/{x}/{y}.png', reference_name: 'stamen.toner_lite', attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>', subdomains: 'abcd', min_zoom: 0, max_zoom: 17 },
    { name: 'Stamen Watercolor', url: 'http://{s}.tile.stamen.com/watercolor/{z}/{x}/{y}.jpg', reference_name: 'stamen.watercolor', attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>', subdomains: 'abcd', min_zoom: 3, max_zoom: 15, enabled: true },
    { name: 'Esri World street map', url: 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', reference_name: 'esri.world_street_map', attribution: 'Tiles &copy; Esri &mdash; Source: Esri, DeLorme, NAVTEQ, USGS, Intermap, iPC, NRCAN, Esri Japan, METI, Esri China (Hong Kong), Esri (Thailand), TomTom, 2012' },
    { name: 'Esri De lorme', url: 'http://server.arcgisonline.com/ArcGIS/rest/services/Specialty/DeLorme_World_Base_Map/MapServer/tile/{z}/{y}/{x}', reference_name: 'esri.de_lorme', attribution: 'Tiles &copy; Esri &mdash; Copyright: &copy;2012 DeLorme', min_zoom: 1, max_zoom: 11 },
    { name: 'Esri World topo map', url: 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}', reference_name: 'esri.world_topo_map', attribution: 'Tiles &copy; Esri &mdash; Esri, DeLorme, NAVTEQ, TomTom, Intermap, iPC, USGS, FAO, NPS, NRCAN, GeoBase, Kadaster NL, Ordnance Survey, Esri Japan, METI, Esri China (Hong Kong), and the GIS User Community' },
    { name: 'Esri World imagery', url: 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', reference_name: 'esri.world_imagery', attribution: 'Tiles &copy; Esri &mdash; Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community', enabled: true },
    { name: 'Esri Nat geo world map', url: 'http://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer/tile/{z}/{y}/{x}', reference_name: 'esri.nat_geo_world_map', attribution: 'Tiles &copy; Esri &mdash; National Geographic, Esri, DeLorme, NAVTEQ, UNEP-WCMC, USGS, NASA, ESA, METI, NRCAN, GEBCO, NOAA, iPC', max_zoom: 12 },
    { name: 'Esri World gray canvas', url: 'http://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}', reference_name: 'esri.world_gray_canvas', attribution: 'Tiles &copy; Esri &mdash; Esri, DeLorme, NAVTEQ', max_zoom: 16 }
  ].freeze

  def change
    reference_names = select_values('SELECT reference_name FROM map_backgrounds').uniq
    MAP_BACKGROUNDS.each do |background|
      next if reference_names.include?(background[:reference_name])
      execute 'INSERT INTO map_backgrounds (' + background.keys.join(', ') +
              ', created_at, updated_at) SELECT ' +
              background.values.map { |v| quote(v) }.join(', ') +
              ', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP;'
    end
  end
end
