//
//  HeadlineBody.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 7/1/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


open class HeadlineBody: View {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    let headlineLabel = Label(fontStyle: .BoldTitleLarge)
    public let messageLabel = Label(fontStyle: .RegularBodySmall)
    
    //--------------------------------------------------
    // MARK: - Constraints
    //--------------------------------------------------
    
    var spaceBetweenLabelsConstant = Padding.One
    var spaceBetweenLabels: NSLayoutConstraint?
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    func hasText() -> Bool {
        headlineLabel.hasText || messageLabel.hasText
    }
    
    //--------------------------------------------------
    // MARK: - Styling
    //--------------------------------------------------

    func style(with style: HeadlineBodyModel.Style?) {
        
        switch style {
        case .landingHeader:
            styleLandingPageHeader()
            
        case .header:
            stylePageHeader()
            
        case .item:
            styleListItem()
            
        case .itemHeader:
            styleListItemDivider()
            
        default: break
        }
    }
    
    public func styleLandingPageHeader() {
        headlineLabel.setFontStyle(.Title2XLarge)
        messageLabel.setFontStyle(.RegularBodySmall)
        spaceBetweenLabelsConstant = Padding.Two
    }
    
    public func stylePageHeader() {
        headlineLabel.setFontStyle(.BoldTitleLarge)
        messageLabel.setFontStyle(.RegularBodySmall)
        spaceBetweenLabelsConstant = Padding.One
    }
    
    public func styleListItem() {
        headlineLabel.setFontStyle(.BoldBodySmall)
        messageLabel.setFontStyle(.RegularBodySmall)
        spaceBetweenLabelsConstant = 0
    }
    
    public func styleListItemDivider() {
        headlineLabel.setFontStyle(.BoldTitleMedium)
        messageLabel.setFontStyle(.RegularBodySmall)
        spaceBetweenLabelsConstant = 0
    }
    
    //--------------------------------------------------
    // MARK: - MVMCoreViewProtocol
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        backgroundColor = .clear
        isAccessibilityElement = false
        shouldGroupAccessibilityChildren = true
        accessibilityElements = [headlineLabel, messageLabel]
        
        addSubview(headlineLabel)
        addSubview(messageLabel)
        
        headlineLabel.setContentCompressionResistancePriority(.required, for: .vertical)
        headlineLabel.setContentHuggingPriority(.required, for: .vertical)
        messageLabel.setContentCompressionResistancePriority(.required, for: .vertical)
        messageLabel.setContentHuggingPriority(.required, for: .vertical)
        
        headlineLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
        
        spaceBetweenLabels = messageLabel.topAnchor.constraint(equalTo: headlineLabel.bottomAnchor, constant: spaceBetweenLabelsConstant)
        spaceBetweenLabels?.isActive = true
        
        NSLayoutConstraint.activate([
            headlineLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
            trailingAnchor.constraint(equalTo: headlineLabel.trailingAnchor),
            messageLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
            trailingAnchor.constraint(equalTo: messageLabel.trailingAnchor),
            bottomAnchor.constraint(equalTo: messageLabel.bottomAnchor)
        ])
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        setSpacing()
        headlineLabel.updateView(size)
        messageLabel.updateView(size)
    }
    
    //--------------------------------------------------
    // MARK: - Constraining
    //--------------------------------------------------
    
    public func setSpacing() {
        if headlineLabel.hasText && messageLabel.hasText {
            spaceBetweenLabels?.constant = spaceBetweenLabelsConstant
        } else {
            spaceBetweenLabels?.constant = 0
        }
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol,
                                               _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { 58 }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? HeadlineBodyModel else { return }
        
        style(with: model.style)
        
        headlineLabel.setOptional(with: model.headline, delegateObject, additionalData)
        messageLabel.setOptional(with: model.body, delegateObject, additionalData)
    }
    
    open override func reset() {
        super.reset()
        stylePageHeader()
    }
}
