//
//  TwoButtonView.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 3/21/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit


@objcMembers open class TwoButtonView: View, MVMCoreUIViewConstrainingProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open var primaryButton: PillButton = PillButton()
    open var secondaryButton: PillButton = PillButton()
    private var stack = UIStackView()
    
    //--------------------------------------------------
    // MARK: - Constraints
    //--------------------------------------------------
    
    private var equalWidthConstraint: NSLayoutConstraint?
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    public func setDefaultAppearance() {
        
        primaryButton.stylePrimary()
        secondaryButton.styleSecondary()
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        
        primaryButton.updateView(size)
        secondaryButton.updateView(size)
    }
    
    open override func setupView() {
        super.setupView()
        
        stack.translatesAutoresizingMaskIntoConstraints = false
        isAccessibilityElement = false
        addSubview(stack)
        stack.addArrangedSubview(secondaryButton)
        stack.addArrangedSubview(primaryButton)
        NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
        stack.axis = .horizontal
        stack.spacing = Padding.Component.gutterForApplicationWidth
        equalWidthConstraint = secondaryButton.widthAnchor.constraint(equalTo: primaryButton.widthAnchor, multiplier: 1)
        equalWidthConstraint?.isActive = true
    }
    
    //--------------------------------------------------
    // MARK: - Stack Manipulation
    //--------------------------------------------------
    
    public func showPrimaryButton() {
        
        if !stack.arrangedSubviews.contains(primaryButton) {
            stack.addArrangedSubview(primaryButton)
            primaryButton.isHidden = false
        }
        
        if secondaryButton.superview != nil {
            equalWidthConstraint?.isActive = true
        }
        
        primaryButton.isAccessibilityElement = true
    }
    
    public func showSecondaryButton() {
        
        if !stack.arrangedSubviews.contains(secondaryButton) {
            stack.insertArrangedSubview(secondaryButton, at: 0)
            secondaryButton.isHidden = false
        }
        
        if primaryButton.superview != nil {
            equalWidthConstraint?.isActive = true
        }
        
        secondaryButton.isAccessibilityElement = true
    }
    
    public func hidePrimaryButton() {
        
        if primaryButton.superview != nil {
            stack.removeArrangedSubview(primaryButton)
            primaryButton.isHidden = true
        }
        
        primaryButton.isAccessibilityElement = false
        equalWidthConstraint?.isActive = false
    }
    
    public func hideSecondaryButton() {
        
        if secondaryButton.superview != nil {
            stack.removeArrangedSubview(secondaryButton)
            secondaryButton.isHidden = true
        }
        
        secondaryButton.isAccessibilityElement = false
        equalWidthConstraint?.isActive = false
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func reset() {
        super.reset()
        
        setDefaultAppearance()
    }
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        
        guard let model = model as? TwoButtonViewModel,
            let buttonModel = model.primaryButton ?? model.secondaryButton
            else { return 0 }
        
        return PillButton.estimatedHeight(with: buttonModel, delegateObject)
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? TwoButtonViewModel else { return }
        
        if let secondaryModel = model.secondaryButton {
            showSecondaryButton()
            secondaryButton.set(with: secondaryModel, delegateObject, additionalData)
        } else {
            hideSecondaryButton()
        }
        
        if let primaryModel = model.primaryButton {
            showPrimaryButton()
            primaryButton.set(with: primaryModel, delegateObject, additionalData)
        } else {
            hidePrimaryButton()
        }
    }
    //--------------------------------------------------
    // MARK: - MVMCoreUIViewConstrainingProtocol
    //--------------------------------------------------
    
    open func horizontalAlignment() -> UIStackView.Alignment {
        return .center
    }
}
