import {UndirectedGraph} from './undirected_graph.js';
import {PositionedVertex, PositionedEdge} from './positioned_graph.js';

/* eslint-disable no-magic-numbers */

const WALK_TYPE = 'walk';
const DRIVE_TYPE = 'drive';

const WALK_SPEED = 5;
const DRIVE_SPEED = 15;

const PLACEHOLDER_MAP = {
  vertices: [
    {
      name: 0,
      position: [100, 0],
    },
    {
      name: 1,
      position: [0, 100],
    },
    {
      name: 2,
      position: [-50, 0],
    },
    {
      name: 3,
      position: [0, -100],
    },
  ],
  edges: [
    {
      type: WALK_TYPE,
      source: 0,
      destination: 1,
      path: [[100, 0], [100, 80], [0, 80], [0, 100]],
      length: 200,
    },
    {
      type: WALK_TYPE,
      source: 1,
      destination: 2,
      path: [[0, 100], [-50, 100], [-50, 0]],
      length: 150,
    },
    {
      type: WALK_TYPE,
      source: 2,
      destination: 3,
      path: [[-50, 0], [-100, 0], [-100, -100], [0, -100]],
      length: 250,
    },
    {
      type: WALK_TYPE,
      source: 3,
      destination: 0,
      path: [[0, -100], [0, -80], [100, -80], [100, 0]],
      length: 200,
    },
    {
      type: WALK_TYPE,
      source: 0,
      destination: 2,
      path: [[100, 0], [50, 0], [50, -25], [-50, -25], [-50, 0]],
      length: 200,
    },
    {
      type: DRIVE_TYPE,
      source: 0,
      destination: 1,
      path: [[100, 0], [100, 80], [0, 80], [0, 100]],
      length: 200,
    },
    {
      type: DRIVE_TYPE,
      source: 1,
      destination: 2,
      path: [[0, 100], [-50, 100], [-50, 0]],
      length: 150,
    },
    {
      type: DRIVE_TYPE,
      source: 2,
      destination: 3,
      path: [[-50, 0], [-100, 0], [-100, -100], [0, -100]],
      length: 250,
    },
    {
      type: DRIVE_TYPE,
      source: 3,
      destination: 0,
      path: [[0, -100], [0, -80], [100, -80], [100, 0]],
      length: 200,
    },
    {
      type: DRIVE_TYPE,
      source: 0,
      destination: 2,
      path: [[100, 0], [50, 0], [50, -25], [-50, -25], [-50, 0]],
      length: 200,
    },
  ],
};

function dataToGraphs(data) {
  const verticesByName = new Map();
  const walkGraph = new UndirectedGraph();
  const driveGraph = new UndirectedGraph();
  for (const description of data.vertices) {
    const vertex = new PositionedVertex(description.name, description.position);
    verticesByName.set(description.name, vertex);
    walkGraph.addVertex(vertex);
    driveGraph.addVertex(vertex);
  }
  for (const description of data.edges) {
    const source = verticesByName.get(description.source);
    const destination = verticesByName.get(description.destination);
    if (description.type === WALK_TYPE && walkGraph.getEdge(source, destination) === undefined) {
      const edge = new PositionedEdge(source, description.path, description.length / WALK_SPEED, destination);
      walkGraph.addEdge(source, edge, destination);
    }
    if (description.type === DRIVE_TYPE && driveGraph.getEdge(source, destination) === undefined) {
      const edge = new PositionedEdge(source, description.path, description.length / DRIVE_SPEED, destination);
      driveGraph.addEdge(source, edge, destination);
    }
  }
  return {
    walkGraph,
    driveGraph,
  };
}

export const PLACEHOLDER_GRAPHS = dataToGraphs(PLACEHOLDER_MAP);

export function loadGraphs(address, radius, success, error) {
  $.ajax({
    url: 'server/city.cgi',
    data: {
      address,
      radius,
    },
    dataType: 'json',
    success: (data) => {
      success(dataToGraphs(data));
    },
    error,
  });
}

export const internals = {
  dataToGraphs,
  WALK_SPEED,
  DRIVE_SPEED,
};