//
//  ListDeviceComplexButtonMedium.swift
//  MVMCoreUI
//
//  Created by Dhamodaram Nandi on 21/04/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers open class ListDeviceComplexButtonMedium: TableViewCell {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var verticalStack: Stack<StackModel>
    public let eyebrow = Label(fontStyle: .RegularMicro)
    public let headline = Label(fontStyle: .BoldTitleMedium)
    public let body = Label(fontStyle: .RegularBodySmall)
    public let body2 = Label(fontStyle: .RegularBodySmall)
    public let button = PillButton(frame: .zero)
    public let rightImageView = LoadImageView()
    public var stack: Stack<StackModel>
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        rightImageView.addSizeConstraintsForAspectRatio = true
        rightImageView.imageView.contentMode = .scaleAspectFit
        rightImageView.heightAnchor.constraint(equalToConstant: 116.0).isActive = true
        rightImageView.widthAnchor.constraint(equalToConstant: 116.0).isActive = true
        verticalStack = Stack<StackModel>.createStack(with: [(view: eyebrow, model: StackItemModel(horizontalAlignment: .leading)),
                                                             (view: headline, model: StackItemModel(horizontalAlignment: .leading)),
                                                             (view: body, model: StackItemModel(horizontalAlignment: .leading)),
                                                             (view: body2, model: StackItemModel(horizontalAlignment: .leading)),
                                                             (view: button, model: StackItemModel(spacing: 16, horizontalAlignment: .leading))],
                                                      axis: .vertical, spacing: 0)
        stack = Stack<StackModel>.createStack(with: [(view: verticalStack, model: StackItemModel(horizontalAlignment: .leading, verticalAlignment: .leading)),
                                                     (view: rightImageView, model: StackItemModel(horizontalAlignment: .fill, verticalAlignment: .center))],
                                              axis: .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 setupView() {
        super.setupView()
        rightImageView.shouldNotifyDelegateOnUpdate = false
        addMolecule(stack)
        stack.restack()
        verticalStack.restack()
        accessibilityHint = button.accessibilityHint
        accessibilityTraits = button.accessibilityTraits
    }
    
    //--------------------------------------------------
    // 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? ListDeviceComplexButtonMediumModel else { return }
        
        verticalStack.updateContainedMolecules(with: [model.eyebrow, model.headline, model.body, model.body2, model.button],
                                               delegateObject, additionalData)
        rightImageView.set(with: model.image, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 120
    }
    
    public func setDefault() {
        eyebrow.setFontStyle(.RegularMicro)
        headline.setFontStyle(.BoldTitleMedium)
        body.setFontStyle(.RegularBodySmall)
        body2.setFontStyle(.RegularBodySmall)
        eyebrow.textColor = .mvmCoolGray6
    }
    
    open override func reset() {
        super.reset()
        setDefault()
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func getAccessibilityMessage() -> String? {
        var message = ""
        if let eyebrowText = eyebrow.text, !eyebrowText.isEmpty {
            message += eyebrowText + ", "
        }
        
        if let headlineText = headline.text, !headlineText.isEmpty {
            message += headlineText + ", "
        }
        
        if let bodyText = body.text, !bodyText.isEmpty {
            message += bodyText + ", "
        }
        
        if let body2Text = body2.text, !body2Text.isEmpty {
            message += body2Text + ", "
        }
        
        if let rightImageViewText = rightImageView.imageView.accessibilityLabel, !rightImageViewText.isEmpty {
            message += rightImageViewText
        }
        return message.count > 0 ? message : nil
    }
    
    func updateAccessibilityLabel() {
        if let accessoryView = accessoryView {
            // Both caret and button. Read all content on caret.
            isAccessibilityElement = false
            accessoryView.accessibilityLabel = getAccessibilityMessage()
            accessibilityElements = [accessoryView, button]
        } else {
            // Make whole cell focusable if no action.
            isAccessibilityElement = true
            if let message = getAccessibilityMessage(),
                let buttonTitle = button.titleLabel?.text {
                accessibilityLabel = message + ", " + buttonTitle
            } else {
                accessibilityLabel = getAccessibilityMessage()
            }
        }
    }
    
    open override func accessibilityActivate() -> Bool {
        return button.accessibilityActivate()
    }
}
