import { CryptoCurrency } from "@ledgerhq/ledger-wallet-framework/types";
import type {
  Account,
  AccountRaw,
  CurrencyBridge,
  TransactionCommon,
  TransactionCommonRaw,
  TransactionStatusCommon,
  TransactionStatusCommonRaw,
} from "@ledgerhq/types-live";
import type { BigNumber } from "bignumber.js";
import type { Observable } from "rxjs";
import type { TransferProposal } from "../network/gateway";
import type {
  CantonOnboardProgress,
  CantonOnboardResult,
  CantonAuthorizeProgress,
  CantonAuthorizeResult,
} from "./onboard";

export interface CantonCurrencyBridge extends CurrencyBridge {
  onboardAccount: (
    currency: CryptoCurrency,
    deviceId: string,
    creatableAccount: Account,
  ) => Observable<CantonOnboardProgress | CantonOnboardResult>;
  authorizePreapproval: (
    currency: CryptoCurrency,
    deviceId: string,
    creatableAccount: Account,
    partyId: string,
  ) => Observable<CantonAuthorizeProgress | CantonAuthorizeResult>;
  transferInstruction: (
    currency: CryptoCurrency,
    deviceId: string,
    account: Account,
    partyId: string,
    contractId: string,
    type:
      | "accept-transfer-instruction"
      | "reject-transfer-instruction"
      | "withdraw-transfer-instruction",
    reason?: string,
  ) => Promise<void>;
}

export type NetworkInfo = {
  family: "canton";
  serverFee: BigNumber;
  baseReserve: BigNumber;
};

export type NetworkInfoRaw = {
  family: "canton";
  serverFee: string;
  baseReserve: string;
};

export type Transaction = TransactionCommon & {
  family: "canton";
  fee: BigNumber | null | undefined;
  memo?: string;
  tokenId: string;
  expireInSeconds?: number;
  instrumentAdmin?: string;
};

export type TransactionRaw = TransactionCommonRaw & {
  family: "canton";
  fee: string | null | undefined;
  memo?: string;
  tokenId: string;
  expireInSeconds?: number;
  instrumentAdmin?: string;
};

export enum DurationEnum {
  ONE_DAY = "1d",
  THREE_HOURS = "3h",
  SIX_HOURS = "6h",
  ONE_WEEK = "1w",
  ONE_MONTH = "1m",
}

export type TransactionStatus = TransactionStatusCommon;
export type TransactionStatusRaw = TransactionStatusCommonRaw;

export type CantonResources = {
  isOnboarded: boolean;
  instrumentUtxoCounts: Record<string, number>;
  pendingTransferProposals: TransferProposal[];
  publicKey?: string;
  xpub?: string;
};
export type CantonResourcesRaw = {
  isOnboarded: boolean;
  instrumentUtxoCounts: Record<string, number>;
  pendingTransferProposals: TransferProposal[];
  publicKey?: string;
  xpub?: string;
};

export type CantonAccount = Account & {
  cantonResources: CantonResources;
};
export type CantonAccountRaw = AccountRaw & {
  cantonResources: CantonResourcesRaw;
};
