//
//  ListTwoColumnPriceDescription.swift
//  MVMCoreUI
//
//  Created by Kruthika KP on 24/02/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers open class ListTwoColumnPriceDescription: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-------------------------------------------------------
    
    public let leftHeadline = Label(fontStyle: .BoldBodySmall)
    public let leftBody = Label(fontStyle: .RegularBodySmall)
    public let rightLabel = Label(fontStyle: .RegularBodySmall)
    public let rightSubLabel = Label(fontStyle: .RegularBodySmall)
    
    public let view = MVMCoreUICommonViewsUtility.commonView()
    public let leftVerticalStack: UIStackView
    public let rightVerticalStack: UIStackView
    
    //------------------------------------------------------
    // MARK: - Initializers
    //------------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        leftVerticalStack = UIStackView(arrangedSubviews: [leftHeadline, leftBody])
        leftVerticalStack.axis = .vertical
        leftVerticalStack.alignment = .leading
        rightVerticalStack = UIStackView(arrangedSubviews: [rightLabel, rightSubLabel])
        rightVerticalStack.axis = .vertical
        rightVerticalStack.alignment = .trailing
        rightVerticalStack.setContentCompressionResistancePriority(.required, for: .horizontal)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //-----------------------------------------------------
    // MARK: - Lifecycle
    //-----------------------------------------------------
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        leftVerticalStack.updateView(size)
        rightVerticalStack.updateView(size)
        
        // Resolves text layout issues found between both dynamically sized labels, number is not exact but performs as required.
        if leftBody.hasText && (rightLabel.hasText || rightSubLabel.hasText) {
            let padding = MFStyler.defaultHorizontalPadding(forSize: size) * 2
            let maximumTextWidth = (size - (padding + Padding.Four)) * 0.9 // This percent determines available width.
            // Subtracting 10 resolves issues of SE and iPad
            leftBody.preferredMaxLayoutWidth = round(maximumTextWidth) - 10
        } else {
            leftBody.preferredMaxLayoutWidth = 0
        }
    }
    
    override open func setupView() {
        super.setupView()
        
        contentView.addSubview(view)
        containerHelper.constrainView(view)
        
        leftVerticalStack.translatesAutoresizingMaskIntoConstraints = false
        rightVerticalStack.translatesAutoresizingMaskIntoConstraints = false
        rightLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
        rightSubLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
        view.addSubview(leftVerticalStack)
        view.addSubview(rightVerticalStack)
        NSLayoutConstraint.pinViews(leftView: leftVerticalStack, rightView: rightVerticalStack, alignTop: true)
        
    }
    
    //----------------------------------------------------
    // 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? ListTwoColumnPriceDescriptionModel else { return }
        
        leftHeadline.set(with: model.leftHeadline, delegateObject, additionalData)
        leftBody.set(with: model.leftBody, delegateObject, additionalData)
        rightLabel.set(with: model.rightLabel, delegateObject, additionalData)
        rightSubLabel.set(with: model.rightSubLabel, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with molecule: MoleculeModelProtocol?, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 80
    }
    
    override open func reset() {
        super.reset()
        leftVerticalStack.reset()
        rightVerticalStack.reset()
        leftHeadline.setFontStyle(.BoldBodySmall)
        leftBody.setFontStyle(.RegularBodySmall)
        rightLabel.setFontStyle(.RegularBodySmall)
        rightSubLabel.setFontStyle(.RegularBodySmall)
    }
    
    //----------------------------------------------------
    // MARK: - Accessibility
    //----------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        isAccessibilityElement = true
        
        var message = ""
        
        if let leftHeadline = leftHeadline.text, !leftHeadline.isEmpty {
            message += leftHeadline + ", "
        }
        
        if let leftBody = leftBody.text, !leftBody.isEmpty {
            message += leftBody + ", "
        }
        
        if let rightLabel = rightLabel.text, !rightLabel.isEmpty {
            message += rightLabel + ", "
        }
        
        if let rightSubLabel = rightSubLabel.text, !rightSubLabel.isEmpty {
            message += rightSubLabel
        }
        
        accessibilityLabel = message
    }
}
