package com.sybotan.android.demo.viewmodel

import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.viewModelScope
import com.framework.mvvm.model.db.entity.task.InfosforBuild
import com.framework.mvvm.model.repository.AdmRepository
import com.google.gson.Gson
import com.sybotan.android.demo.activities.BaseActivity
import com.sybotan.android.demo.entitiies.*
import com.sybotan.android.demo.retrofit.BaseViewModelInterface
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

class BuildVM(app: Application, b: BaseViewModelInterface, a: BaseActivity) :
    BaseViewModel(app, b, a), DIAware {
    companion object {
        const val BUILD_TYPE = "BUILD_TYPE"
    }

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

    fun getBuilds() {
        viewModelScope.launch {
            val builds = repo.getBuildByObjType("building")
            var t = ArrayList<Build>()
            for (build in builds) {
                val floors = repo.getFloorByBuildingId(build.id, "floor")
                var floorList = ArrayList<Floor>()
                for (floor in floors) {
                    val infos = Gson().fromJson(floor.infos, InfosforBuild::class.java)
                    var floorMap = ""
                    if (TextUtils.isEmpty(infos.floorMap)) {
                        floorMap = ""
                    } else {
                        floorMap = infos.floorMap
                    }
                    var localId = ""
                    if (TextUtils.isEmpty(floor.localId)) {
                        localId = ""
                    } else {
                        localId = floor.localId!!
                    }
                    floorList.add(
                        Floor(
                            bimIdPre = "",
                            buildingId = floor.buildingId!!,
                            count = 0.0,
                            createTime = "",
                            floorSequenceID = infos.floorSequenceID,
                            id = floor.id,
                            lastUpdate = "",
                            localId = localId,
                            localName = floor.localName!!,
                            modelId = "",
                            name = floor.name ?: "",
                            objectType = "floor",
                            projectId = floor.projectId!!,
                            statistics = Statistics(),
                            infos = Info("", floorMap)
                        )
                    )
                }
                floorList.sortByDescending { floor -> floor.floorSequenceID }
                t.add(
                    Build(
                        createTime = "",
                        id = build.id,
                        localId = build.localId!!,
                        lastUpdate = "",
                        localName = build.localName!!,
                        name = build.name ?: "",
                        objectType = build.objType ?: "",
                        projectId = build.projectId!!,
                        statistics = StatisticsX(),
                        floor = floorList
                    )
                )
            }
            mEmitter.SendDirective(BUILD_TYPE, t)
        }

//        val observable = RetrofitFactory.getInstance().requestBuildJob()
//        this.sendRequest(observable, object : BaseObserver<List<Build>>(mActivity, this) {
//            override fun onSuccess(t: List<Build>?) {
//                mEmitter.SendDirective(BUILD_TYPE, t)
//            }
//
//        }, true)
    }
}