//
//  DataProfileViewController.swift
//  Em-call
//
//  Created by Alex Sh on 01.03.2022.
//

import UIKit

protocol DataProfileViewControllerDelegate:class {
    func dataProfileDelegateClick(text:String,flagData:Int)
}

class DataProfileViewController: UIViewController {

    @IBOutlet weak var name: UITextField!
    @IBOutlet weak var email: UITextField!
    @IBOutlet weak var contact: UITextField!
    @IBOutlet weak var payment: UITextField!
    
    weak var delegate:DataProfileViewControllerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    @IBAction func saveName(_ sender: Any) {
        if name.text == ""  {
            DispatchQueue.main.async {
                   let alert = UIAlertController(title: "Warning", message: "<Name>  is empty", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                   self.present(alert, animated: true)
                }
              return
        }
        else {
            let sname = name.text ?? " "
            self.delegate?.dataProfileDelegateClick(text: sname,flagData:0)
            _  = self.navigationController?.popViewController( animated: true)
        }
    }
    
    @IBAction func saveEMail(_ sender: Any) {
        if email.text == ""  {
            DispatchQueue.main.async {
                   let alert = UIAlertController(title: "Warning", message: "<E-mail>  is empty", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                   self.present(alert, animated: true)
                }
              return
        }
        else {
            let sname = email.text ?? " "
            self.delegate?.dataProfileDelegateClick(text: sname,flagData:1)
            _  = self.navigationController?.popViewController( animated: true)
        }
    }
    
    
    @IBAction func saveContact(_ sender: Any) {
        if contact.text == ""  {
            DispatchQueue.main.async {
                   let alert = UIAlertController(title: "Warning", message: "<Contact>  is empty", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                   self.present(alert, animated: true)
                }
              return
        }
        else {
            let sname = contact.text ?? " "
            self.delegate?.dataProfileDelegateClick(text: sname,flagData:2)
            _  = self.navigationController?.popViewController( animated: true)
        }
    }
    
    
    @IBAction func savePayment(_ sender: Any) {
        if payment.text == ""  {
            DispatchQueue.main.async {
                   let alert = UIAlertController(title: "Warning", message: "<Payment>  is empty", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                   self.present(alert, animated: true)
                }
              return
        }
        else {
            let sname = payment.text ?? " "
            self.delegate?.dataProfileDelegateClick(text: sname,flagData:3)
            _  = self.navigationController?.popViewController( animated: true)
        }
    }
    /*
    // 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.
    }
    */

}
