//
//  BGImageHeadlineBodyButton.swift
//  MVMCoreUI
//
//  Created by Khan, Arshad on 02/04/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers public class BGImageHeadlineBodyButton: Container {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    let headlineBody = HeadlineBody()
    let button = PillButton()
    let backgroundImageView = LoadImageView(pinnedEdges: .all)
    
    //--------------------------------------------------
    // MARK: - Property
    //--------------------------------------------------
    
    let maxWidth: CGFloat = 350
    static let heightConstant: CGFloat = 320
    
    //--------------------------------------------------
    // MARK: - Constraints
    //--------------------------------------------------
    
    var heightConstraint: NSLayoutConstraint?
    
    //--------------------------------------------------
    // MARK: - Setup
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        heightConstraint = heightAnchor.constraint(equalToConstant: Self.heightConstant)
        heightConstraint?.isActive = true
        
        backgroundImageView.addSizeConstraintsForAspectRatio = true
        
        let container = MVMCoreUICommonViewsUtility.commonView()
        addAndContain(container)
        
        container.addSubview(headlineBody)
        container.addSubview(button)
        
        headlineBody.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
        headlineBody.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
        
        let headLineBodyWidth = headlineBody.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.67)
        headLineBodyWidth.priority = UILayoutPriority(rawValue: 999)
        headLineBodyWidth.isActive = true
        headlineBody.widthAnchor.constraint(lessThanOrEqualToConstant: maxWidth).isActive = true
        
        button.translatesAutoresizingMaskIntoConstraints = false
        button.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
        container.bottomAnchor.constraint(greaterThanOrEqualTo: button.bottomAnchor).isActive = true
        button.topAnchor.constraint(equalTo: headlineBody.bottomAnchor, constant: PaddingDefault).isActive = true
        
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        backgroundImageView.imageView.contentMode = .scaleAspectFill
        backgroundImageView.pinEdges(.all)
        addSubview(backgroundImageView)
        NSLayoutConstraint.constraintPinSubview(toSuperview: backgroundImageView)
        sendSubviewToBack(backgroundImageView)
        
        isAccessibilityElement = true
        accessibilityHint = button.accessibilityHint
        accessibilityTraits = button.accessibilityTraits
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        headlineBody.updateView(size)
        button.updateView(size)
        backgroundImageView.updateView(size)
        backgroundImageView.pinEdges(.all)
    }
    
    open override func reset() {
        super.reset()
        headlineBody.reset()
        backgroundImageView.reset()
    }
    
    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? BGImageHeadlineBodyButtonModel else { return }
        
        headlineBody.set(with: model.headlineBody, delegateObject, additionalData)
        button.setOptional(with: model.button, delegateObject, additionalData)
        button.isHidden = model.button == nil
        backgroundImageView.set(with: model.image, delegateObject, additionalData)
        backgroundImageView.pinEdges(.all)
        updateAccessibilityLabel()
    }
    
    //----------------------------------------------------
    // MARK: - Accessibility
    //----------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        var message = ""
        
        if let headline = headlineBody.headlineLabel.text, !headline.isEmpty {
            message += headline + ", "
        }
        
        if let body = headlineBody.messageLabel.text, !body.isEmpty {
            message += body + ", "
        }
        
        if let backgroundImageText = backgroundImageView.accessibilityLabel, !backgroundImageText.isEmpty {
            message += backgroundImageText + ", "
        }
        
        if let buttonLabel = button.accessibilityLabel, !buttonLabel.isEmpty {
            message += buttonLabel
        }
        
        accessibilityLabel = message
    }
    
    open override func accessibilityActivate() -> Bool {
        return button.accessibilityActivate()
    }
}
