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

import UIKit

@objcMembers open class HeadlineBodyToggle: View {
    public let headlineBody = HeadlineBody(frame: .zero)
    public let toggle = Toggle()
    
    // MARK: - MVMCoreViewProtocol
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        headlineBody.updateView(size)
        toggle.updateView(size)
    }
    
    open override func setupView() {
        super.setupView()
        guard toggle.superview == nil else {
            return
        }
        headlineBody.styleListItem()
        let view = MVMCoreUICommonViewsUtility.commonView()
        addSubview(view)
        NSLayoutConstraint.constraintPinSubview(toSuperview: view)
        
        view.addSubview(headlineBody)
        view.addSubview(toggle)
        NSLayoutConstraint.pinViews(leftView: headlineBody, rightView: toggle, alignTop: false)
    }
    
    // MARK:- MoleculeViewProtocol
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let headlineBodyToggleModel = model as? HeadlineBodyToggleModel else { return }
        headlineBody.set(with: headlineBodyToggleModel.headlineBody, delegateObject, additionalData)
        toggle.set(with: headlineBodyToggleModel.toggle, delegateObject, additionalData)
    }
    
    open class override func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        guard let model = model as? HeadlineBodyToggleModel,
            let toggleHeight = Toggle.estimatedHeight(with: model.toggle, delegateObject),
            let headlineBody = HeadlineBody.estimatedHeight(with: model.headlineBody, delegateObject) else { return nil }
        return max(toggleHeight, headlineBody)
    }
    
    // MARK: - MoleculeViewProtocol
    open override func reset() {
        super.reset()
        headlineBody.reset()
        toggle.reset()
        headlineBody.styleListItem()
    }
}
