//
//  AddViewController.swift
//  MyReminders
//
//  Created by Pawan Kumar on 29/10/21.
//
//

import UIKit

class AddViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var titleField: UITextField!
    @IBOutlet var bodyField: UITextField!
    @IBOutlet var datePicker: UIDatePicker!

    public var completion: ((String, String, Date) -> Void)?

    override func viewDidLoad() {
        super.viewDidLoad()
        titleField.delegate = self
        bodyField.delegate = self
        
        self.titleField.layer.cornerRadius = 8.0
        self.titleField.layer.borderWidth = 1.0
        self.titleField.layer.borderColor = UIColor.systemPurple.cgColor
        self.bodyField.layer.cornerRadius = 8.0
        self.bodyField.layer.borderWidth = 1.0
        self.bodyField.layer.borderColor = UIColor.systemPurple.cgColor
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .done, target: self, action: #selector(didTapSaveButton))
    }

    @objc func didTapSaveButton() {
        if let titleText = titleField.text, !titleText.isEmpty,
            let bodyText = bodyField.text, !bodyText.isEmpty {

            let targetDate = datePicker.date

            completion?(titleText, bodyText, targetDate)

        }
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

}
