//
//  ManualToolbarViewController.swift
//  IQKeyboardManager
//
//  Created by InfoEnum02 on 20/04/15.
//  Copyright (c) 2015 Iftekhar. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift

class ManualToolbarViewController : UIViewController, UIPopoverPresentationControllerDelegate {
    
    @IBOutlet fileprivate var textField1 : UITextField!
    @IBOutlet fileprivate var textField2 : UITextField!
    @IBOutlet fileprivate var textView3 : UITextView!
    @IBOutlet fileprivate var textField4 : UITextField!
    @IBOutlet fileprivate var textField5 : UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()
        
        textField1.addPreviousNextDoneOnKeyboardWithTarget(self, previousAction: #selector(self.previousAction(_:)), nextAction: #selector(self.nextAction(_:)), doneAction: #selector(self.doneAction(_:)), shouldShowPlaceholder: true)
        textField1.setEnablePrevious(false, isNextEnabled: true)
        
        textField2.addPreviousNextDoneOnKeyboardWithTarget(self, previousAction: #selector(self.previousAction(_:)), nextAction: #selector(self.nextAction(_:)), doneAction: #selector(self.doneAction(_:)), shouldShowPlaceholder: true)
        textField2.setEnablePrevious(true, isNextEnabled: false)

        textView3.addPreviousNextDoneOnKeyboardWithTarget(self, previousAction: #selector(self.previousAction(_:)), nextAction: #selector(self.nextAction(_:)), doneAction: #selector(self.doneAction(_:)), shouldShowPlaceholder: true)

        textField4.setTitleTarget(self, action: #selector(self.titleAction(_:)))
        textField4.placeholderText = "Saved Passwords"
        textField4.addDoneOnKeyboardWithTarget(self, action: #selector(self.doneAction(_:)), shouldShowPlaceholder: true)

        textField5.inputAccessoryView = UIView()
    }

    
    func previousAction(_ sender : UITextField!) {
        
        if (textField2.isFirstResponder)
        {
            textView3.becomeFirstResponder()
        }
        else if (textView3.isFirstResponder)
        {
            textField1.becomeFirstResponder()
        }
    }
    
    func nextAction(_ sender : UITextField!) {
        
        if (textField1.isFirstResponder)
        {
            textView3.becomeFirstResponder()
        }
        else if (textView3.isFirstResponder)
        {
            textField2.becomeFirstResponder()
        }
    }
    
    func doneAction(_ sender : UITextField!) {
        self.view.endEditing(true)
    }

    func titleAction(_ sender : UIButton) {
        
        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        
        alertController.addAction(UIAlertAction(title: "test@example.com", style: .default, handler: { (action : UIAlertAction) in
            self.textField4.text = "test";
        }))
        
        alertController.addAction(UIAlertAction(title: "demo@example.com", style: .default, handler: { (action : UIAlertAction) in
            self.textField4.text = "demo";
        }))
        
        alertController.popoverPresentationController?.sourceView = sender
        self.present(alertController, animated: true, completion: nil)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        if let identifier = segue.identifier {
            
            if identifier == "SettingsNavigationController" {
                
                let controller = segue.destination
                
                controller.modalPresentationStyle = .popover
                controller.popoverPresentationController?.barButtonItem = sender as? UIBarButtonItem
                
                let heightWidth = max(UIScreen.main.bounds.width, UIScreen.main.bounds.height);
                controller.preferredContentSize = CGSize(width: heightWidth, height: heightWidth)
                controller.popoverPresentationController?.delegate = self
            }
        }
    }
    
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
    
    func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController) {
        self.view.endEditing(true)
    }
    
    override var shouldAutorotate : Bool {
        return true
    }
}
