//
//  NewPasswordVC.swift
//  WOKA
//
//  Created by Bilal on 03/05/2024.
//

import UIKit

class NewPasswordVC: UIViewController {

    @IBOutlet weak var enterNewPasswordTF: TextFieldShadow!
    @IBOutlet weak var confirmPassTF: TextFieldShadow!
    @IBOutlet weak var nextBtn: LocalisedElementsButton!
    
    var vm = NewPasswordVM()
    
    // MARK: - View LifeCycle

    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
        if self.isMovingFromParent {
            // Back button was pressed
            PersistentStorage.shared.addOthersCount()
        }
    }
        
    @IBAction func nextBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addOthersCount()
        guard let pass = enterNewPasswordTF.text?.trimmingCharacters(in: .whitespaces) , let confirmPass = confirmPassTF.text?.trimmingCharacters(in: .whitespaces) else{return}

        /*
         Check for password
         */
        if pass.count == 0 || confirmPass.count == 0{
            let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
            
            vcPush.contentLabel = "Enter your password".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.mainTitleText = "Password".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
//                vcPush.onDoneBlock = { isDone in }
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            self.present(vcPush, animated: true)
            return
        }
        
        if pass.count < 6 || confirmPass.count < 6{
            let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
            
            vcPush.contentLabel = "Password is too short.".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.mainTitleText = "Password".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
//                vcPush.onDoneBlock = { isDone in }
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            self.present(vcPush, animated: true)
            return
        }
        
        /*
         Check if password are matching
         */
        if pass != confirmPass{
            let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
            
            vcPush.contentLabel = "Password doesn't match".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.mainTitleText = "Password".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
//                vcPush.onDoneBlock = { isDone in }
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            self.present(vcPush, animated: true)
            return
        }
        
        vm.updatePassword()
    }
}

// MARK: - Textfield Delegate

extension NewPasswordVC : UITextFieldDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        switch textField{
        case enterNewPasswordTF,confirmPassTF:
            let currentString = (textField.text ?? "") as NSString
            let newString = currentString.replacingCharacters(in: range, with: string)
            newString.count == 0 ? (textField.rightView?.isHidden = true) : (textField.rightView?.isHidden = false)
            return ValidatorClass.sharedInstanec.limitCharacter(length: 16,textField, shouldChangeCharactersIn: range, replacementString: string)
        default:
            return true
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        switch textField{
        case enterNewPasswordTF:
            self.confirmPassTF.becomeFirstResponder()
        case confirmPassTF:
            self.view.endEditing(true)
        default:
            break
        }
        return true
    }
}
