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

import UIKit
import VDS

@objcMembers open class TwoButtonView: VDS.View, VDSMoleculeViewProtocol {
      
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    open var viewModel: TwoButtonViewModel!
    open var delegateObject: MVMCoreUIDelegateObject?
    open var additionalData: [AnyHashable : Any]?
    
    open var primaryButton = PillButton()
    open var secondaryButton = PillButton()
    private var buttonGroup = VDS.ButtonGroup()
    private var heightConstraint: NSLayoutConstraint?
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    open override func setup() {
        super.setup()
        isAccessibilityElement = false
        addSubview(buttonGroup)
        buttonGroup.pinToSuperView()
        buttonGroup.alignment = .center
        buttonGroup.rowQuantityPhone = 2
        buttonGroup.rowQuantityTablet = 2
        heightConstraint = height(constant: VDS.Button.Size.large.height, priority: .required)
    }
        
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func reset() {
        super.reset()
        buttonGroup.reset()
    }
    
    public static 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 func viewModelDidUpdate() {
        var buttons = [PillButton]()
        if let secondaryModel = viewModel.secondaryButton {
            secondaryButton.set(with: secondaryModel, delegateObject, additionalData)
            buttons.append(secondaryButton)
        }
        
        if let primaryModel = viewModel.primaryButton {
            primaryButton.set(with: primaryModel, delegateObject, additionalData)
            buttons.append(primaryButton)
        }

        buttonGroup.childWidth = viewModel.fillContainer ? .percentage(100) : nil
        
        if buttons.count != buttonGroup.buttons.count {
            buttonGroup.buttons = buttons
        }
        
        heightConstraint?.constant = primaryButton.size == .small || secondaryButton.size == .small ? VDS.Button.Size.small.height : VDS.Button.Size.large.height
    }

    //--------------------------------------------------
    // MARK: - MVMCoreUIViewConstrainingProtocol
    //--------------------------------------------------
    
    open func horizontalAlignment() -> UIStackView.Alignment {
        return .center
    }
    
    //--------------------------------------------------
    // MARK: - MVMCoreViewProtocol
    //--------------------------------------------------
    
    public func updateView(_ size: CGFloat) {
        setNeedsUpdate()
    }
}
