import FawryPaySDK

class FawryModelConverter {
    static func convert(_ model: FawryLaunchModel) -> FawryPaySDK.FawryLaunchModel {
        return FawryPaySDK.FawryLaunchModel(
          customer: convert(model.customerInfo),
          merchant: convert(model.merchantInfo),
          chargeItems: convert(model.items),
          paymentSignture: (model.paymentSignature != nil && !(model.paymentSignature ?? "").isEmpty) ? model.paymentSignature : nil,
          tokenizationSignature: (model.tokenizationSignature != nil && !(model.tokenizationSignature ?? "").isEmpty) ? model.tokenizationSignature : nil,
            allowVoucher: model.allowVoucher,
            paymentWithCardToken: model.payWithCardToken,
            skipReceipt: model.skipReceipt,
            skipCustomerInput: model.skipCustomerInput ?? false,
            paymentMethod: convertPaymentMethod(model.paymentMethod),
            checkoutModel: convert(model.launchCheckoutModel),
            applePayModel: convert(model.launchApplePayModel)
        )
    }

    static func convert(_ customer: CustomerInfo) -> FawryPaySDK.LaunchCustomerModel {
        return FawryPaySDK.LaunchCustomerModel(
            customerName: customer.customerName,
            customerEmail: customer.customerEmail,
            customerMobile: customer.customerMobile,
            customerProfileId: customer.customerProfileId
        )
    }

    static func convert(_ merchant: MerchantInfo) -> FawryPaySDK.LaunchMerchantModel {
        return FawryPaySDK.LaunchMerchantModel(
            merchantCode: merchant.merchantCode,
            merchantRefNum: merchant.merchantRefNum,
            secureKey: merchant.merchantSecretCode
        )
    }

  static func convert(_ items: [BillItems]) -> [FawryPaySDK.ChargeItemsParamsModel] {
        return items.map { item in
            FawryPaySDK.ChargeItemsParamsModel(
                itemId: item.itemId,
                charge_description: item.description,
                price: Double(item.price) ?? 0.0,
                quantity: Int(item.quantity) ?? 1
            )
        }
    }

  static func convert(_ checkoutModel: LaunchCheckoutModel?) -> FawryPaySDK.LaunchCheckoutModel? {
        if(checkoutModel == nil) {
          return nil
        }else{
          return FawryPaySDK.LaunchCheckoutModel(
            scheme: checkoutModel?.scheme
          )
        }
    }

  static func convert(_ applePayModel: LaunchApplePayModel?) -> FawryPaySDK.LaunchApplePayModel? {
        if(applePayModel == nil) {
          return nil
        }else{
          return FawryPaySDK.LaunchApplePayModel(
            merchantID: applePayModel?.merchantID
          )
        }
    }

    static func convertPaymentMethod(_ method: String?) -> FawryPaySDK.Payment_Method {
        switch method?.uppercased() {
        case "PAYATFAWRY": return .payAtFawry
        case "PAYUSINGCC": return .card
        case "MWALLET": return .mWallet
        case "FPCARD": return .yellowCard
        case "APPLE_PAY": return .applePay
        default: return .all
        }
    }

    static func printModel(_ model: FawryPaySDK.FawryLaunchModel) {
      print(JSONConverter.toJson(model))
    }
}

