import type { Axios } from 'axios';
import type { UserPublicInfo } from '@blocklet/js-sdk';
import { OAUTH_PROVIDER } from '@arcblock/ux/lib/Util/constant';
import type { CloseConnect, OpenConnect } from '@arcblock/did-connect-react/lib/types';

export type SessionContext = {
  session: Session;
  api: Axios;
  connectApi: {
    open: OpenConnect;
    close: CloseConnect;
  };
};

export type OAuthAccount = {
  provider: keyof typeof OAUTH_PROVIDER;
  id: string;
  did: string;
  pk: string;
  lastLoginAt: string;
  firstLoginAt: string;
  order?: number;
  userInfo?: {
    email: string;
    emailVerified: boolean;
    name: string;
    picture: string;
    sub: string;
    extraData: Object;
  };
};

export type WalletAccount = {
  provider: 'wallet';
  did: string;
  pk: string;
  lastLoginAt: string;
  firstLoginAt: string;
};

export type NFTAccount = {
  provider: 'nft';
  did: string;
  pk: string;
  owner: string;
  lastLoginAt: string;
  firstLoginAt: string;
};

export type ConnectedAccount = OAuthAccount | WalletAccount | NFTAccount;

export type UserMetadataLink = {
  url: string;
  favicon?: string;
};

export enum DurationEnum {
  NoClear = 'no_clear',
  ThirtyMinutes = '30_minutes',
  OneHour = '1_hour',
  FourHours = '4_hours',
  Today = 'today',
  ThisWeek = 'this_week',
  Custom = 'custom',
}

export enum StatusEnum {
  Meeting = 'meeting',
  Community = 'community',
  Holiday = 'holiday',
  OffSick = 'off_sick',
  WorkingRemotely = 'working_remotely',
}

export type UserPhoneProps = {
  country: string;
  phoneNumber?: string;
};

export type UserMetadata = {
  bio?: string;
  location?: string;
  timezone?: string;
  joinedAt?: string;
  status?: {
    value: string;
    duration?: DurationEnum;
    dateRange?: Date[];
  };
  links?: UserMetadataLink[];
  cover?: string;
  // 这两个字段是 User, 方便数据更新，在保存时同步
  email?: string;
  phone?: UserPhoneProps;
};

export type UserAddress = {
  country?: string;
  province?: string;
  city?: string;
  line1?: string;
  line2?: string;
  postalCode?: string;
};

export type User = UserPublicInfo & {
  role: string;
  email?: string;
  phone?: string;
  sourceProvider?: string;
  sourceAppPid?: string;
  lastLoginAt?: string;
  lastLoginIp?: string;
  createdAt?: string;
  passports?: any[];
  didSpace?: Record<string, any>;
  connectedAccounts?: any[];
  locale?: string;
  url: string;
  inviter?: string;
  emailVerified?: boolean;
  phoneVerified?: boolean;
  // 1.16.40 新增
  metadata?: UserMetadata;
  // 1.16.41 新增
  address?: UserAddress;
};

export type UserCenterTab = {
  value: string;
  label: string;
  url: string;
  protected: boolean | string;
  icon?: string;
  isPrivate?: boolean; // 如果为 true 则不应该出现在隐私设置中
};

export type Session = {
  loading: boolean;
  initialized: boolean;
  user?: User;
  login: any;
  logout: any;
  switch: any;
  switchDid: any;

  refreshProfile: () => Promise<void>;
  switchProfile: any;
  switchPassport: any;

  refresh: Function;
  useOAuth: Function;
  usePasskey: Function;
  withSecondaryAuth: Function;
};

export type WebhookType = 'slack' | 'api';
export type WebhookItemData = {
  type: WebhookType;
  url: string;
};

export type WebhookItemProps = {
  onTest: (params: WebhookItemData) => void;
  onSave: (params: WebhookItemData) => void;
  onDelete?: (params: WebhookItemData) => void;
  onCancel?: () => void;
  edit: boolean;
  type?: WebhookType;
  url?: string;
};

export type CreatePassportProps = {
  issuer: string;
  title: string;
  issuerDid: string;
  issuerAvatarUrl: string;
  ownerDid: string;
  ownerName?: string;
  ownerAvatarUrl: string;
  preferredColor?: string;
  revoked?: boolean;
  isDataUrl?: boolean;
  width?: string;
  height?: string;
  scope?: string;
  role?: string;
  display?: {
    type: string;
    content: string;
  };
};

export type BlockletMetaProps = {
  appLogo?: React.ReactNode;
  appName?: string;
  theme?: {
    background?: string;
  };
  enableConnect?: boolean;
  enableLocale?: boolean;
  navigation?: Array<{
    title?: string | object;
    link?: string | object;
    icon?: string;
    items?: Array<{
      title?: string | object;
      link?: string | object;
    }>;
  }>;
};

export type SessionManagerProps = {
  showText?: boolean;
  showRole?: boolean;
  switchDid?: boolean;
  switchProfile?: boolean;
  switchPassport?: boolean;
  disableLogout?: boolean;
  onLogin?: () => void;
  onLogout?: () => void;
  onSwitchDid?: () => void;
  onSwitchProfile?: () => void;
  onSwitchPassport?: () => void;
  menu?: any[];
  menuRender?: () => void;
  dark?: boolean;
  size?: number;
};
