import { get, patch, post } from "@src/api";
import { ESNSType } from "@src/constants/Login";
import { IFilter } from "metarare";
import { EItemType, ECurrencyType, ESaleType } from "@src/constants/Enums";
import { isDev, isLocal, LOCAL_ENDPOINT } from "@src/constants/NodeEnv";

const getHost = () => {
  if (isLocal) {
    return LOCAL_ENDPOINT;
  } else if (isDev) {
    return "https://api.pepemomy.com";
  } else {
    return "https://api.meta-rare.net";
  }
};

const host = getHost();

// collection
const addCollection = (formData: FormData) => {
  const url = `${host}/v1/collection`;

  return post({ url, body: formData });
};

const updateCollection = (formData: FormData) => {
  const url = `${host}/v1/collection`;

  return patch({ url, body: formData });
};

const checkCollectionDuplicate = (name: string) => {
  const url = `${host}/v1/collection/duplicate/${name}`;

  return get({ url });
};

const fetchCollectionActivity = (name: string) => {
  const url = `${host}/v1/collection/activity/${name}`;

  return post({ url });
};

const fetchCollectionItem = ({
  name,
  filter
}: {
  name: string;
  filter: IFilter;
}) => {
  const url = `${host}/v1/collection/item/${name}`;

  return post({ url, body: filter });
};

const fetchCollection = (name: string) => {
  const url = `${host}/v1/collection/${name}`;

  return get({ url });
};

// explore
const fetchExploreHome = (filter: IFilter) => {
  const url = `${host}/v1/explore/home`;

  return post({ url, body: filter });
};

const fetchExploreSearch = (filter: IFilter) => {
  const url = `${host}/v1/explore/search`;

  return post({ url, body: filter });
};

const fetchExploreHint = (filter: IFilter) => {
  const url = `${host}/v1/explore/hint`;

  return post({ url, body: filter });
};

const fetchExploreKeyword = (collectionName: string) => {
  const url = `${host}/v1/explore/keyword`;

  return post({ url, body: { collection_name: collectionName, limit: 50 } });
};

// home
const fetchHome = () => {
  const url = `${host}/v1/home`;

  return get({ url });
};

const fetchHomeArtists = (filter: IFilter) => {
  const url = `${host}/v1/home/artist`;

  return get({ url, params: filter });
};

// sale
const saleHighestPrice = ({
  bid_price,
  currency,
  sale_id
}: {
  bid_price: number;
  currency: ECurrencyType;
  sale_id: number;
}) => {
  const url = `${host}/v1/sale/auction`;

  return post({ url, body: { bid_price, currency, sale_id } });
};

const saleBid = ({
  bid_price,
  currency,
  sale_id
}: {
  bid_price: number;
  currency: ECurrencyType;
  sale_id: number;
}) => {
  const url = `${host}/v1/sale/bid`;

  return post({ url, body: { bid_price, currency, sale_id } });
};

const cancelBid = (id: number) => {
  const url = `${host}/v1/sale/bid`;

  return patch({ url, body: { id } });
};

const fetchBid = (id: number) => {
  const url = `${host}/v1/sale/bid/${id}`;

  return get({ url });
};

const saleBuy = ({
  bid_price,
  currency,
  sale_id
}: {
  bid_price: number;
  currency: ECurrencyType;
  sale_id: number;
}) => {
  const url = `${host}/v1/sale/buy`;

  return post({ url, body: { bid_price, currency, sale_id } });
};

const resale = ({
  currency,
  end_at,
  price,
  sale_type,
  start_at,
  start_price,
  token_id
}: {
  currency: ECurrencyType;
  end_at?: string;
  price?: number;
  sale_type: ESaleType;
  start_at?: string;
  start_price?: number;
  token_id: number;
}) => {
  const url = `${host}/v1/sale/resale`;

  return post({
    url,
    body: {
      currency,
      end_at,
      price,
      sale_type,
      start_at,
      start_price,
      token_id
    }
  });
};

const changeSaleStatus = (id: number) => {
  const url = `${host}/v1/sale/status`;

  return patch({ url, body: { id } });
};

const fetchSale = (id: number) => {
  const url = `${host}/v1/sale/${id}`;

  return get({ url });
};

const fetchSaleRanking = (filter: IFilter) => {
  const url = `${host}/v1/sale/ranking`;

  return get({ url, params: filter });
};

// token
const fetchToken = (id: number) => {
  const url = `${host}/v1/token/${id}`;

  return get({ url });
};

