import React from "react";

let busy = false;

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

let vehicle = {
  vin: "3C4PDCBG0ET127145",
  year: 2021,
  model: "Ocean",
  trim: "Basic",
  ecu_list: "ECUA 2.0.0, ECUB 2.1.1",
  log_level: "info",
  canbus: {
    enabled: true,
    data_logger_enabled: true,
    max_mem_buffer_size: 1,
    max_disk_buffer_size: 2,
    filters: filters,
  },
};
let vehicleState = {
  data: {
    online: false,
    online_hmi: true,
    battery: {
      percent: 95,
    },
    max_range: {
      max_miles: 577,
    },
    doors: {
      hood: false,
      left_front: false,
      left_rear: false,
      right_front: false,
      right_rear: false,
      trunk: false,
    },
    location: {
      altitude: 17,
      longitude: -122.414,
      latitude: 37.764,
    },
    door_locks: {
      driver: false,
      all: false,
    },
    sunroof: {
      sunroof: 0,
    },
    cabin_climate: {
      cabin_temperature: 0,
      internal_temperature: 29,
    },
    ambient_temperature: {
      temperature: 26,
    },
    trex_version: "1000000",
    updated: "2022-07-26T00:26:38.880381Z",
  },
};

let vehicles = [];
let models = ["Ocean", "PEAR"];
let years = [2023, 2024];
let totalVehicles = 0;
let fleets = ["fleet1", "fleet2"];
let totalFleets = 2;
let error = null;

export const VehicleProvider = ({ children }) => {
  return <div data-testid="mocked-vehicleprovider">{children}</div>;
};

export const useVehicleContext = () => ({
  busy,
  models,
  fleets,
  totalFleets,
  totalVehicles,
  vehicle,
  vehicles,
  years,
  addVehicle: jest.fn(),
  getConnections: jest.fn((vins, _token) => {
    const result = {};

    vins.forEach((vin) => {
      result[vin] = true;
    });

    return result;
  }),
  getECUs: jest.fn(() => {
    return {
      data: [
        {
          config: "CONFIG",
          created: "2021-07-14T20:09:40.98187Z",
          ecu: "ECUA",
          hw_version: "HWVERSION",
          sw_version: "SWVERSION",
          updated: "2021-07-14T20:09:40.98187Z",
        },
        {
          config: "CONFIG",
          created: "2021-07-14T20:09:40.98187Z",
          hw_version: "HWVERSION",
          sw_version: "SWVERSION",
          updated: "2021-07-14T20:09:40.98187Z",
        },
      ],
      total: 2,
    };
  }),
  getLocations: jest
    .fn()
    .mockResolvedValue([
      { altitude: 5, longitude: 10, latitude: 15, vin: "TESTVIN123" },
    ]),
  getModels: jest.fn(() => {
    models = ["Ocean", "PEAR"];
  }),
  getState: jest.fn(() => vehicleState),
  getYears: jest.fn(() => {
    years = [2023, 2024];
  }),
  getVehicle: jest.fn(),
  getVehicles: jest.fn(() => vehicles),
  sendCommand: jest.fn((vins, command, parameters, _token) => ({
    vins,
    command,
    parameters,
  })),
  getFleets: jest.fn((vin, search,_token) => {return {
    data: ["fleet1", "fleet2"],
    total: 2,
  }}),
});

export const setBusy = (val) => {
  busy = val;
};

export const setVehicles = (val) => {
  vehicles = val;
};
