//
//  VerificationVC.swift
//  Garmentoo
//
//  Created by ANGEL FS on 25/03/22.
//

import UIKit
import MJSnackBar

class VerificationVC: UIViewController {

    @IBOutlet weak var otpText: UITextField!
    @IBOutlet weak var timerLabel: UILabel!
    
    var snackBar: MJSnackBar!
    var count = 60

    override func viewDidLoad() {
        super.viewDidLoad()

        self.viewLoadingProperties()
    }
    
    //Button actions
    @IBAction func verifyBtnTapped(_ sender: Any) {
        if self.otpText.text == ""
        {
            self.showSnackBar(Message:"Enter OTP" , snackbar: self.snackBar)
            return
        }
        let sb = UIStoryboard(name: "Onboarding", bundle: nil)
        let nextVc = sb.instantiateViewController(withIdentifier: "ConfirmAddressVC")as! ConfirmAddressVC
        self.navigationController?.pushViewController(nextVc, animated: true)
    }
    
}

extension VerificationVC
{
    func viewLoadingProperties()
    {
        self.snackBar =  MJSnackBar(onView: self.view)
        Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)

    }
    @objc func update() {
        if count > 0 {
            count -= 1
            self.timerLabel.text = count.description
           }
    }
}


extension VerificationVC : UITextFieldDelegate
{
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let maxLength = 6
            let currentString: NSString = (textField.text ?? "") as NSString
            let newString: NSString =
                currentString.replacingCharacters(in: range, with: string) as NSString
            return newString.length <= maxLength
    }
}
