//
//  OtpVC.swift
//  OrderPicky
//
//  Created by Vijith on 09/02/22.
//

import UIKit

class OtpVC: UIViewController,UITextFieldDelegate {
    
    @IBOutlet weak var tf1: UITextField!
    @IBOutlet weak var tf2: UITextField!
    @IBOutlet weak var tf3: UITextField!
    @IBOutlet weak var tf4: UITextField!

    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
    @IBOutlet weak var verifyBtn: UIButton!
    func initializeHideKeyboard(){
        //Declare a Tap Gesture Recognizer which will trigger our dismissMyKeyboard() function
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(
            target: self,
            action: #selector(dismissMyKeyboard))
        //Add this tap gesture recognizer to the parent view
        view.addGestureRecognizer(tap)
    }
    @objc func dismissMyKeyboard(){
        //endEditing causes the view (or one of its embedded text fields) to resign the first responder status.
        //In short- Dismiss the active keyboard.
        view.endEditing(true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.tf1.becomeFirstResponder()
        self.tf1.text = ""
        self.tf2.text = ""
        self.tf3.text = ""
        self.tf4.text = ""
        
        tf1.addTarget(self, action: #selector(self.textdidChange(textfield:)), for: UIControl.Event.editingChanged)
        tf2.addTarget(self, action: #selector(self.textdidChange(textfield:)), for: UIControl.Event.editingChanged)
        tf3.addTarget(self, action: #selector(self.textdidChange(textfield:)), for: UIControl.Event.editingChanged)
        tf4.addTarget(self, action: #selector(self.textdidChange(textfield:)), for: UIControl.Event.editingChanged)
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        
     //   print("signup =\(isFromSignup)")
        self.addKeyboardObserver()
        initializeHideKeyboard()
        tf1.addBottomBorder()
        tf2.addBottomBorder()
        tf3.addBottomBorder()
        tf4.addBottomBorder()
        
        setui()
    }
    override func viewWillDisappear(_ animated: Bool) {
        self.removeKeyboardObserver()
    }
    @objc func textdidChange(textfield: UITextField)
{
    let text = textfield.text
    if text?.utf16.count == 1 {
        switch textfield{
            
        case tf1: tf2.becomeFirstResponder()
            break
            
        case tf2: tf3.becomeFirstResponder()
            break
            
        case tf3: tf4.becomeFirstResponder()
            break
            
        case tf4: tf4.resignFirstResponder()
            break
            
      
        default : break
            
        }
    }
    if  text?.utf16.count == 0 {
        switch textfield{
        case tf1:
            tf1.becomeFirstResponder()
        case tf2:
            tf1.becomeFirstResponder()
        case tf3:
            tf2.becomeFirstResponder()
        case tf4:
            tf3.becomeFirstResponder()
       
        default:
            break
        }
    }
    else {
        
    }}
    
    func setui(){
        self.verifyBtn.layer.cornerRadius = 8.0
    }
   
    @IBAction func OnTapBackBtn(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @IBAction func onTapVerifyBtn(_ sender: Any) {
        let nextVc = self.storyboard?.instantiateViewController(withIdentifier: "HomeVC")as! HomeVC
        self.navigationController?.pushViewController(nextVc, animated: true)
    }
    
    
}
