import UIKit
import Async
import WowonderMessengerSDK

class ForgetPasswordVC: BaseVC {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var emailTextField: UITextField!
    
    // MARK: - View Life Cycles
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initialConfig()
    }
    
    // MARK: - Selectors
    
    // Back Button Action
    @IBAction func backButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.navigationController?.popViewController(animated: true)
    }
    
    // Send Button Action
    @IBAction func sendButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        if self.emailTextField.text?.trimmingCharacters(in: .whitespaces).count == 0 {
            let securityAlertVC = R.storyboard.main.securityPopupVC()
            securityAlertVC?.titleText  = NSLocalizedString("Security", comment: "Security")
            securityAlertVC?.errorText = NSLocalizedString("Please enter email.", comment: "Please enter email.")
            self.present(securityAlertVC!, animated: true, completion: nil)
            return
        }
        if !(self.emailTextField.text?.isEmail ?? false) {
            let securityAlertVC = R.storyboard.main.securityPopupVC()
            securityAlertVC?.titleText  = NSLocalizedString("Security", comment: "Security")
            securityAlertVC?.errorText = NSLocalizedString("Email is badly formatted.", comment: "Email is badly formatted.")
            self.present(securityAlertVC!, animated: true, completion: nil)
            return
        }
        self.handleForgetPasswordTap()
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.emailTextField.placeHolderColor = UIColor.darkGray
    }
    
    private func handleForgetPasswordTap() {
        self.view.endEditing(true)
        if Connectivity.isConnectedToNetwork() {
            let email = self.emailTextField.text ?? ""
            Async.background {
                UserManager.instance.forgetPassword(Email: email, completionBlock: { (success, sessionError, serverError, error) in
                    if success != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                // print("Success = \(success?.apiStatus)")
                                let securityAlertVC = R.storyboard.main.securityPopupVC()
                                securityAlertVC?.titleText = NSLocalizedString("Check your email", comment: "Check your email")
                                securityAlertVC?.errorText = "\(NSLocalizedString("We Sent email to", comment: "We Sent email to")) \(email). \(NSLocalizedString("Click the link in the email to reset your password.", comment: "Click the link in the email to reset your password."))"
                                self.present(securityAlertVC!, animated: true, completion: nil)
                            }
                        }
                    } 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)")
            }
        }
    }
    
}
