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

export interface IFieldMarkerAPIContext {
  addFieldMarker: (
    fieldMarker: pond.FieldMarkerSettings,
    otherTeam?: string
  ) => Promise<AxiosResponse<pond.AddFieldMarkerResponse>>;
  getFieldMarker: (fieldMarkerID: string, otherTeam?: string) => Promise<AxiosResponse<pond.FieldMarker>>;
  listFieldMarkers: (
    limit: number,
    offset: number,
    order?: "asc" | "desc",
    orderBy?: string,
    search?: string,
    asRoot?: boolean,
    otherTeam?: string
  ) => Promise<AxiosResponse<pond.ListFieldMarkersResponse>>;
  removeFieldMarker: (
    fieldMarkerID: string,
    otherTeam?: string
  ) => Promise<AxiosResponse<pond.RemoveFieldMarkerResponse>>;
  updateFieldMarker: (
    key: string,
    fieldMarker: pond.FieldMarkerSettings,
    asRoot?: true,
    otherTeam?: string
  ) => Promise<AxiosResponse<pond.UpdateFieldMarkerResponse>>;
}

export const FieldMarkersAPIContext = createContext<IFieldMarkerAPIContext>(
  {} as IFieldMarkerAPIContext
);

interface Props {}

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

  const addFieldMarker = (fieldMarker: pond.FieldMarkerSettings, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view)
      return post<pond.AddFieldMarkerResponse>(pondURL("/fieldMarkers?as=" + view), fieldMarker);
    return post<pond.AddFieldMarkerResponse>(pondURL("/fieldMarkers"), fieldMarker);
  };

  const getFieldMarker = (fieldMarkerId: string, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view) return get<pond.FieldMarker>(pondURL("/fieldMarkers/" + fieldMarkerId + "?as=" + view));
    return get<pond.FieldMarker>(pondURL("/fieldMarkers/" + fieldMarkerId));
  };

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

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

  const updateFieldMarker = (key: string, hm: pond.FieldMarkerSettings, asRoot?: boolean, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view)
      return put<pond.UpdateFieldMarkerResponse>(
        pondURL(
          "/fieldMarkers/" + key + "?as=" + view + (asRoot ? "&asRoot=" + asRoot.toString() : "")
        ),
        hm
      );
    return put<pond.UpdateFieldMarkerResponse>(
      pondURL("/fieldMarkers/" + key + (asRoot ? "?asRoot=" + asRoot.toString() : "")),
      hm
    );
  };

  return (
    <FieldMarkersAPIContext.Provider
      value={{
        addFieldMarker,
        getFieldMarker,
        removeFieldMarker,
        listFieldMarkers,
        updateFieldMarker
      }}>
      {children}
    </FieldMarkersAPIContext.Provider>
  );
}

export const useFieldMarkerAPI = () => useContext(FieldMarkersAPIContext);
