//
//  LockUpsPlanNames.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 15/07/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

@objcMembers open class LockUpsPlanNames: View {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    public let headline = Label(fontStyle: .Title2XLarge)
    public let subHeadline = Label(fontStyle: .RegularTitleLarge)
    public let body = Label(fontStyle: .RegularBodySmall)
    public lazy var stack: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [(view: headline, model: StackItemModel()),
                                                    (view: subHeadline, model: StackItemModel(spacing: 16)),
                                                    (view: body, model: StackItemModel(spacing: 8))])
      }()
    
    //-------------------------------------------------------
    // MARK: - Lifecycle
    //-------------------------------------------------------
    open override func setupView() {
        super.setupView()
        addSubview(stack)
        NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
        stack.restack()
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        stack.updateView(size)
    }
    
    //----------------------------------------------------
    // MARK: - Molecule
    //------------------------------------------------------
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model = model as? LockUpsPlanNamesModel else { return }
        headline.set(with: model.headline, delegateObject, additionalData)
        subHeadline.set(with: model.subHeadline, delegateObject, additionalData)
        body.set(with: model.body, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 121
    }
    
    open override func reset() {
        super.reset()
        stack.reset()
        headline.setFontStyle(.Title2XLarge)
        subHeadline.setFontStyle(.RegularTitleLarge)
        body.setFontStyle(.RegularBodySmall)
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    func updateAccessibilityLabel() {
        isAccessibilityElement = true
        var message = ""
        if let headline = headline.text, !headline.isEmpty {
            message += headline + ", "
        }
        if let subHeadline = subHeadline.text, !subHeadline.isEmpty {
            message += subHeadline + ", "
        }
        if let body = body.text, !body.isEmpty {
            message += body
        }
        accessibilityLabel = message
    }
}
