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

export interface IFieldAPIContext {
  addField: (field: pond.FieldSettings, otherTeam?: string) => Promise<any>;
  getField: (fieldId: string, otherTeam?: string) => Promise<any>;
  listFields: (
    limit: number,
    offset: number,
    order?: "asc" | "desc",
    orderBy?: string,
    search?: string,
    otherTeam?: string,
    asRoot?: boolean
  ) => Promise<AxiosResponse<pond.ListFieldsResponse>>;
  removeField: (key: string, otherTeam?: string) => Promise<AxiosResponse<pond.RemoveFieldResponse>>;
  updateField: (
    key: string,
    field: pond.FieldSettings,
    asRoot?: true,
    otherTeam?: string
  ) => Promise<AxiosResponse<pond.UpdateSiteResponse>>;
}

export const FieldAPIContext = createContext<IFieldAPIContext>({} as IFieldAPIContext);

interface Props {}

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

  const addField = (field: pond.FieldSettings, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view) return post<pond.AddFieldResponse>(pondURL("/fields?as=" + view), field);
    return post(pondURL("/fields"), field);
  };

  const getField = (fieldId: string, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view) return get(pondURL("/field/" + fieldId + "?as=" + view));
    return get(pondURL("/field/" + fieldId));
  };

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

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

  const updateField = (key: string, field: pond.FieldSettings, asRoot?: boolean, otherTeam?: string) => {
    const view = otherTeam ? otherTeam : as
    if (view)
      return put<pond.UpdateFieldResponse>(
        pondURL("/fields/" + key + "?as=" + view + (asRoot ? "&asRoot=" + asRoot.toString() : "")),
        field
      );
    return put<pond.UpdateFieldResponse>(
      pondURL("/fields/" + key + (asRoot ? "?asRoot=" + asRoot.toString() : "")),
      field
    );
  };

  return (
    <FieldAPIContext.Provider
      value={{
        addField,
        getField,
        listFields,
        removeField,
        updateField
      }}>
      {children}
    </FieldAPIContext.Provider>
  );
}

export const useFieldAPI = () => useContext(FieldAPIContext);
