const filters = [
  {
    can_id: "123-456",
    interval: 789
  },
  {
    can_id: "1",
    interval: 1000
  },
  {
    can_id: "1000",
    interval: 1
  }
]

const fleets = [
  {
    name: "US-WEST",
    log_level: "info",
    canbus: { enabled: true, data_logger_enabled: true, max_mem_buffer_size: 1, max_disk_buffer_size: 2, filters: filters },
    idps_enabled: true,
    vehicles: ["USWESTVIN12345678", "USWESTVIN12345679", "USWESTVIN12345670"]
  },
  {
    name: "US-CENTRAL",
    log_level: "warning",
    canbus: { enabled: false, data_logger_enabled: false, max_mem_buffer_size: 0, max_disk_buffer_size: 0 },
    idps_enabled: true,
    vehicles: ["USCENTVIN12345678", "USCENTVIN12345679", "USCENTVIN12345670"]
  },
  {
    name: "US-EAST",
    log_level: "error",
    canbus: { enabled: true },
    idps_enabled: true,
    vehicles: ["USEASTVIN12345678", "USEASTVIN12345679", "USEASTVIN12345670"]
  },
];

const vehicles = [
  {
    vin: "USWESTVIN12345678",
    connected: false,
    connectedHMI: false,
    trex_version: "",
  },
  {
    vin: "USWESTVIN12345679",
    connected: true,
    connectedHMI: true,
    trex_version: "",
  },
  {
    vin: "USWESTVIN12345670",
    connected: false,
    connectedHMI: false,
    trex_version: "",
  },
];

const fleetsAPI = {
  addFleet: async (fleet) => {
    fleets.push(fleet);
    return fleet;
  },
  getFleet: async (name) => {
    const index = fleets.findIndex(element => element.name === name);
    return fleets[index];
  },
  getFleets: async () => {
    return { data: fleets };
  },
  updateFleet: async (name, fleet) => {
    const index = fleets.findIndex(element => element.name === name);
    if (index >= 0) fleets[index] = fleet;
    return fleet;
  },
  deleteFleet: async (name) => {
    const index = fleets.findIndex(element => element.name === name);
    if (index >= 0) fleets.splice(index, 1);
    return name;
  },

  getFleetVehicles: async () => {
    return { data: vehicles };
  },
  addFleetVehicles: async (_name, payload) => {
    const index = fleets.findIndex((fleet) => fleet.name === _name);

    if (!payload.vins || index === -1) {
      return;
    }

    vehicles.push(...payload.vins.map((vin => ({
      vin,
      connected: false,
      connectedHMI: false,
      trex_version: "",
    }))));
    fleets[index].vehicles.push(...payload.vins);

    return payload;
  },
  deleteFleetVehicles: async (_name, vins) => {
    const index = fleets.findIndex((fleet) => fleet.name === _name);

    if (!vins || index === -1) {
      return;
    }

    for (let i = 0; i < vins; i++) {
      const vin = vins[i];
      const index = vehicles.findIndex(element => element.vin === vin);
      if (index >= 0) vehicles.splice(index, 1);
      fleets[index].vehicles = fleets[index].vehicles.filter((vin) => !vins.includes(vin))
    }

    return { message: "Deleted" };
  },

  getFleetCANFilters: async () => {
    return { data: filters };
  },
  addFleetCANFilter: async (_name, filter) => {
    filters.push(filter);
    return filter;
  },
  updateFleetCANFilter: async (_name, can_id, filter) => {
    const index = filters.findIndex(element => element.can_id === can_id);
    if (index >= 0) filters[index] = filter;
    return filter;
  },
  deleteFleetCANFilter: async (_name, can_id) => {
    const index = filters.findIndex(element => element.can_id === can_id);
    if (index >= 0) vehicles.splice(index, 1);
    return can_id;
  }
};

export default fleetsAPI;
