//
//  TextInputViewController.swift
//  ABC Bluetooth Demo
//
//  Created by AB Circle on 3/11/2020.
//  Copyright © 2020-2024 AB Circle. All rights reserved.
//

import UIKit

/// Delegate protocol for delegate classes to implement
protocol TextInputViewControllerProtocol {
    
    /// Call back function definition to implement for handling the closing of the text input view
    /// and retrieving the inputted values.
    ///
    /// - Parameters:
    ///   - textViewContent: Content of the input textview
    ///   - isApdu: Flag indicating if APDU or Escape Command was handled
    func closePopup(textViewContent: String, isApdu: Bool)
}

/// Controller for handling the APDU / Escape Command input
class TextInputViewController: UIViewController {
    
    /// :nodoc: View identifier
    static let identifier   = "TextInputViewController"
    
    /// Delegate, set this to the class acting as delegate for this class using the TextInputViewControllerProtocol
    var delegate: TextInputViewControllerProtocol?
    
    /// Flag indicating what the view is handling (APDU or Escape Command)
    var isApdu  : Bool = true

    /// Control outlet for main dialog box UIView
    @IBOutlet weak var dialogBoxView: UIView!
    /// Control outlet for input HexEditUITextView
    @IBOutlet weak var textView: HexEditUITextView!
    /// Control outlet for OK UIButton
    @IBOutlet weak var buttonOK: UIButton!

    /// Called after the controller's view is loaded into memory.
    /// Initialises the local variables, the controls and starts the terminal list updater
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //adding an overlay to the view to give focus to the dialog box
        view.backgroundColor = UIColor.black.withAlphaComponent(0.50)
        
        //customizing the dialog box view
        dialogBoxView.layer.cornerRadius = 6.0
        dialogBoxView.layer.borderWidth = 1.2
        dialogBoxView.layer.borderColor = UIColor(named: "dialogBoxGray")?.cgColor

        initialiseTextView(textView: textView)
    }
    
    /// Static function to create and show this view as a popup.
    ///
    /// - Parameters:
    ///   - parentVC: Parent view
    ///   - textViewContent: Content to show on the textview
    ///   - isApdu: Flag indicating if APDU is being handles or Escape Command
    static func showPopup(parentVC: UIViewController, textViewContent: String, isApdu: Bool) {
        //creating a reference for the dialogView controller
        if let popupViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TextInputViewController") as? TextInputViewController {
            popupViewController.modalPresentationStyle = .custom
            popupViewController.modalTransitionStyle = .crossDissolve
            
            //setting the delegate of the dialog box to the parent viewController
            popupViewController.delegate        = parentVC as? TextInputViewControllerProtocol
            //setting initial properties
            popupViewController.isApdu          = isApdu
            //presenting the pop up viewController from the parent viewController
            parentVC.present(popupViewController, animated: true)
            popupViewController.textView?.text   = textViewContent
        }
    }
    
    /// OK button callback. Calls the delegate closePopup function to export the contents and closes the popup
    ///
    /// - Parameter sender: UI control
    @IBAction func onOKClicked(_ sender: UIButton) {
        delegate?.closePopup(textViewContent: textView.text, isApdu: isApdu)
        closePopup()
    }
    
    /// Close button callback. Closes the popup.
    ///
    /// - Parameter sender: UI control
    @IBAction func onCloseClicked(_ sender: UIButton) {
        closePopup()
    }
    
    /// Initialises the entered textview
    /// 
    /// - Parameter textView: textview to initialise
    private func initialiseTextView(textView: UITextView)
    {
        //textView.delegate               = self
        textView.autocapitalizationType = .allCharacters
        textView.text                   = ""
        textView.layer.borderWidth      = 1.0
        textView.layer.cornerRadius     = 8
        textView.layer.borderColor      =  UIColor.lightGray.cgColor
    }
    
    /// Closes the popup.
    private func closePopup() {
        self.dismiss(animated: true, completion: nil)
    }
}
