//
//  ForEmbedVC.swift
//  AppIzipay
//
//  Created by Enrrique Antonio Pariasca Celiz on 11/06/25.
//

import UIKit
import IzipayPayButtonSDK
import VisaSensoryBranding
import MastercardSonic

protocol ForEmbedVCDelegate {
    
    func passResponseToOrigin(_ paymentResult: PaymentResult)
    
}

class ForEmbedVC: UIViewController {

    private let sonicManager: MCSonicController = MCSonicController()
    
    var configIzipay : ConfigPaymentIzipay?
    var transactionId : String?
    var delegate : ForEmbedVCDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.showIzipayPaymentEmbedded()
    }
    
    @IBOutlet weak var viewContainer: UIView!
    
    func showIzipayPaymentEmbedded(){
        let s = UIStoryboard(name: "IziPayment", bundle: Bundle(for: PaymentFormViewController.self))
        let vc = s.instantiateViewController(withIdentifier: "PaymentFormView") as! PaymentFormViewController
        vc.delegate = self
        vc.configPayment = self.configIzipay
        addChild(vc)
        vc.view.frame = viewContainer.bounds
        viewContainer.addSubview(vc.view)
        vc.didMove(toParent: self)
    }
    
    
    @IBAction func onTapBack(_ sender: Any) {
        var resultBack = PaymentResult()
        resultBack.code = "CERRADO X COMERCIO"
        resultBack.message = "El SDK(en modo embebido) fue cerrado por la app del comercio, esta respuesta viene de la app del comercio."

        //recomendado: consultar al api de consulta de transacciones en estos casos
        delegate?.passResponseToOrigin(resultBack)
        self.dismiss(animated: false)
    }
}

extension ForEmbedVC : IzipayPaymentDelegate {
    
    func executeSensoryBrandVisa(view: UIView) {
        if let visaview = view as? SensoryBranding {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                visaview.animate()
            }
        }
    }
    
    func executeSensoryBrandMastercard(view: UIView, params: MastercardSonicParams) {
        let merchant = MCSonicMerchant(merchantName: params.name, countryCode: params.country,
                                       city: nil, merchantCategoryCodes: [params.mcc], merchantId: nil)
        
        if let mastercardview = view as? MCSonicView, let safeMerchant = merchant {
            self.sonicManager.prepare(with: .soundAndAnimation, sonicCue: params.sonicCue,
                                      sonicEnvironment: .sandbox, merchant: safeMerchant) { status in
                
                if status == .success {
                    self.sonicManager.play(with: mastercardview) { MCSonicStatus in
                        print(MCSonicStatus)
                    }
                }
            }
            
        }
    }
    
    
    func executeProfiling(_ params: IzipayPayButtonSDK.ScoringParams) {

    }

    func getPaymentResult(_ paymentResult: PaymentResult) {
        //Lectura de respuesta del SDK
        self.dismiss(animated: false) { [weak self] in
            self?.delegate?.passResponseToOrigin(paymentResult)
        }
    }
    
    
    
}
