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

import Foundation


@objcMembers open class ListDeviceComplexLinkSmall: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    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 twoLinkView = TwoLinkView()
    public let rightImage = LoadImageView()
    let verticalStack: Stack<StackModel>
    public let stack: Stack<StackModel>

    //------------------------------------------------------
    // MARK: - Initializers
    //------------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        rightImage.addSizeConstraintsForAspectRatio = true
        rightImage.imageView.contentMode = .scaleAspectFit
        rightImage.heightAnchor.constraint(equalToConstant: 71).isActive = true
        rightImage.widthAnchor.constraint(equalToConstant: 71).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: twoLinkView, model: StackItemModel(spacing: 16, horizontalAlignment: .leading))],
                                                      axis: .vertical, spacing: 0)
        
        stack = Stack<StackModel>.createStack(with: [(view: verticalStack, model: StackItemModel(horizontalAlignment: .leading, verticalAlignment: .leading)),
                                                     (view: rightImage, model: StackItemModel(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()
        rightImage.shouldNotifyDelegateOnUpdate = false
        addMolecule(stack)
        stack.restack()
        verticalStack.restack()
        updateAccessibilityLabel()
    }
    
    //------------------------------------------------------
    // 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? ListDeviceComplexLinkSmallModel else { return }
        
        verticalStack.updateContainedMolecules(with: [model.eyebrow, model.headline, model.body, model.body2, model.twoLinkView], delegateObject, additionalData)
        rightImage.set(with: model.image, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 120
    }
    
    open override func reset() {
        super.reset()
        eyebrow.setFontStyle(.RegularMicro)
        headline.setFontStyle(.BoldTitleMedium)
        body.setFontStyle(.RegularBodySmall)
        body2.setFontStyle(.RegularBodySmall)
        eyebrow.textColor = .mvmCoolGray6
    }
    
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        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 rightImageLabel = rightImage.imageView.accessibilityLabel, !rightImageLabel.isEmpty {
            message += rightImageLabel
        }
        
        let leftLinkIsDisplayed = !twoLinkView.leftLink.isHidden
        let rightLinkIsDisplayed = !twoLinkView.rightLink.isHidden
        
        // Both links are displayed
        if leftLinkIsDisplayed && rightLinkIsDisplayed {
            isAccessibilityElement = false
            var views = [UIView]()
            
            if let eyebrowText = eyebrow.text, !eyebrowText.isEmpty {
                views.append(eyebrow)
            }
            
            if let headlineText = headline.text, !headlineText.isEmpty {
                views.append(headline)
            }
            
            if let bodyText = body.text, !bodyText.isEmpty {
                views.append(body)
            }
            
            if let body2Text = body2.text, !body2Text.isEmpty {
                views.append(body2)
            }
            
            views.append(twoLinkView.leftLink)
            views.append(twoLinkView.rightLink)
            
            if let rightImageLabel = rightImage.imageView.accessibilityLabel, !rightImageLabel.isEmpty {
                views.append(rightImage.imageView)
            }
            
            accessibilityElements = views
            return
            
        } else if leftLinkIsDisplayed {
            accessibilityHint = twoLinkView.leftLink.accessibilityHint
            accessibilityTraits = twoLinkView.leftLink.accessibilityTraits
            message += twoLinkView.leftLink.accessibilityLabel ?? ""
            
        } else if rightLinkIsDisplayed {
            accessibilityHint = twoLinkView.rightLink.accessibilityHint
            accessibilityTraits = twoLinkView.rightLink.accessibilityTraits
            message += twoLinkView.rightLink.accessibilityLabel ?? ""
            
        }
        
        isAccessibilityElement = true
        accessibilityLabel = message
    }
    
    open override func accessibilityActivate() -> Bool {
        
        if isAccessibilityElement {
            if !twoLinkView.leftLink.isHidden {
                return twoLinkView.leftLink.accessibilityActivate()
            } else if !twoLinkView.rightLink.isHidden {
                return twoLinkView.rightLink.accessibilityActivate()
            }
        }
        
        return false
    }
}
