import { AxiosResponse } from "axios";
import { useHTTP } from "hooks";
import { pond } from "protobuf-ts/pond";
import { useGlobalState } from "providers/StateContainer";
import { createContext, PropsWithChildren, useContext } from "react";
import { or } from "utils";
import { pondURL } from "./pond";

export interface IHarvestPlanAPIContext {
  addHarvestPlan: (harvestPlan: pond.HarvestPlanSettings, otherTeam?: string) => Promise<any>;
  getHarvestPlan: (harvestPlanId: string, otherTeam?: string) => Promise<any>;
  listHarvestPlans: (
    limit: number,
    offset: number,
    order?: "asc" | "desc",
    orderBy?: string,
    search?: string,
    otherTeam?: string,
    asRoot?: boolean
  ) => Promise<AxiosResponse<pond.ListHarvestPlansResponse>>;
  listHistory: (
    id: string,
    limit: number,
    offset: number
  ) => Promise<AxiosResponse<pond.ListHarvestPlanHistoryResponse>>;
  removeHarvestPlan: (
    harvestPlanId: string,
    otherTeam?: string
  ) => Promise<AxiosResponse<pond.RemoveHarvestPlanResponse>>;
  updateHarvestPlan: (
    key: string,
    harvestPlan: pond.HarvestPlanSettings,
    asRoot?: true,
    otherTeam?: string
  ) => Promise<AxiosResponse<pond.UpdateHarvestPlanResponse>>;
}

export const HarvestPlanAPIContext = createContext<IHarvestPlanAPIContext>(
  {} as IHarvestPlanAPIContext
);

interface Props {}

export default function HarvestPlanProvider(props: PropsWithChildren<Props>) {
  const { children } = props;
  const { get, del, post, put } = useHTTP();
  const [{ as }] = useGlobalState();

  const addHarvestPlan = (harvestPlan: pond.HarvestPlanSettings, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view) return post(pondURL("/harvestPlans?as=" + view), harvestPlan);
    return post(pondURL("/harvestPlans"), harvestPlan);
  };

  const getHarvestPlan = (harvestPlanId: string, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view) return get(pondURL("/harvestPlans/" + harvestPlanId + "?as=" + view));
    return get(pondURL("/harvestPlans/" + harvestPlanId));
  };

  const removeHarvestPlan = (key: string, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view)
      return del<pond.RemoveHarvestPlanResponse>(pondURL("/harvestPlans/" + key + "?as=" + view));
    return del<pond.RemoveHarvestPlanResponse>(pondURL("/harvestPlans/" + key));
  };

  const listHarvestPlans = (
    limit: number,
    offset: number,
    order?: "asc" | "desc",
    orderBy?: string,
    search?: string,
    otherTeam?: string,
    asRoot?: boolean
  ) => {
    const view = otherTeam ? otherTeam : as
    return get<pond.ListHarvestPlansResponse>(
      pondURL(
        "/harvestPlans" +
          "?limit=" +
          limit +
          "&offset=" +
          offset +
          ("&order=" + or(order, "asc")) +
          ("&by=" + or(orderBy, "key")) +
          (asRoot ? "&asRoot=" + asRoot.toString() : "") +
          (search ? "&search=" + search : "") +
          (view ? "&as=" + view : "")
      )
    );
  };

  const listHistory = (id: string, limit: number, offset: number) => {
    return get<pond.ListHarvestPlanHistoryResponse>(
      pondURL("/harvestPlans/" + id + "/history?limit=" + limit + "&offset=" + offset)
    );
  };

  const updateHarvestPlan = (
    key: string,
    harvestPlan: pond.HarvestPlanSettings,
    asRoot?: boolean,
    otherTeam?: string
  ) => {
    const view = otherTeam ? otherTeam : as
    if (view)
      return put<pond.UpdateHarvestPlanResponse>(
        pondURL(
          "/harvestPlans/" +
            key +
            (view ? "?as=" + view : "") +
            (asRoot ? "&asRoot=" + asRoot.toString() : "")
        ),
        harvestPlan
      );
    return put<pond.UpdateHarvestPlanResponse>(
      pondURL("/harvestPlans/" + key + (asRoot ? "?asRoot=" + asRoot.toString() : "")),
      harvestPlan
    );
  };

  return (
    <HarvestPlanAPIContext.Provider
      value={{
        addHarvestPlan,
        getHarvestPlan,
        removeHarvestPlan,
        listHarvestPlans,
        listHistory,
        updateHarvestPlan
      }}>
      {children}
    </HarvestPlanAPIContext.Provider>
  );
}

export const useHarvestPlanAPI = () => useContext(HarvestPlanAPIContext);
