

import Async
import UIKit
import WowonderMessengerSDK

class TwoFactorVerifyVC: BaseVC {
    @IBOutlet weak var verifyCodeTextField: UITextField!
    
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var verifyBtn: UIButton!
    
    var code:String? = ""
    var userID : String? = ""
    var error = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = false
        self.verifyCodeTextField.placeholder = NSLocalizedString("Add code number", comment: "Add code number")
        self.verifyBtn.setTitle(NSLocalizedString("VERIFY", comment: "VERIFY"), for: .normal)
        self.label1.text = NSLocalizedString("To log in, you need to verify  your identity.", comment: "To log in, you need to verify  your identity.")
        self.label2.text = NSLocalizedString("We have sent you the confirmation code to your email address.", comment: "We have sent you the confirmation code to your email address.")
        self.verifyBtn.setTitleColor(.accent, for: .normal)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    @IBAction func verifyPressed(_ sender: Any) {
        if self.verifyCodeTextField.text!.isEmpty{
            self.view.makeToast(NSLocalizedString("Please enter Code", comment: "Please enter Code"))
        }else{
            self.verifyTwoFactor()
        }
    }
    private func verifyTwoFactor(){
        
        if appDelegate.isInternetConnected{
            if (self.verifyCodeTextField.text!.isEmpty){
                let securityAlertVC = R.storyboard.main.securityPopupVC()
                securityAlertVC?.titleText  = NSLocalizedString("Security", comment: "Security")
                securityAlertVC?.errorText = NSLocalizedString("Please enter Code.", comment: "Please enter Code.")
                self.present(securityAlertVC!, animated: true, completion: nil)
            }else{
                //
                let userID = self.userID ?? ""
                let code = self.verifyCodeTextField.text ?? ""
                
                Async.background({
                    TwoFactorManager.instance.verifyCode(code: code, UserID: userID) { (success, sessionError, serverError, error) in
                        if success != nil{
                            Async.main{
                                self.dismissProgressDialog {
                                    let user_Session = success?.getStringAnyData() ?? ["":""]
                                    UserDefaults.standard.setUserSession(value: user_Session, ForKey: Local.USER_SESSION.User_Session)
                                    AppInstance.instance.fetchUserProfile { userData in
                                        var getSwitchAccountData = UserDefaults.standard.getSwitchAccountData(Key: "Switch_Account_Data")
                                        let data = try! JSONSerialization.data(withJSONObject: getSwitchAccountData, options: [])
                                        let switchAccountData = try! JSONDecoder().decode([UserData].self, from: data)
                                        if switchAccountData.contains(where: { $0.user_id == userData?.user_data?.user_id }) {
                                            print("Switch Account Data Already Added")
                                        } else {
                                            var user = userData?.user_data?.getStringAnyData() ?? [:]
                                            user["access_token"] = success?.access_token
                                            getSwitchAccountData.append(user)
                                            UserDefaults.standard.setSwitchAccountData(value: getSwitchAccountData, ForKey: "Switch_Account_Data")
                                            print("Switch Account Data Added")
                                        }
                                    }
                                    let vc = R.storyboard.main.introVC()
                                    self.navigationController?.pushViewController(vc!, animated: true)
                                    self.view.makeToast(NSLocalizedString("Login Successfull!!", comment: "Login Successfull!!"))
                                }
                            }
                        }else if sessionError != nil{
                            Async.main{
                                self.dismissProgressDialog {
                                    print("session Error = \(sessionError?.errors?.error_text ?? "")")
                                    
                                    let securityAlertVC = R.storyboard.main.securityPopupVC()
                                    securityAlertVC?.titleText  = NSLocalizedString("Security", comment: "Security")
                                    securityAlertVC?.errorText = sessionError?.errors?.error_text ?? ""
                                    self.present(securityAlertVC!, animated: true, completion: nil)
                                    
                                }
                            }
                        }else if serverError != nil{
                            Async.main({
                                self.dismissProgressDialog {
                                    
                                    print("serverError = \(serverError?.errors?.error_text ?? "")")
                                    
                                    let securityAlertVC = R.storyboard.main.securityPopupVC()
                                    securityAlertVC?.titleText  = NSLocalizedString("Security", comment: "Security")
                                    securityAlertVC?.errorText = sessionError?.errors?.error_text ?? ""
                                    self.present(securityAlertVC!, animated: true, completion: nil)
                                }
                            })
                            
                        }else {
                            Async.main({
                                self.dismissProgressDialog {
                                    print("error = \(error?.localizedDescription ?? "")")
                                    let securityAlertVC = R.storyboard.main.securityPopupVC()
                                    securityAlertVC?.titleText  = NSLocalizedString("Security", comment: "Security")
                                    securityAlertVC?.errorText = error?.localizedDescription ?? ""
                                    self.present(securityAlertVC!, animated: true, completion: nil)
                                }
                            })
                        }
                    }
                })
            }
        }else{
            self.dismissProgressDialog {
                let securityAlertVC = R.storyboard.main.securityPopupVC()
                securityAlertVC?.titleText  = NSLocalizedString("Internet Error", comment: "Internet Error")
                securityAlertVC?.errorText = InterNetError
                self.present(securityAlertVC!, animated: true, completion: nil)
                print("internetError - \(InterNetError)")
            }
        }
    }
}
