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,
  },
  idps_enabled: true,
};
let vehicleState = {
  data: {
    online: false,
    online_hmi: true,
    battery: {
      total_mileage_odometer: 12000,
      percent: 95,
      battery_voltage: 12.5,
    },
    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",
    dbc_version: "d439abd3662dd20099f49dd8f43f7b145202e961caa2b5aba2c6154c8096348b",
    ip: "172.20.0.17:49850",
    updated: "2022-07-26T00:26:38.880381Z",
    vehicle_speed: {
      speed: 77.7,
    },
    gear: {
      in_park: true,
    },
    safe_state: {
      vehicle_safe_state: false,
      vcu_safe_state: true,
      mcu_front_safe_state: false,
      mcu_rear_safe_state: true,
      mcu_rear_decoup_state: false,
      mcu_front_inverter_error: true,
      mcu_rear_inverter_error: false,
    }
  },
};

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 VehicleConsumer = ({ children }) => {
  return children();
};

export const useVehicleContext = () => ({
  busy,
  models,
  fleets,
  totalFleets,
  totalVehicles,
  vehicle,
  vehicles,
  years,
  addVehicle: jest.fn(),
  getConnections: jest
    .fn().mockImplementation((vins, _token) => {
      const result = {
        "USWESTVIN12345678": true,
        "2:USWESTVIN12345678": false,
        "USWESTVIN12345679": true,
        "2:USWESTVIN12345679": false,
        "USWESTVIN12345670": true,
        "2:USWESTVIN12345670": false,
      };
      return Promise.resolve(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({
      // tests only pass without mocking the data here
      // data: [{ altitude: 5, longitude: 10, latitude: 15, vin: "TESTVIN123" }]
    }),
  getLocationsVehiclePaths: jest
    .fn()
    .mockResolvedValue({
      // tests only pass without mocking the data here
      // '3FAFP13P31R199430': [[16.891136999999986, 26.832352999999955], [56.891136999999986, 66.832352999999955], [26.891136999999986, 36.832352999999955]],
      // '3FAFP13P71R199060': [[36.891136999999986, 46.832352999999955], [76.891136999999986, 16.832352999999955]],
      // '3FAFP13P61R199390': [],
    }),
  getModels: jest.fn(() => {
    models = ["Ocean", "PEAR"];
  }),
  getState: jest.fn().mockResolvedValue(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,
    }
  }),
  getAllFlashpacks: jest.fn((options, token) => {
    return {
      data: [
        {
          flashpack: "44.4",
          car_model: "Ocean",
          car_year: "2023",
        },
        {
          flashpack: "38.4",
          car_model: "Ocean",
          car_year: "2023",
        },
      ],
      total: 2,
    }
  }),
  getFlashpackECUMappings: jest.fn((model, year, flashpack, options, token) => {
    return {
      data: [
        {
          flashpack: "44.4",
          car_model: "Ocean",
          car_year: "2023",
          car_ecu_name: "ADAS",
          car_ecu_version: "99",
        },
        {
          flashpack: "44.4",
          car_model: "Ocean",
          car_year: "2023",
          car_ecu_name: "PDI",
          car_ecu_version: "11",
        },
      ],
      total: 2,
    }
  }),
});

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

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