//
//  WalletSuccessViewController.swift
//  B24PaymentSdk
//
//  Created by visal ny on 5/2/25.
//

import UIKit

class WalletSuccessViewController: UIViewController {

    @IBOutlet weak var bgContainer: UIView!
    @IBOutlet weak var successTitle: UILabel!
    @IBOutlet weak var subTitle: UILabel!
    @IBOutlet weak var buttonDone: UIButton!
    weak var presentingWalletVC: UIViewController?
    weak var bottomSheetVC: BottomSheetViewController?
    
    var language:String = "km"
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = DefaultAppearance.shared.screenBgColor
        
        getSharePref()
        
        B24PaymentSdkHelper.getCurrentLanguage(language: language)
        
        applyAppearance()

        // Do any additional setup after loading the view.
        
        
    }
    
    private func getSharePref(){
        if let languageCode = SharedPreferenceManager.getString(forKey: SharePrefKey.lanuageCode.rawValue){
            language = languageCode
        }else{
            language = "km"
        }
    }
    
    private func applyAppearance(){
        bgContainer.backgroundColor = DefaultAppearance.shared.screenBgColor
        successTitle.text = B24PaymentSdkHelper.localized(WalletSuccessLocalizedKeys.title.rawValue)
        successTitle.font = FontManager.shared.boldFont(forLanguage: language, size: FixFontSize.cardTitle)
        successTitle.textColor = DefaultAppearance.shared.primaryLabelColor
        
        subTitle.text = B24PaymentSdkHelper.localized(WalletSuccessLocalizedKeys.sub_title.rawValue)
        subTitle.font = FontManager.shared.regularFont(forLanguage: language, size: FixFontSize.cardSubTitle)
        subTitle.textColor = DefaultAppearance.shared.primaryLabelColor
        

        buttonDone.backgroundColor = DefaultAppearance.shared.primaryColor
        buttonDone.layer.cornerRadius = 8
        let topupAttributedTitle = NSAttributedString(
            string: B24PaymentSdkHelper.localized(WalletSuccessLocalizedKeys.done.rawValue),
                    attributes: [
                        .font: FontManager.shared.regularFont(forLanguage: self.language, size: FixFontSize.buttonText),
                        .foregroundColor: DefaultAppearance.shared.onPrimaryColor
                    ]
                )
        self.buttonDone.setAttributedTitle(topupAttributedTitle, for: .normal)
        
        
    }
    
    
    @IBAction func doneAction(_ sender: Any) {
        // Find the existing nav controller (WalletSuccessVC is presented on PortraitNavigationController)
        let navVC = (self.presentingViewController as? UINavigationController)
                    ?? self.presentingViewController?.navigationController

        if let navVC = navVC,
           let accountDetailVC = navVC.viewControllers.first(where: { $0 is AccountDetailView }) as? AccountDetailView {
            // Pop back to the existing AccountDetailView and refresh it
            navVC.popToViewController(accountDetailVC, animated: false)
            self.dismiss(animated: true) {
                accountDetailVC.refreshAfterPayment()
            }
            return
        }

        // Fallback: just dismiss if the nav stack doesn't have AccountDetailView
        self.dismiss(animated: true)
    }


}
