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

import UIKit

enum NavigateToVC{
    case newPass
    case registeration
}

class OTPVC : UIViewController{
    
    @IBOutlet weak var wokaLogoTopConstriant: NSLayoutConstraint!
    
    @IBOutlet weak var tf1: UITextField!
    @IBOutlet weak var tf2: UITextField!
    @IBOutlet weak var tf3: UITextField!
    @IBOutlet weak var tf4: UITextField!
    @IBOutlet weak var nextBtn: LocalisedElementsButton!
    @IBOutlet weak var resendOTPBtn: LocalisedElementsButton!
    
    @IBOutlet weak var codeSentLabel: UILabel!
    @IBOutlet weak var otpValidTillLabel: LocalisedElementsLabel!
    @IBOutlet weak var enterCodeLabel: UILabel!
    @IBOutlet weak var requestThemLabel: LocalisedElementsLabel!
    
    var vm = OTPVM()
    var navigateCheck = NavigateToVC.registeration
    
    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()
        }
    }
    
    deinit {
        vm.timer?.invalidate()
    }
    // MARK: - Button Tap Handler
    
    @IBAction func nextBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addOthersCount()
        vm.validateOTP()
    }
    
    @IBAction func resendOTPBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addOthersCount()
        vm.remainingTime = 10 * 60
        vm.resendOTP()
    }
}

// MARK: - TextField Delegate

extension OTPVC : UITextFieldDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        // Check if the input text contains only numbers
        if !string.onlyNumber(){return false}
        
        // If user pastes text into the text field, check the count and process accordingly
        if string.count == 4{
            self.vm.textFieldDidChange(textField, otpCode: string)
            return false
        }
        
        // Limit the input to one character
        let maxLength = 1
        let currentString = (textField.text ?? "") as NSString
        let newString = currentString.replacingCharacters(in: range, with: string)
        return newString.count <= maxLength
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        // Check if the pasted text contains the content of the general pasteboard
        if text.contains(UIPasteboard.general.string ?? "") {
            return false
        }
        return true
    }
}
