//
//  ThreeHeadlineBodyLink.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 6/4/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


open class ThreeHeadlineBodyLink: View {
    //-------------------------------------------------------
    // MARK: - Outlets
    //-------------------------------------------------------
    
    public let headline1 = Label(fontStyle: .BoldBodySmall)
    public let headline2 = Label(fontStyle: .BoldBodySmall)
    public let headline3 = Label(fontStyle: .BoldBodySmall)
    public let body = Label(fontStyle: .RegularBodySmall)
    public let link = Link()
    
    //------------------------------------------------------
    // MARK: - Lifecycle
    //------------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        
        addSubview(headline1)
        addSubview(headline2)
        addSubview(headline3)
        addSubview(body)
        addSubview(link)
        
        link.titleLabel?.font = Styler.Font.RegularBodySmall.getFont()
        
        headline1.topAnchor.constraint(equalTo: topAnchor).isActive = true
        headline1.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        trailingAnchor.constraint(equalTo: headline1.trailingAnchor).isActive = true
        
        headline2.topAnchor.constraint(equalTo: headline1.bottomAnchor, constant: 5).isActive = true
        headline2.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        trailingAnchor.constraint(equalTo: headline2.trailingAnchor).isActive = true
        
        headline3.topAnchor.constraint(equalTo: headline2.bottomAnchor, constant: 5).isActive = true
        headline3.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        trailingAnchor.constraint(equalTo: headline3.trailingAnchor).isActive = true
        
        body.topAnchor.constraint(equalTo: headline3.bottomAnchor, constant: 5).isActive = true
        body.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        trailingAnchor.constraint(equalTo: body.trailingAnchor).isActive = true
        
        link.topAnchor.constraint(equalTo: body.bottomAnchor, constant: Padding.Two).isActive = true
        link.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        trailingAnchor.constraint(equalTo: link.trailingAnchor).isActive = true
        bottomAnchor.constraint(equalTo: link.bottomAnchor).isActive = true
        
        isAccessibilityElement = true
        accessibilityHint = link.accessibilityHint
        accessibilityTraits = link.accessibilityTraits
        updateAccessibilityLabel()
    }

    override open func reset() {
        super.reset()
        
        headline1.reset()
        headline2.reset()
        headline3.reset()
        body.reset()
        link.reset()
        link.titleLabel?.font = Styler.Font.RegularBodySmall.getFont()
    }
    
    //------------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //------------------------------------------------------
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 320
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ThreeHeadlineBodyLinkModel else { return }
        
        headline1.set(with: model.headline1, delegateObject, additionalData)
        headline2.set(with: model.headline2, delegateObject, additionalData)
        headline3.set(with: model.headline3, delegateObject, additionalData)
        body.set(with: model.body, delegateObject, additionalData)
        link.set(with: model.link, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    //----------------------------------------------------
    // MARK: - Accessibility
    //----------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        var message = ""
        
        if let headline1Text = headline1.text, !headline1Text.isEmpty {
            message += headline1Text + ", "
        }
        
        if let headline2Text = headline2.text, !headline2Text.isEmpty {
            message += headline2Text + ", "
        }
        
        if let headline3Text = headline3.text, !headline3Text.isEmpty {
            message += headline3Text + ", "
        }
        
        if let bodyText = body.text, !bodyText.isEmpty {
            message += bodyText + ", "
        }
        
        if let linkLabel = link.accessibilityLabel, !linkLabel.isEmpty {
            message += linkLabel
        }
        
        accessibilityLabel = message
    }
    
    open override func accessibilityActivate() -> Bool {
        
        return link.accessibilityActivate()
    }
}
