import React, { useContext, useState } from "react";

import api from "../../services/updates";

const UpdatesContext = React.createContext();

export const UpdatesProvider = ({ children }) => {
  const [busy, setBusy] = useState(false);
  const [packages, setPackages] = useState([]);
  const [carUpdates, setCarUpdates] = useState([]);
  const [totalPackages, setTotalPackages] = useState(0);
  const [totalCarUpdates, setTotalCarUpdates] = useState(0);

  const getPackages = async (search, token) => {
    let result;

    try {
      setBusy(true);
      result = await api.getPackages(search, token);
      if (result.error)
        throw new Error(`Get packages error. ${result.message}`);
      setPackages(result.data);
      if (search && search.offset === 0 && result.total) {
        setTotalPackages(result.total);
      }
    } finally {
      setBusy(false);
    }

    return result;
  };

  const updatePackage = async (data, token) => {
    let result = null;

    try {
      setBusy(true);
      validateUpdatePackage(data);
      result = await api.updatePackage(data, token);
      if (result.error)
        throw new Error(`Update package error. ${result.message}`);
    } finally {
      setBusy(false);
    }

    return result;
  };

  const createCarUpdates = async (data, token) => {
    let result;

    try {
      setBusy(true);
      validateCreateCarUpdates(data);
      result = await api.createCarUpdates(data, token);
      if (result.error)
        throw new Error(`Create car updates error. ${result.message}`);
    } finally {
      setBusy(false);
    }

    return result;
  };

  const getCarUpdates = async (search, token) => {
    let result;

    try {
      setBusy(true);
      result = await api.getCarUpdates(search, token);
      if (result.error)
        throw new Error(`Get packages error. ${result.message}`);
      setCarUpdates(result.data);
      if (search && search.offset === 0 && result.total) {
        setTotalCarUpdates(result.total);
      }
    } finally {
      setBusy(false);
    }

    return result;
  };

  return (
    <UpdatesContext.Provider
      value={{
        busy,
        packages,
        totalPackages,
        carUpdates,
        totalCarUpdates,
        getPackages,
        updatePackage,
        createCarUpdates,
        getCarUpdates,
      }}
    >
      {children}
    </UpdatesContext.Provider>
  );
};

export const useUpdatesContext = () => useContext(UpdatesContext);

const validateUpdatePackage = (data) => {
  if (data === null) {
    throw new Error("No update data");
  }

  if (!data.package_name) {
    throw new Error("Package name required");
  }

  if (!data.version) {
    throw new Error("Version required");
  }
};

const validateCreateCarUpdates = (data) => {
  if (data === null) {
    throw new Error("No car update data");
  }

  if (!data.package_id || data.package_id === 0) {
    throw new Error("Package id required");
  }

  if (!data.car_ids || data.car_ids.length === 0) {
    throw new Error("Car ids required");
  }
};
