//
//  ProfileVC.swift
//  WOKA
//
//  Created by Bilal on 05/06/2024.
//

import UIKit

class ProfileVC: UIViewController {

    @IBOutlet weak var avatarImage: UIImageView!
    @IBOutlet weak var userNameLabel: UILabel!
    @IBOutlet weak var fullNameTF: TextFieldShadow!
    @IBOutlet weak var emailTF: TextFieldShadow!
    @IBOutlet weak var dob: UIDatePicker!
    @IBOutlet weak var boyView: UIView!
    @IBOutlet weak var girlView: UIView!
    @IBOutlet weak var nextBtn: LocalisedElementsButton!
    
    var vm = ProfileVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
        self.title = "Profile".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
        
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
    }

    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()
        if fullNameTF.text!.count < 3 {
            let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
            vcPush.contentLabel = "Name is too short".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.mainTitleText = "Error".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            self.present(vcPush, animated: true)
            return
        }
        
        vm.updateUserData()
    }
}

// MARK: - Textfield

extension ProfileVC : UITextFieldDelegate {
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        switch textField{
        case fullNameTF:
            // check if the latest name is same as the stored name
            if let text = textField.text, let textRange = Range(range, in: text) {
                let updatedText = text.replacingCharacters(in: textRange, with: string)
                vm.checkChanges(name: updatedText)
            }
            if !string.nameCharacterOnly(){return false}
            return ValidatorClass.sharedInstanec.limitCharacter(length: 50,textField, shouldChangeCharactersIn: range, replacementString: string)
        default:
            return true
        }
    }
    
//    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//        switch textField{
//        case emailTF:
//            nameTF.becomeFirstResponder()
//        case nameTF:
//            textField.resignFirstResponder()
//        default:
//            break
//        }
//        return true
//    }
//    
}
