//
//  UserDetailsRegisterVC.swift
//  WOKA
//
//  Created by MacBook Pro on 29/04/24.
//

import UIKit

class UserDetailsRegisterVC : UIViewController{
    
    @IBOutlet weak var nextBtn: LocalisedElementsButton!
    @IBOutlet weak var enterNameTF: TextFieldShadow!
    @IBOutlet weak var enterUserNameTF: TextFieldShadow!
    @IBOutlet weak var enterPasswordTF: TextFieldShadow!
    @IBOutlet weak var dontSharePassLabel: LocalisedElementsLabel!
    
    var vm = UserDetailsRegisterVM()
    
    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)
    }
    
    // MARK: - Button Tap Handler
    
    @IBAction func nextBtnTapped(_ sender: LocalisedElementsButton) {
        guard let name = enterNameTF.text ,let userName = enterUserNameTF.text , let pass = enterPasswordTF.text else{return}
        
        /*
         Check for username
         */
        if userName.count < 3{
            enterUserNameTF.rightView?.isHidden = false
            enterUserNameTF.setError(K.ConstantString.shortUsername.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue), show: true)
            return
        }
        
        /*
         Check for name
         */
        if name.count < 2{
            enterNameTF.rightView?.isHidden = false
            enterNameTF.setError("Name is too short.".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) , show: true)
            return
        }
        
        /*
         Check for password
         */
        if pass.count < 6{
            let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
            
            vcPush.contentLabel = K.ConstantString.shortPass.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.mainTitleText = K.ConstantString.error.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
//                vcPush.onDoneBlock = { isDone in }
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            self.present(vcPush, animated: true)
            return
        }
        
        /*
         Check if the username exist or not
         */
        vm.checkUsernameExist { isDone in
            if isDone{
                /*
                 Initializing our local data store for user Register
                 */
                AuthFunc.shareInstance.regData.full_name = name
                AuthFunc.shareInstance.regData.username = userName
                AuthFunc.shareInstance.regData.password = pass
                
                let sb = UIStoryboard(name: K.StoryBoard.authenticationSB, bundle: nil)
                let vc = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Authentication.userIntrestVC) as! UserIntrestVC
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
}

// MARK: - Textfield Delegate

extension UserDetailsRegisterVC : UITextFieldDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        switch textField{
        case enterNameTF:
            textField.hideError()
            if !string.nameCharacterOnly(){return false}
            return ValidatorClass.sharedInstanec.limitCharacter(length: 50,textField, shouldChangeCharactersIn: range, replacementString: string)
        case enterUserNameTF:
            textField.hideError()
            if !string.numberAndCharacterAndSpecialChar(){return false}
            return ValidatorClass.sharedInstanec.limitCharacter(length: 16,textField, shouldChangeCharactersIn: range, replacementString: string)
        case enterPasswordTF:
            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 enterUserNameTF:
            enterNameTF.becomeFirstResponder()
        case enterNameTF:
            enterPasswordTF.becomeFirstResponder()
        case enterPasswordTF:
            enterPasswordTF.resignFirstResponder()
        default:
            break
        }
        return true
    }
    
}
