//
//  AccordionMoleculeTableViewCell.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 8/30/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


@objcMembers public class AccordionMoleculeTableViewCell: MoleculeTableViewCell {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    var accordionListItemModel: AccordionListItemModel? {
        listItemModel as? AccordionListItemModel
    }
    
    let accordionButton: Button = {
        let accordionButton = Button(type: .custom)
        accordionButton.setTitle("+", for: .normal)
        accordionButton.setTitleColor(.black, for: .normal)
        accordionButton.titleLabel?.font = UIFont.systemFont(ofSize: 40, weight: .ultraLight)
        accordionButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        accordionButton.translatesAutoresizingMaskIntoConstraints = true
        return accordionButton
    }()
    
    //--------------------------------------------------
    // MARK: - Setup
    //--------------------------------------------------
    
    override public func setupView() {
        customAccessoryView = true
        super.setupView()
        accessoryView = accordionButton
        accessoryView?.isUserInteractionEnabled = false
    }
    
    override public func didSelectCell(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
        
        accordionButton.isSelected.toggle()
        accordionButton.setTitle(accordionButton.isSelected ? "-" : "+", for: .normal)
        
        guard let model = accordionListItemModel else { return }
        model.selected = accordionButton.isSelected
        
        if accordionButton.isSelected {
            MVMCoreActionHandler.shared()?.asyncHandleAction(with: AddMoleculesActionModel(.automatic), additionalData: [KeySourceModel: model], delegateObject: delegateObject)
        } else {
            MVMCoreActionHandler.shared()?.asyncHandleAction(with: RemoveMoleculesActionModel(.automatic), additionalData: [KeySourceModel: model], delegateObject: delegateObject)
        }
        
        if (accordionListItemModel?.hideLineWhenExpanded ?? false) && (self.bottomSeparatorView?.shouldBeVisible() ?? false) {
            bottomSeparatorView?.isHidden = accordionButton.isSelected
        }
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model = model as? AccordionListItemModel else { return }
        accordionButton.isSelected = model.selected
        accordionButton.setTitle(accordionButton.isSelected ? "-" : "+", for: .normal)
        
        if (accordionListItemModel?.hideLineWhenExpanded ?? false) && (self.bottomSeparatorView?.shouldBeVisible() ?? false) {
            bottomSeparatorView?.isHidden = accordionButton.isSelected
        }
    }
}
