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 IHomeMarkerAPIContext {
  addHomeMarker: (
    homeMarker: pond.HomeMarkerSettings,
    otherTeam?: string
  ) => Promise<AxiosResponse<pond.AddHomeMarkerResponse>>;
  getHomeMarker: (homeMarkerID: string) => Promise<AxiosResponse<pond.HomeMarker>>;
  listHomeMarkers: (
    limit: number,
    offset: number,
    order?: "asc" | "desc",
    orderBy?: string,
    search?: string,
    asRoot?: boolean,
    otherTeam?: string
  ) => Promise<AxiosResponse<pond.ListHomeMarkersResponse>>;
  removeHomeMarker: (homeMarkerID: string) => Promise<AxiosResponse<pond.RemoveHomeMarkerResponse>>;
  updateHomeMarker: (
    key: string,
    homeMarker: pond.HomeMarkerSettings,
    asRoot?: true,
    otherTeam?:string
  ) => Promise<AxiosResponse<pond.UpdateHomeMarkerResponse>>;
}

export const HomeMarkersAPIContext = createContext<IHomeMarkerAPIContext>(
  {} as IHomeMarkerAPIContext
);

interface Props {}

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

  const addHomeMarker = (homeMarker: pond.HomeMarkerSettings, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view) return post<pond.AddHomeMarkerResponse>(pondURL("/homeMarkers?as=" + view), homeMarker);
    return post<pond.AddHomeMarkerResponse>(pondURL("/homeMarkers"), homeMarker);
  };

  const getHomeMarker = (homeMarkerId: string) => {
    return get<pond.HomeMarker>(pondURL("/homeMarkers/" + homeMarkerId));
  };

  const removeHomeMarker = (key: string) => {
    return del<pond.RemoveHomeMarkerResponse>(pondURL("/homeMarkers/" + key));
  };

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

  const updateHomeMarker = (key: string, hm: pond.HomeMarkerSettings, asRoot?: boolean, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view)
      return put<pond.UpdateHomeMarkerResponse>(
        pondURL(
          "/homeMarkers/" + key + "?as=" + view + (asRoot ? "&asRoot=" + asRoot.toString() : "")
        ),
        hm
      );
    return put<pond.UpdateHomeMarkerResponse>(
      pondURL("/homeMarkers/" + key + (asRoot ? "?asRoot=" + asRoot.toString() : "")),
      hm
    );
  };

  return (
    <HomeMarkersAPIContext.Provider
      value={{
        addHomeMarker,
        getHomeMarker,
        removeHomeMarker,
        listHomeMarkers,
        updateHomeMarker
      }}>
      {children}
    </HomeMarkersAPIContext.Provider>
  );
}

export const useHomeMarkerAPI = () => useContext(HomeMarkersAPIContext);
