import map from './controllers/map.js';
import mapTiles from './controllers/mapTiles.js';
import mapFeatures from './controllers/mapFeatures.js';

import mapCatalog from './controllers/mapCatalog.js';
import mapCatalogAttribute from './controllers/mapCatalogAttribute.js';
// import rtile from './controllers/rtile.js';
import vtile from './controllers/vtile.js';
import vtile1 from './vtile1.js';
import geojson from './controllers/geojson.js';
import jsonData from './controllers/jsonData.js';
import layerList from './controllers/layerList.js';

import mapFormat from './controllers/mapFormat.js';
import maps from './controllers/maps.js';
import markerIconApi from './controllers/marker_icon.js';

import getMap from './maps/get.map.js';
import addMap from './maps/add.map.js';
import delMap from './maps/del.map.js';

import getWidget from './widgets/get.widget.js';
import addWidget from './widgets/add.widget.js';
import delWidget from './widgets/del.widget.js';

const schemaInfo = {
  type: 'object',
  properties: {
    publicParams: {
      slug: {
        type: 'string',
      },
      z: {
        type: 'number',
      },
      y: {
        type: 'number',
      },
      x: {
        type: 'string',
        pattern: '^\\d+\\.(pbf|vmt)$',
      },
      id: {
        type: 'string',
      },
    },
  },
};

const publicParams = { config: { policy: 'L0' }, schema: schemaInfo, package: 'gis' }; // * L0 === public
const privilegedParams = { config: { policy: 'L1', role: 'admin' }, schema: schemaInfo, package: 'gis' }; // ? just auth or admin

export default async function route(app) {
  if (!app.hasRoute({ method: 'GET', url: '/gis-map/:id?' })) {
    app.get('/gis-map/:id?', publicParams, getMap);
  }
  if (!app.hasRoute({ method: 'POST', url: '/gis-map/:id?' })) {
    app.post('/gis-map/:id?', privilegedParams, addMap);
  }
  if (!app.hasRoute({ method: 'PUT', url: '/gis-map/:id' })) {
    app.put('/gis-map/:id', privilegedParams, addMap);
  }
  if (!app.hasRoute({ method: 'DELETE', url: '/gis-map/:id' })) {
    app.delete('/gis-map/:id', privilegedParams, delMap);
  }

  if (!app.hasRoute({ method: 'GET', url: '/gis-widget/:map/:id?' })) {
    app.get('/gis-widget/:map/:id?', publicParams, getWidget);
  }
  if (!app.hasRoute({ method: 'POST', url: '/gis-widget/:map/:id?' })) {
    app.post('/gis-widget/:map/:id?', privilegedParams, addWidget);
  }
  if (!app.hasRoute({ method: 'PUT', url: '/gis-widget/:map/:id' })) {
    app.put('/gis-widget/:map/:id', privilegedParams, addWidget);
  }
  if (!app.hasRoute({ method: 'DELETE', url: '/gis-widget/:map/:id' })) {
    app.delete('/gis-widget/:map/:id', privilegedParams, delWidget);
  }

  if (!app.hasRoute({ method: 'GET', url: '/maps' })) {
    app.get('/maps', publicParams, maps);
  }
  if (!app.hasRoute({ method: 'GET', url: '/map/:slug' })) {
    app.get('/map/:slug', publicParams, map);
  }
  if (!app.hasRoute({ method: 'GET', url: '/map-tiles/:slug/:z/:y/:x' })) {
    app.get('/map-tiles/:slug/:z/:y/:x', publicParams, mapTiles);
  }
  if (!app.hasRoute({ method: 'GET', url: '/map-features/:slug/:id' })) {
    app.get('/map-features/:slug/:id', publicParams, mapFeatures);
  }
  if (!app.hasRoute({ method: 'GET', url: '/map-features-point/:slug' })) {
    app.get('/map-features-point/:slug', publicParams, mapFeatures);
  }

  if (!app.hasRoute({ method: 'GET', url: '/map-catalog' })) {
    app.get('/map-catalog', publicParams, mapCatalog);
  }
  if (!app.hasRoute({ method: 'GET', url: '/map-catalog/:service/:attr' })) {
    app.get('/map-catalog/:service/:attr', publicParams, mapCatalogAttribute);
  }
  // if (!app.hasRoute({ method: 'GET', url: '/layer-rtile/:id/:z/:y/:x' })) {
  //   app.get('/layer-rtile/:id/:z/:y/:x', publicParams, rtile);
  // }
  if (!app.hasRoute({ method: 'GET', url: '/layer-vtile/:id/:z/:y/:x' })) {
    app.get('/layer-vtile/:id/:z/:y/:x', publicParams, vtile);
  }

  if (!app.hasRoute({ method: 'GET', url: '/api/vtile/:layer/:lang/:z/:y/:x' })) {
    console.log('\x1b[34m%s\x1b[0m', 'add vtile from gis');
    app.get('/vtile/:layer/:lang/:z/:y/:x', publicParams, vtile1);
  }

  if (!app.hasRoute({ method: 'GET', url: '/api/geojson/:layer' })) {
    console.log('\x1b[34m%s\x1b[0m', 'add geojson from gis');
    app.get('/geojson/:layer', publicParams, geojson);
  }

  if (!app.hasRoute({ method: 'GET', url: '/api/json/:layer' })) {
    console.log('\x1b[34m%s\x1b[0m', 'add json from gis');
    app.get('/json/:layer', publicParams, jsonData);
  }

  if (!app.hasRoute({ method: 'GET', url: '/api/gis-layer-list' })) {
    console.log('\x1b[34m%s\x1b[0m', 'add gis-layer-list from gis');
    app.get('/gis-layer-list', publicParams, layerList);
  }

  if (!app.hasRoute({ method: 'GET', url: '/gis-icon/*' })) {
    app.get('/gis-icon/*', publicParams, markerIconApi);
  }
  if (!app.hasRoute({ method: 'GET', url: '/icon/*' })) {
    app.get('/icon/*', publicParams, markerIconApi);
  }
  if (!app.hasRoute({ method: 'GET', url: '/marker-icon/*' })) {
    app.get('/marker-icon/*', publicParams, markerIconApi);
  }

  if (!app.hasRoute({ method: 'GET', url: '/api/map-format' })) {
    app.get('/map-format', publicParams, mapFormat);
  }
}
