//
//  BaseDropdownField.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 10/23/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit

/**
 This class is intended to be subclassed.
 See ItemDropdownEntryField and DateDropdownEntryField.
 */
@objcMembers open class BaseDropdownEntryField: TextEntryField {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    let dropDownCaretView: CaretView = {
        let caret = CaretView()
        caret.direction = .down
        caret.lineWidth = 1.5
        caret.isUserInteractionEnabled = false
        caret.heightAnchor.constraint(equalToConstant: 9).isActive = true
        caret.widthAnchor.constraint(equalToConstant: 16).isActive = true
        return caret
    }()
    
    public var baseDropdownEntryFieldModel: BaseDropdownEntryFieldModel? {
        model as? BaseDropdownEntryFieldModel
    }
    
    var additionalData: [AnyHashable: Any]?
    
    //--------------------------------------------------
    // MARK: - Property Observers
    //--------------------------------------------------
    
    @objc public override var isEnabled: Bool {
        get { super.isEnabled }
        set (enabled) {
            dropDownCaretView.isEnabled = enabled
            super.isEnabled = enabled
        }
    }

    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    @objc public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    @objc required public init?(coder: NSCoder) {
        super.init(coder: coder)
        fatalError("\(String(describing: Self.self)) does not support xib.")
    }
    
    required public init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.init(model: model, delegateObject, additionalData)
    }
    
    //--------------------------------------------------
    // MARK: - Setup
    //--------------------------------------------------
    
    @objc public override func setupFieldContainerContent(_ container: UIView) {
        super.setupFieldContainerContent(container)
        
        container.addSubview(dropDownCaretView)
        
        textFieldTrailingConstraint?.isActive = false
        textFieldTrailingConstraint = dropDownCaretView.leadingAnchor.constraint(equalTo: textField.trailingAnchor, constant: 6)
        textFieldTrailingConstraint?.isActive = true
        
        container.trailingAnchor.constraint(equalTo: dropDownCaretView.trailingAnchor, constant: Padding.Four).isActive = true
        dropDownCaretView.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        self.additionalData = additionalData
        guard let model = model as? BaseDropdownEntryFieldModel else { return }
        
        dropDownCaretView.setOptional(with: model.caretView, delegateObject, additionalData)
    }
    
    @objc public override func dismissFieldInput(_ sender: Any?) {
        if !textField.isFirstResponder {
            performDropdownAction()
        }
        super.dismissFieldInput(sender)
    }
    
    func performDropdownAction() {
        guard let baseDropdownEntryFieldModel = baseDropdownEntryFieldModel,
              let actionModel = baseDropdownEntryFieldModel.action
        else { return }
        
        let additionalDataWithSource = additionalData.dictionaryAdding(key: KeySourceModel, value: baseDropdownEntryFieldModel)
        MVMCoreActionHandler.shared()?.asyncHandleAction(with: actionModel, additionalData: additionalDataWithSource, delegateObject: delegateObject)
    }
}
