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

export interface ILibraCartProxyAPIContext {
  //add new organization
  addAccount: (
    teamKey: string,
    userID: string,
    libracartCode: string,
    libracartAuth: string,
    libracartUsername: string
  ) => Promise<AxiosResponse<pond.AddLibraCartAccountResponse>>;
  //list organizations
  listAccounts: (
    limit: number,
    offset: number,
    as?: string,
    keys?: string[],
    types?: string[]
  ) => Promise<AxiosResponse<pond.ListLibraCartAccountsResponse>>;
  updateAccount: (
    key: string,
    options: pond.DataOption[],
    as?: string
  ) => Promise<AxiosResponse<pond.UpdateLibraCartAccountResponse>>;
  listDestinations: (
    limit: number,
    offset: number,
    libracartKey?: string,
    as?: string
  ) => Promise<AxiosResponse<pond.ListLibraCartDestinationsResponse>>;
  syncData: () => Promise<any>
}

export const LibraCartProxyAPIContext = createContext<ILibraCartProxyAPIContext>(
  {} as ILibraCartProxyAPIContext
);

interface Props {}

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

  const addAccount = (
    teamKey: string,
    userID: string,
    libracartCode: string,
    libracartAuth: string,
    libracartUsername: string
  ) => {
    return post<pond.AddLibraCartAccountResponse>(
      pondURL(
        "/libracartAccounts?team=" +
          teamKey +
          "&user=" +
          userID +
          "&code=" +
          libracartCode +
          "&auth=" +
          libracartAuth +
          "&libracartUsername=" +
          libracartUsername
      )
    );
  };

  const listAccounts = (
    limit: number,
    offset: number,
    as?: string,
    keys?: string[],
    types?: string[]
  ) => {
    return get<pond.ListLibraCartAccountsResponse>(
      pondURL(
        "/libracartAccounts?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.UpdateLibraCartAccountResponse>(
      pondURL(
        "/libracartAccounts/" + key + "?options=" + options.toString() + (as ? "&as=" + as : "")
      )
    );
  };

  const listDestinations = (limit: number, offset: number, libracartKey?: string, as?: string) => {
    return get<pond.ListLibraCartDestinationsResponse>(
      pondURL(
        "/libracartDestinations?limit" + limit + "&offset=" + offset + (libracartKey ? "&libracartKey=" + libracartKey : "") + (as ? "&as=" + as : "")
      )
    )
  }

  const syncData = () => {
    return get(pondURL("/libracartImport" + (as ? "?as=" + as : "")))
  }

  return (
    <LibraCartProxyAPIContext.Provider
      value={{
        addAccount,
        listAccounts,
        updateAccount,
        listDestinations,
        syncData
      }}>
      {children}
    </LibraCartProxyAPIContext.Provider>
  );
}

export const useLibraCartProxyAPI = () => useContext(LibraCartProxyAPIContext);
