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


@objcMembers open class ListLeftVariableCheckboxBodyText: TableViewCell {
    //-----------------------------------------------------
    // MARK: - Outlets
    //-----------------------------------------------------
    
    public let checkbox = Checkbox()
    public var headlineBody = HeadlineBody()
    
    //-----------------------------------------------------
    // MARK: - Properties
    //-----------------------------------------------------
    
    public var stack: Stack<StackModel>
    
    private var observation: NSKeyValueObservation? = nil
    
    //-----------------------------------------------------
    // MARK: - Initializers
    //-----------------------------------------------------
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stack = Stack<StackModel>.createStack(with: [(view: checkbox, model: StackItemModel(horizontalAlignment: .fill)),
                                                     (view: headlineBody, model: StackItemModel(horizontalAlignment: .leading))],
                                              axis: .horizontal)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    //-----------------------------------------------------
    // MARK: - Lifecycle
    //-----------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        addMolecule(stack)
        stack.restack()
        isAccessibilityElement = true
        accessibilityHint = checkbox.accessibilityHint
        accessibilityTraits = checkbox.accessibilityTraits
        // Update accessibility label on checkbox state change.
        observation = observe(\.checkbox.isSelected, options: [.new]) { [weak self] _, _ in
            self?.updateAccessibilityLabel()
        }
        
        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? ListLeftVariableCheckboxBodyTextModel else { return }
        
        checkbox.set(with: model.checkbox, delegateObject, additionalData)
        headlineBody.set(with: model.headlineBody, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 90
    }
    
    public override func didSelectCell(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
        checkbox.isSelected.toggle()
    }
    
    //----------------------------------------------------
    // MARK: - Accessibility
    //----------------------------------------------------
    
    func updateAccessibilityLabel() {
        
        var message = ""
        
        checkbox.updateAccessibilityLabel()
        
        if let checkboxLabel = checkbox.accessibilityLabel {
            message += checkboxLabel + ", "
        }
        
        if let headlineLabel = headlineBody.headlineLabel.text {
            message += headlineLabel + ", "
        }
        
        if let messageLabel = headlineBody.messageLabel.text {
            message += messageLabel
        }
        
        accessibilityLabel = message
    }
}
