package com.sybotan.android.demo.viewmodel

import android.app.Application
import androidx.lifecycle.viewModelScope
import com.framework.mvvm.model.repository.AdmRepository
import com.google.gson.Gson
import com.sybotan.android.demo.activities.BaseActivity
import com.sybotan.android.demo.bean.model3d.MeshObject
import kotlinx.coroutines.launch
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.android.x.closestDI
import org.kodein.di.instance


/**
 * Created by lihao.
 * Date: 2021/6/9
 */
class Scene3DVM(
    app: Application,
    b: (name: String, params: Any) -> Unit, activity: BaseActivity?,
    private val projectId: String,
    private val buildingId: String,
    private val floorId: String,
) : BaseViewModel(app, b, activity), DIAware {
    private val gson = Gson()

    override val di: DI by closestDI()
    val repo: AdmRepository by instance()


    fun getObject(meshString: String) {
        viewModelScope.launch {
            val meshObject = gson.fromJson(meshString, MeshObject::class.java)
            val equip = repo.getObjectByBimId(meshObject.ID)
            mEmitter.SendDirective(EQUIP_OBJECT, equip)
        }
    }

    fun getFloorSpace() {
        viewModelScope.launch {
            val spaces = repo.getSpaceByFloorId(floorId)
            val floor = repo.getObject(floorId)
            mEmitter.SendDirective(FLOOR_OBJECT, floor)
            mEmitter.SendDirective(FLOOR_SPACE, spaces)
        }
    }

    companion object {
        const val EQUIP_OBJECT = "EQUIP_OBJECT"
        const val FLOOR_OBJECT = "FLOOR_OBJECT"
        const val FLOOR_SPACE = "FLOOR_SPACE"
    }
}