//
//  LabelToggle.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 8/26/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit


@objcMembers open class LabelToggle: View {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------

    public let label = Label(fontStyle: .BoldBodySmall)
    public let toggle = Toggle()
    
    //--------------------------------------------------
    // MARK: - MVMCoreViewProtocol
    //--------------------------------------------------
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        label.updateView(size)
        toggle.updateView(size)
    }
    
    open override func setupView() {
        super.setupView()
        
        addSubview(label)
        addSubview(toggle)
        label.setContentHuggingPriority(.required, for: .vertical)
        NSLayoutConstraint.pinViews(leftView: label, rightView: toggle, alignTop: false)
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        guard let model = model as? LabelToggleModel,
            let toggleHeight = Toggle.estimatedHeight(with: model.toggle, delegateObject),
            let labelHeight = Label.estimatedHeight(with: model.label, delegateObject)
            else { return nil }
        
        return max(toggleHeight, labelHeight)
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        
        guard let labelToggleModel = model as? LabelToggleModel else { return }
        
        label.set(with: labelToggleModel.label, delegateObject, additionalData)
        toggle.set(with: labelToggleModel.toggle, delegateObject, additionalData)
    }
    
    // MARK: - MoleculeViewProtocol
    open override func reset() {
        super.reset()
        label.reset()
        toggle.reset()
        label.setFontStyle(.BoldBodySmall)
    }
}
