import SignetRequestController from '@/api/requests';
import {
  AuthenticationRequest,
  Bonus,
  CloseRewardFundRequest,
  ContributeRequest,
  CreateQueueRequest,
  CreateQueueResponse,
  CreateRewardFundRequest,
  GetBalanceRequest,
  GetBalanceResponse,
  GetContributionsRequest,
  GetContributionsResponse,
  GetQueueMembersRequest,
  GetQueueMembersResponse,
  GetQueuesResponse,
  GetRewardFundRequest,
  GetRewardFundResponse,
  GetRewardFundsRequest,
  GetRewardFundsResponse,
  LoginResponse,
  SuccessResponse,
} from '@/api/types';

const controller = new SignetRequestController();

export const register = (username: string, password: string) => controller.post<SuccessResponse, AuthenticationRequest>('Register', {
  username,
  password,
});

export const login = (username: string, password: string) => controller.post<LoginResponse, AuthenticationRequest>('Login', {
  username,
  password,
});

export const getRewardFunds = (offset: number) => controller.post<GetRewardFundsResponse, GetRewardFundsRequest>('GetRewardFunds', { offset });

export const getQueues = () => controller.post<GetQueuesResponse, null>('GetQueues', null);

export const getQueueMembers = (id: number) => controller.post<GetQueueMembersResponse, GetQueueMembersRequest>('GetQueueMembers', { id });

export const createQueue = (name: string) => controller.post<CreateQueueResponse, CreateQueueRequest>('CreateQueue', { name });

export const createRewardFund = (
  asset: string,
  fundWallet: string,
  sellingWallet: string,
  issuerWallet: string,
  memo: string,
  minContribution: number,
  title: string,
  description: string,
  bonuses: Bonus[],
  queueID?: number | null | undefined,
) => controller.post<SuccessResponse, CreateRewardFundRequest>('CreateRewardFund', {
  asset,
  fundWallet,
  sellingWallet,
  issuerWallet,
  memo,
  minContribution,
  title,
  description,
  bonuses,
  queueID,
});

export const deleteRewardFund = (id: number, close: boolean) => controller.post<
  SuccessResponse, CloseRewardFundRequest
>(
  'CloseRewardFund',
  {
    id,
    close,
  },
);

export const getRewardFund = (id: number, consolidateContributions: boolean) => controller.post<GetRewardFundResponse, GetRewardFundRequest>('GetRewardFund', {
  id,
  consolidateContributions,
});

export const getContributions = (
  id: number,
  offset: number,
  forDate: string | undefined,
  consolidateContributions: boolean,
) => controller.post<GetContributionsResponse, GetContributionsRequest>(
  'GetContributions',
  {
    id,
    offset,
    forDate,
    consolidateContributions,
  },
);

export const getBalance = (secretKey: string) => controller.post<GetBalanceResponse, GetBalanceRequest>('GetBalance', { secretKey });

export const contribute = (privateKey: string, amount: number, rewardFund: number) => controller.post<SuccessResponse, ContributeRequest>('Contribute', {
  privateKey,
  amount,
  rewardFund,
});
