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

import Foundation


@objcMembers open class TwoLinkView: View, MVMCoreUIViewConstrainingProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open var leftLink = Link()
    open var rightLink = Link()
    private var stack = UIStackView()
    
    //--------------------------------------------------
    // MARK: - MVMCoreViewProtocol
    //--------------------------------------------------
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        stack.updateView(size)
    }
    
    open override func setupView() {
        super.setupView()
        
        stack.translatesAutoresizingMaskIntoConstraints = false
        addSubview(stack)
        stack.addArrangedSubview(leftLink)
        stack.addArrangedSubview(rightLink)
        NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
        stack.axis = .horizontal
        stack.spacing = 8
    }
    
    //--------------------------------------------------
    // MARK: - Stack Manipulation
    //--------------------------------------------------
    
    public func showRightLink() {
        if !stack.arrangedSubviews.contains(rightLink) {
            stack.addArrangedSubview(rightLink)
            rightLink.isHidden = false
        }
    }
    
    public func showLeftLink() {
        if !stack.arrangedSubviews.contains(leftLink) {
            stack.insertArrangedSubview(leftLink, at: 0)
            leftLink.isHidden = false
        }
    }
    
    public func hideRightLink() {
        if rightLink.superview != nil {
            stack.removeArrangedSubview(rightLink)
            rightLink.isHidden = true
        }
    }
    
    public func hideLeftLink() {
        if leftLink.superview != nil {
            stack.removeArrangedSubview(leftLink)
            leftLink.isHidden = true
        }
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func reset() {
        super.reset()
        stack.reset()
    }
   
    //--------------------------------------------------
    // MARK: - MVMCoreUIViewConstrainingProtocol
    //--------------------------------------------------
    
    open func horizontalAlignment() -> UIStackView.Alignment {
        return .center
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 16
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? TwoLinkViewModel else { return }
        
        if let model = model.leftLink {
            showLeftLink()
            leftLink.set(with: model, delegateObject, additionalData)
        } else {
            hideLeftLink()
        }
        
        if let model = model.rightLink {
            showRightLink()
            rightLink.set(with: model, delegateObject, additionalData)
        } else {
            hideRightLink()
        }
    }
}
