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

export interface IJohnDeereProxyAPIContext {
  //add new organization
  addAccount: (
    teamKey: string,
    userID: string,
    jdCode: string,
    jdUserName: string
  ) => Promise<AxiosResponse<pond.AddJDAccountResponse>>;
  //list organizations
  listAccounts: ( 
    limit: number,
    offset: number,
    as?: string,
    keys?: string[],
    types?: string[]
  ) => Promise<AxiosResponse<pond.ListJDAccountsResponse>>;
  updateAccount: (
    key: string,
    options: pond.DataOption[],
    as?: string
  ) => Promise<AxiosResponse<pond.UpdateJDAccountResponse>>;
  //update organizations
  listFields: (
    limit: number,
    offset: number,
    // order?: "asc" | "desc",
    // orderBy?: string,
    // search?: string,
    as?: string,
    asRoot?: boolean
  ) => Promise<AxiosResponse<pond.ListJDFieldsResponse>>;
}

export const JohnDeereProxyAPIContext = createContext<IJohnDeereProxyAPIContext>(
  {} as IJohnDeereProxyAPIContext
);

interface Props {}

export default function JohnDeereProvider(props: PropsWithChildren<Props>) {
  const { children } = props;
  const { post, get, put } = useHTTP();

  const addAccount = (teamKey: string, userID: string, jdCode: string, jdUserName: string) => {
    return post<pond.AddJDAccountResponse>(
      pondURL(
        "/jdAccounts?team=" +
          teamKey +
          "&user=" +
          userID +
          "&code=" +
          jdCode +
          "&jdUserName=" +
          jdUserName
      )
    );
  };

  const listAccounts = (
    limit: number,
    offset: number,
    as?: string,
    keys?: string[],
    types?: string[]
  ) => {
    return get<pond.ListJDAccountsResponse>(
      pondURL(
        "/jdAccounts?limit=" +
          limit +
          "&offset=" +
          offset +
          (as ? "&as=" + as : "") +
          (keys ? "&keys=" + keys.join(",") : "") +
          (types ? "&types=" + types.join(",") : "")
      )
    );
  };

  const updateAccount = (key: string, options: pond.DataOption[], as?: string) => {
    return put<pond.UpdateJDAccountResponse>(
      pondURL("/jdAccounts/" + key + "?options=" + options.toString() + (as ? "&as=" + as : ""))
    );
  };

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

  return (
    <JohnDeereProxyAPIContext.Provider
      value={{
        addAccount,
        listAccounts,
        updateAccount,
        listFields
      }}>
      {children}
    </JohnDeereProxyAPIContext.Provider>
  );
}

export const useJohnDeereProxyAPI = () => useContext(JohnDeereProxyAPIContext);
