//
//  ListThreeColumnSpeedTest.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 04/08/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class ListThreeColumnSpeedTest: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-------------------------------------------------------
    
    public var title = Label(fontStyle: .RegularMicro)
    public var topLeftHeadlineBody = HeadlineBody()
    public var topCenterHeadlineBody = HeadlineBody()
    public var topRightHeadlineBody = HeadlineBody()
    public var topHorizontalStack: Stack<StackModel>
    public var bottomLeftHeadlineBody = HeadlineBody()
    public var bottomCenterHeadlineBody = HeadlineBody()
    public var bottomRightHeadlineBody = HeadlineBody()
    public var bottomHorizontalStack: Stack<StackModel>
    public var stack: Stack<StackModel>
    
    //------------------------------------------------------
    // MARK: - Initializers
    //------------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        topHorizontalStack = Stack<StackModel>.createStack(with: [(view: topLeftHeadlineBody, model: StackItemModel(percent: 37, verticalAlignment: .top)), (view: topCenterHeadlineBody, model: StackItemModel(percent: 33, verticalAlignment: .top)), (view: topRightHeadlineBody, model: StackItemModel(percent: 30, verticalAlignment: .top))], axis: .horizontal)
        bottomHorizontalStack = Stack<StackModel>.createStack(with: [(view: bottomLeftHeadlineBody, model: StackItemModel(percent: 37, verticalAlignment: .top)), (view: bottomCenterHeadlineBody, model: StackItemModel(percent: 33, verticalAlignment: .top)), (view: bottomRightHeadlineBody, model: StackItemModel(percent: 30, verticalAlignment: .top))], axis: .horizontal)
        stack = Stack<StackModel>.createStack(with: [(view: title, model: StackItemModel()), (view: topHorizontalStack, model: StackItemModel(spacing: 2)), (view: bottomHorizontalStack, model: StackItemModel())], axis: .vertical)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //------------------------------------------------------
    // MARK: - Lifecycle
    //------------------------------------------------------
    open override func setupView() {
        super.setupView()
        addMolecule(stack)
        stack.restack()
        topHorizontalStack.restack()
        bottomHorizontalStack.restack()
    }
    
    //--------------------------------------------------
    // MARK: - ModelMoleculeViewProtocol
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ListThreeColumnSpeedTestModel else { return }
        
        title.set(with: model.title, delegateObject, additionalData)
        topHorizontalStack.updateContainedMolecules(with: [model.topLeftHeadlineBody, model.topCenterHeadlineBody, model.topRightHeadlineBody], delegateObject, additionalData)
        bottomHorizontalStack.updateContainedMolecules(with: [model.bottomLeftHeadlineBody, model.bottomCenterHeadlineBody, model.bottomRightHeadlineBody], delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 121
    }
    
    open override func reset() {
        super.reset()
        title.setFontStyle(.BoldBodySmall)
        topLeftHeadlineBody.spaceBetweenLabelsConstant = 2
        topCenterHeadlineBody.spaceBetweenLabelsConstant = 2
        topRightHeadlineBody.spaceBetweenLabelsConstant = 2
        bottomLeftHeadlineBody.spaceBetweenLabelsConstant = 2
        bottomCenterHeadlineBody.spaceBetweenLabelsConstant = 2
        bottomRightHeadlineBody.spaceBetweenLabelsConstant = 2
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        var message = ""
        
        if let title = title.text, !title.isEmpty {
            message += title + ", "
        }
        
        if let topLeftHeadlineBodyText = accessibilityTextOfHeadlineBody(topLeftHeadlineBody), !topLeftHeadlineBodyText.isEmpty {
            message += topLeftHeadlineBodyText + ", "
        }
        
        if let topCenterHeadlineBodyText = accessibilityTextOfHeadlineBody(topCenterHeadlineBody), !topCenterHeadlineBodyText.isEmpty {
            message += topCenterHeadlineBodyText + ", "
        }
        
        if let topRightHeadlineBodyText = accessibilityTextOfHeadlineBody(topRightHeadlineBody), !topRightHeadlineBodyText.isEmpty {
            message += topRightHeadlineBodyText + ", "
        }
        
        if let bottomLeftHeadlineBodyText = accessibilityTextOfHeadlineBody(bottomLeftHeadlineBody), !bottomLeftHeadlineBodyText.isEmpty {
            message += bottomLeftHeadlineBodyText + ", "
        }
        
        if let bottomCenterHeadlineBodyText = accessibilityTextOfHeadlineBody(bottomCenterHeadlineBody), !bottomCenterHeadlineBodyText.isEmpty {
            message += bottomCenterHeadlineBodyText + ", "
        }
        
        if let bottomRightHeadlineBodyText = accessibilityTextOfHeadlineBody(bottomRightHeadlineBody), !bottomRightHeadlineBodyText.isEmpty {
            message += bottomRightHeadlineBodyText
        }
        
        accessibilityLabel = message
    }
    
    func accessibilityTextOfHeadlineBody(_ headlineBody: HeadlineBody) -> String? {
        
        var message = ""
        
        if let headlineLabel = headlineBody.headlineLabel.text {
            message += headlineLabel + ", "
        }
        
        if let messageLabel = headlineBody.messageLabel.text {
            message += messageLabel
        }
        
        return message
    }
}
