//
//  TwoLinkView.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 4/29/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation
import VDS

@objcMembers open class TwoLinkView: VDS.View, VDSMoleculeViewProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    open var viewModel: TwoLinkViewModel!
    open var delegateObject: MVMCoreUIDelegateObject?
    open var additionalData: [AnyHashable : Any]?

    open var leftLink = Link()
    open var rightLink = Link()
    private var buttonGroup = VDS.ButtonGroup()
    private var buttons: [Link] = []
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    open override func setup() {
        super.setup()
        isAccessibilityElement = false
        addSubview(buttonGroup)
        buttonGroup.pinToSuperView()
        buttonGroup.alignment = .center
        buttonGroup.rowQuantityPhone = 2
        buttonGroup.rowQuantityTablet = 2
    }

    //--------------------------------------------------
    // MARK: - MVMCoreViewProtocol
    //--------------------------------------------------
    open override func reset() {
        super.reset()
        buttonGroup.reset()
    }

    open func updateView(_ size: CGFloat) { }

    //--------------------------------------------------
    // MARK: - Stack Manipulation
    //--------------------------------------------------
    
    public func showRightLink() {
        if !buttons.contains(rightLink) {
            buttons.insert(rightLink, at: buttons.count)
        }
        buttonGroup.buttons = buttons
    }
    
    public func showLeftLink() {
        if !buttons.contains(leftLink) {
            buttons.insert(leftLink, at: 0)
        }
        buttonGroup.buttons = buttons
    }
    
    public func hideRightLink() {
        if let index = buttons.firstIndex(of: rightLink) {
            buttons.remove(at: index)
        }
        buttonGroup.buttons = buttons
    }
    
    public func hideLeftLink() {
        if let index = buttons.firstIndex(of: leftLink) {
            buttons.remove(at: index)
        }
        buttonGroup.buttons = buttons
    }
       
    //--------------------------------------------------
    // MARK: - MVMCoreUIViewConstrainingProtocol
    //--------------------------------------------------
    
    open func horizontalAlignment() -> UIStackView.Alignment {
        return .center
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    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() {
        buttons.removeAll()
        
        if let model = viewModel.leftLink {
            leftLink.set(with: model, delegateObject, additionalData)
            buttons.append(leftLink)
        }
        
        if let model = viewModel.rightLink {
            rightLink.set(with: model, delegateObject, additionalData)
            buttons.append(rightLink)
        }
        
        buttonGroup.buttons = buttons
    }

}
