//
//  Tilet.swift
//  MVMCoreUI
//
//  Created by Matt Bruce on 1/12/23.
//  Copyright © 2023 Verizon Wireless. All rights reserved.
//

import Foundation
import MVMCore
import VDS
import Combine

/**
 This class expects its height and width to be equal.
 */
open class Tilelet: VDS.Tilelet, VDSMoleculeViewProtocol{
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var model: MoleculeModelProtocol?
    
    public var viewModel: TileletModel! {
        get { model as? TileletModel }
        set { model = newValue }
    }
    public var delegateObject: MVMCoreUIDelegateObject?
    public var additionalData: [AnyHashable: Any]?
        
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    public convenience required init() {
        self.init(frame: .zero)
    }
    
    //--------------------------------------------------
    // MARK: - Public
    //--------------------------------------------------
    public func viewModelDidUpdate() {
        color = viewModel.color
        padding = viewModel.padding
        aspectRatio = viewModel.aspectRatio
        width = viewModel.width
        if let value = viewModel.textWidth {
            textWidth = .value(value)
        } else if let percentage = viewModel.textPercentage {
            textWidth = .percentage(percentage)
        }
        titleModel = viewModel.titleModel(delegateObject: delegateObject, additionalData: additionalData)
        subTitleModel = viewModel.subTitleModel(delegateObject: delegateObject, additionalData: additionalData)
        badgeModel = viewModel.badge
        descriptiveIconModel = viewModel.descriptiveIcon
        directionalIconModel = viewModel.directionalIcon
        //setup action
        if let action = viewModel.action {
            //add the subscriber
            onClick = { [weak self] control in
                guard let self, let viewModel = self.viewModel else { return }
                MVMCoreUIActionHandler.performActionUnstructured(with: action,
                                                                 sourceModel: viewModel,
                                                                 additionalData: self.additionalData,
                                                                 delegateObject: self.delegateObject)
            }
        }
    }

    //--------------------------------------------------
    // MARK: - MVMCoreViewProtocol
    //--------------------------------------------------
    open func updateView(_ size: CGFloat) {}

    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    //since this is a class func, we can't reference it directly
    public static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        100
    }

    //--------------------------------------------------
    // MARK: - Overrides
    //--------------------------------------------------
    open override func layoutSubviews() {
        super.layoutSubviews()
        // Accounts for any collection size changes
        DispatchQueue.main.async { [weak self] in
            guard let self else { return }
            self.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated(self)
        }
    }
}
