//
//  CreateAccountVC.swift
//  Master Your Money Build Week
//
//  Created by Joel Groomer on 8/21/19.
//  Copyright © 2019 Seschwan. All rights reserved.
//

import UIKit

class CreateAccountVC: UIViewController {
    
    // MARK: - Outlets
    
    @IBOutlet weak var firstNameTxtField: UITextField!
    @IBOutlet weak var lastNameTxtField:  UITextField!
    @IBOutlet weak var emailTxtField:     UITextField!
    @IBOutlet weak var passwordTxtField:  UITextField!
    
    @IBOutlet weak var createAcctBtn:     UIButton!
    
    @IBOutlet weak var createAcctBackgroundImageView: UIImageView!
    
    
    var user: User?
    
    var userController = UserController()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        styler()
        borders()
        doneBarBtn()

    }
    

    func styler() {
        let cornerRadius: CGFloat = 5
        
        firstNameTxtField.layer.cornerRadius = cornerRadius
        lastNameTxtField.layer.cornerRadius  = cornerRadius
        emailTxtField.layer.cornerRadius     = cornerRadius
        passwordTxtField.layer.cornerRadius  = cornerRadius
        createAcctBtn.layer.cornerRadius     = cornerRadius
        
        createAcctBackgroundImageView.alpha = 0.4
        
        navigationController?.navigationBar.isHidden = false
    }
    
    func borders() {
        let borderWidth: CGFloat = 1
        let borderColor = UIColor.gray.cgColor
        
        firstNameTxtField.layer.borderWidth = borderWidth
        lastNameTxtField.layer.borderWidth = borderWidth
        emailTxtField.layer.borderWidth = borderWidth
        passwordTxtField.layer.borderWidth = borderWidth
        
        firstNameTxtField.layer.borderColor = borderColor
        lastNameTxtField.layer.borderColor = borderColor
        emailTxtField.layer.borderColor = borderColor
        passwordTxtField.layer.borderColor = borderColor
    }
    
    func doneBarBtn() {
        let toolBar = UIToolbar()
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
        let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(self.doneClicked))
        toolBar.setItems([flexibleSpace,doneButton], animated: false)
        toolBar.sizeToFit()
        firstNameTxtField.inputAccessoryView = toolBar
        lastNameTxtField.inputAccessoryView  = toolBar
        emailTxtField.inputAccessoryView     = toolBar
        passwordTxtField.inputAccessoryView  = toolBar
    }
    
    @objc func doneClicked() {
        view.endEditing(true)
    }
    
    // MARK: - Actions
    @IBAction func createAcctBtnPressed(_ sender: UIButton) {
        guard let firstName = firstNameTxtField.text, !firstName.isEmpty,
            let lastName = lastNameTxtField.text, !lastName.isEmpty,
            let userName = emailTxtField.text, !userName.isEmpty,
            let password = passwordTxtField.text, !password.isEmpty else { return }
        
        //userController.deleteFromPersistentStore()
        
      
        let _ = User(username: userName)
        userController.saveToPersistentStore()
        
        
    }
    // 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?) {
        if segue.identifier == "ToProfileSegue" {
            guard let profileVC = segue.destination as? ProfileVC else { return }
            
        }
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    

}
