//
//  CalendarViewController.swift
//  Em-call
//
//  Created by Alex Sh on 18.02.2022.
//

import UIKit


protocol CalendarViewControllerDelegate:class {
    func calendarDelegateClick(text:String,flagData:Int)
}

class CalendarViewController: UIViewController {
    var showAlert:ShowAlert = ShowAlert()
    @IBOutlet weak var dateAndTimePicker: UIDatePicker!
    @IBOutlet weak var retryNewContact: UITextField!
    
    weak var delegate:CalendarViewControllerDelegate?
    
    @IBOutlet weak var retryNewCard: UITextField!
    @IBOutlet weak var enterNewCard: UITextField!
    @IBOutlet weak var enterNewContact: UITextField!
    
    @IBAction func saveDateTime(_ sender: Any) {
 
        let dateFormatter = DateFormatter()
            dateFormatter.dateStyle = DateFormatter.Style.short
            dateFormatter.timeStyle = DateFormatter.Style.short
            let strDate = dateFormatter.string(from: dateAndTimePicker.date)
           // dateLabel.text = strDate
        
        self.delegate?.calendarDelegateClick(text: strDate,flagData:0)
        
        _  = self.navigationController?.popViewController( animated: true)
        
    }
   
    @IBAction func saveContact(_ sender: Any) {
        //var contact:String
        if retryNewContact.text == "" || enterNewContact.text == "" {
            DispatchQueue.main.async {
                   let alert = UIAlertController(title: "Warning", message: "<Enter new contact> or <Retry new contact> 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 retryNewContact.text == enterNewContact.text {
            let contact = enterNewContact.text ?? " "
            self.delegate?.calendarDelegateClick(text:contact,flagData:1)
            _  = 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: "<Enter new contact> does not match <Retry new contact>", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                   self.present(alert, animated: true)
                }
           // self.showAlert.showAlertSample(title: "Warning",message: "Enter new contact does not match Retry new contact")
  
        }
    }
    
    @IBAction func saveCard(_ sender: Any) {
        if retryNewCard.text == "" || enterNewCard.text == "" {
            DispatchQueue.main.async {
                   let alert = UIAlertController(title: "Warning", message: "<Enter new card> or <Retry new card> 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 retryNewCard.text == enterNewCard.text {
            let card = enterNewCard.text ?? " "
            self.delegate?.calendarDelegateClick(text:card,flagData:2)
            _  = self.navigationController?.popViewController( animated: true)
        }
        else {
            DispatchQueue.main.async {
                   let alert = UIAlertController(title: "Warning", message: "<Enter new card> does not match <Retry new card>", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                   self.present(alert, animated: true)
                }
          }
    }
    
       
    override func viewDidLoad() {
        super.viewDidLoad()

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

    /*
    // 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.
    }
    */

}
