//
//  RegistrationViewController.swift
//  Em-call
//
//  Created by Alex Sh on 22.02.2022.
//

import UIKit

protocol RegistrationViewControllerDelegate:class {
    func registrationDelegateClick(text:String,flagData:Int)
}

class RegistrationViewController: UIViewController {

    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var confPsw: UITextField!
    weak var delegate:RegistrationViewControllerDelegate?
    @IBOutlet weak var nameUser: UITextField!
    @IBOutlet weak var telephonNumber: UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    @IBAction func butReg(_ sender: Any) {
        if password.text == "" || confPsw.text == "" {
            DispatchQueue.main.async {
                   let alert = UIAlertController(title: "Warning", message: "<Password> or <Confirm password> is empty", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                   self.present(alert, animated: true)
                }
           // _  = self.navigationController?.popViewController( animated: true)
            return
        }
        if nameUser.text == "" || telephonNumber.text == "" {
            DispatchQueue.main.async {
                   let alert = UIAlertController(title: "Warning", message: "<Name> or <Telephone number> is empty", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                   self.present(alert, animated: true)
            }
            return
        }
        if password.text == confPsw.text {
            checkTelephone()
             let passwrd = password.text ?? ""
             if passwrd != "" {
                  self.delegate?.registrationDelegateClick(text:passwrd,flagData:1)
            //    DispatchQueue.main.async {
                       let alert = UIAlertController(title: "Success", message: "Your registration is complete", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                       self.present(alert, animated: true)
              //  }
            }
            _  = self.navigationController?.popViewController( animated: true)
        }
        else {
            DispatchQueue.main.async {
               // self.showAlert.showAlertSample(title: "Warning",message: "Enter new contact does not match Retry new contact")
                   let alert = UIAlertController(title: "Warning", message: "<Password> does not match <Confirm password>", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                   self.present(alert, animated: true)
                }
        }
    
    }
    
    func checkTelephone() {
        
    }
    
    func sendData() {
        
    }
    
    /*    // MARK: - Navigation
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }    */

}
