import { FC, ReactNode } from 'react';
import { BigNumber } from '@polymathnetwork/polymesh-sdk';
import {
  TransactionStatus,
  TransactionQueue,
} from '@polymathnetwork/polymesh-sdk/types';
import { EventRecord } from '@polkadot/types/interfaces';
/* eslint-disable import/no-extraneous-dependencies */
import { AugmentedSubmittable } from '@polkadot/api/types/submittable';
import { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
/* eslint-enable */

export enum AppStaticRoute {
  splashscreen = '/',
  extension = '/extension',
  wrongNetwork = '/wrong-network',
  uIdMissing = '/uid-missing',
  // notFound = '/not-found', // TODO: Detect 404 before API init
}

export interface StaticRoutes {
  [path: string]: FC<any>;
}

export type AppBaseReduxConfig = {
  appStateActions: {
    setSdk: any;
    setSelectedAccount: any;
    setDid: any;
    setHasUid: any;
    setIsITN: any;
    setPolyWallet: any;
    setChainProperties: any;
    [key: string]: any;
  };
  provider: any;
  store: any;
};

export interface ApiConfig {
  polyWssNodeUrl: string;
  graphqlEndpoint: string;
  graphqlApiKey: string;
  masqueradeEnv: string;
  isProdEnv: boolean;
  rpcUrl: string;
}

export interface ConfirmationData {
  title: string;
  titleArgs?: Record<string, string | number>;
  identity?: boolean;
  sections?: {
    label?: string;
    items: {
      label: string;
      value: string | ReactNode | null;
      link?: string;
      hr?: boolean;
      translate?: boolean;
    }[];
  }[];
  headerComponent?: ReactNode;
  footerComponent?: ReactNode;
  message?: string;
  messageArgs?: Record<string, string | number>;
  submitButtonTitle?: string;
  apiProtocolFee?: BigNumber;
}

export enum ConfirmationStatus {
  None,
  Pending,
  Done,
}

export enum TransactionQueueResult {
  Success,
  Failure,
  Partial,
}

export enum ApiQueueItemType {
  Batch,
  Single,
}

export type PolymeshTx<Args extends unknown[]> = AugmentedSubmittable<
  (...args: Args) => SubmittableExtrinsic<'promise'>
>;

export interface ApiQueueItem<Args extends unknown[]> {
  method: PolymeshTx<Args>;
  params: Args;
  blocking?: boolean;
  txHash?: string;
  status?: TransactionStatus;
  dataResolver?: (events: EventRecord[]) => Record<string, any>;
  type?: ApiQueueItemType;
}

export interface SigningPackage {
  id: string;
  queue?: TransactionQueue;
  apiQueue?: ApiQueueItem<any>[];
  confirmationData?: ConfirmationData;
  confirmationStatus?: ConfirmationStatus;
  onCompleted?: (
    result: TransactionQueueResult,
    transactions: any // PolymeshTransaction<unknown[], unknown[]>[]
  ) => void;
  failUrl?: string;
}
