import { NativeEventEmitter, NativeModules, Platform } from 'react-native';

const LINKING_ERROR = `
The package '@fawry_pay/rn-fawry-pay-sdk' doesn't seem to be linked. Make sure:

${Platform.select({ ios: "- You have run 'pod install'\n", default: '' })}
- You rebuilt the app after installing the package
- You are not using Expo Go
`;

const RnFawryPaySdk = NativeModules.RnFawryPaySdk || new Proxy({}, {
  get() {
    throw new Error(LINKING_ERROR);
  },
});

export async function startPayment( fawryLaunchModel : FawryLaunchModel
): Promise<any> {
  RnFawryPaySdk.startPayment(
    fawryLaunchModel.baseUrl,
    fawryLaunchModel.lang,
    fawryLaunchModel.merchantInfo,
    fawryLaunchModel.customerInfo,
    fawryLaunchModel.items,
    fawryLaunchModel.allow3DPayment,
    fawryLaunchModel.skipReceipt,
    fawryLaunchModel.skipLogin,
    fawryLaunchModel.payWithCardToken,
    fawryLaunchModel.authCaptureMode,
    fawryLaunchModel.allowVoucher,
    fawryLaunchModel.signature
  );
}

export async function openCardsManager(
  baseUrl: string,
  lang: FawryLanguages,
  merchantInfo: MerchantInfo,
  customerInfo: CustomerInfo
): Promise<any> {
  RnFawryPaySdk.openCardsManager(baseUrl, lang, merchantInfo, customerInfo);
}

export interface BillItems {
  itemId: string;
  description: string;
  quantity: string;
  price: string;
}

export interface MerchantInfo {
  merchantCode: string;
  merchantSecretCode: string;
  merchantRefNum: string;
}

export interface CustomerInfo {
  customerName: string;
  customerMobile: string;
  customerEmail: string;
  customerProfileId: string;
}


export interface FawryLaunchModel {
  baseUrl: string,
  lang: FawryLanguages,
  merchantInfo: MerchantInfo,
  customerInfo: CustomerInfo,
  items: BillItems[],
  allow3DPayment: boolean,
  skipReceipt: boolean,
  skipLogin: boolean,
  payWithCardToken: boolean,
  authCaptureMode: boolean,
  allowVoucher: boolean,
  signature: string
}

export enum FawryLanguages {
  ENGLISH = 'ENGLISH',
  ARABIC = 'ARABIC',
}

export class FawryCallbacks {
  static FawryEmitter = new NativeEventEmitter(RnFawryPaySdk);
  static FAWRY_EVENT_PAYMENT_COMPLETED = 'FAWRY_EVENT_PAYMENT_COMPLETED';
  static FAWRY_EVENT_ON_SUCCESS = 'FAWRY_EVENT_ON_SUCCESS';
  static FAWRY_EVENT_ON_FAIL = 'FAWRY_EVENT_ON_FAIL';
  static FAWRY_EVENT_CardManager_FAIL = 'FAWRY_EVENT_CardManager_FAIL';
}