const addToken = (formData: FormData) => {
  const url = `${host}/v1/token`;

  return post({ url, body: formData });
};

const fetchBaseInfo = (type: EItemType) => {
  const url = `${host}/v1/token/baseinfo?type=${type}`;

  return get({ url });
};

const changeLike = ({
  tokenId,
  isLike
}: {
  tokenId: number;
  isLike: boolean;
}) => {
  const url = `${host}/v1/token/like`;

  return patch({ url, body: { token_id: tokenId, is_like: isLike } });
};

const fetchLog = (id: number) => {
  const url = `${host}/v1/token/log/${id}`;

  return get({ url });
};

const fetchOwner = (id: string) => {
  const url = `${host}/v1/token/owner?token_uid=${id}`;

  return get({ url });
};

const fetchResaleInfo = (id: number) => {
  const url = `${host}/v1/token/resaleinfo/${id}`;

  return get({ url });
};

// user
const fetchUserActivity = (name: string) => {
  const url = `${host}/v1/user/activity/${name}`;

  return get({ url });
};

const fetchUserBid = (filter: IFilter) => {
  const url = `${host}/v1/user/bid`;

  return post({ url, body: filter });
};

const fetchUserCollection = ({
  name,
  filter
}: {
  name: string;
  filter: IFilter;
}) => {
  const url = `${host}/v1/user/collection/${name}`;

  return post({ url, body: filter });
};

const checkUserDuplicateName = (name: string) => {
  const url = `${host}/v1/user/duplicate/${name}`;

  return get({ url });
};

const fetchUserLike = ({ name, filter }: { name: string; filter: IFilter }) => {
  const url = `${host}/v1/user/like/${name}`;

  return post({ url, body: filter });
};

const fetchUserOwned = ({
  name,
  filter
}: {
  name: string;
  filter: IFilter;
}) => {
  const url = `${host}/v1/user/owned/${name}`;

  return post({ url, body: filter });
};

const fetchUserOnSale = ({
  name,
  filter
}: {
  name: string;
  filter: IFilter;
}) => {
  const url = `${host}/v1/user/onsale/${name}`;

  return post({ url, body: filter });
};

const fetchUserProfile = (name: string) => {
  const url = `${host}/v1/user/profile/${name}`;

  return get({ url });
};

const fetchUserInfo = () => {
  const url = `${host}/v1/user/profile`;

  return get({ url });
};

const changeUserProfile = (formData: FormData) => {
  const url = `${host}/v1/user/profile`;

  return patch({ url, body: formData });
};

const transfer = ({
  amount,
  currency,
  to_address,
  code
}: {
  amount: number;
  currency: ECurrencyType;
  to_address: string;
  code: string;
}) => {
  const url = `${host}/v1/user/transfer`;

  return post({ url, body: { amount, currency, to_address, code } });
};

const fetchSNSRedirectUrl = (type: ESNSType) => {
  const url = `${host}/v1/user/redirect/url?type=${type}`;

  return get({ url });
};

const signIn = (code: string, type: ESNSType) => {
  const url = `${host}/v1/user/authenticate`;

  return post({ url, body: { code, type } });
};

const sendEmail = ({
  amount,
  currency
}: {
  amount: number;
  currency: ECurrencyType;
}) => {
  const url = `${host}/v1/user/certification/code`;

  return post({ url, body: { amount, currency } });
};

const APIs = {
  addCollection,
  updateCollection,
  checkCollectionDuplicate,
  fetchCollectionActivity,
  fetchCollectionItem,
  fetchCollection,
  fetchExploreHome,
  fetchExploreSearch,
  fetchExploreKeyword,
  fetchExploreHint,
  fetchHome,
  fetchHomeArtists,
  saleHighestPrice,
  saleBid,
  cancelBid,
  saleBuy,
  resale,
  changeSaleStatus,
  fetchSale,
  fetchSaleRanking,
  fetchBid,
  fetchToken,
  addToken,
  fetchBaseInfo,
  changeLike,
  fetchLog,
  fetchOwner,
  fetchResaleInfo,
  fetchUserActivity,
  fetchUserBid,
  fetchUserCollection,
  fetchUserLike,
  checkUserDuplicateName,
  fetchUserOwned,
  fetchUserOnSale,
  fetchUserProfile,
  fetchUserInfo,
  changeUserProfile,
  transfer,
  fetchSNSRedirectUrl,
  signIn,
  sendEmail
};

export default APIs;
