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

import UIKit

class ResetPassUserNameVC: UIViewController {

    @IBOutlet weak var usernameTF: TextFieldShadow!
    @IBOutlet weak var nextBtn: LocalisedElementsButton!
    var vm = ResetPassUserNameVM()
    
    // 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()
        /*
         Check for username
         */
        if usernameTF.text!.count < 3{
            usernameTF.rightView?.isHidden = false
            usernameTF.setError(K.ConstantString.shortUsername.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue), show: true)
            return
        }
        
        vm.sendOtpToUserName()
    }
}

// MARK: - Textfield Delegate

extension ResetPassUserNameVC : UITextFieldDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        switch textField{
        case usernameTF:
            textField.hideError()
            if !string.numberAndCharacterAndSpecialChar(){return false}
            return ValidatorClass.sharedInstanec.limitCharacter(length: 16,textField, shouldChangeCharactersIn: range, replacementString: string)
        default:
            return true
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        switch textField{
        case usernameTF:
            textField.resignFirstResponder()
        default:
            break
        }
        return true
    }
    
}
