package com.sybotan.android.demo.viewmodel

import android.app.Activity
import android.app.Application
import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.viewModelScope
import com.framework.mvvm.model.db.entity.task.*
import com.framework.mvvm.model.repository.AdmRepository
import com.google.gson.Gson
import com.sybotan.android.demo.activities.BaseActivity
import com.sybotan.android.demo.retrofit.BaseViewModelInterface
import com.sybotan.android.demo.tools.ToastUtils
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 SpaceDeliveryCodeVM(
    app: Application,
    b: BaseViewModelInterface?, activity: BaseActivity?, //建筑id
    private val buildingId: String, //楼层id
    private val floorId: String,
    private val projectId: String
) : BaseViewModel(app, b, activity), DIAware {
    private val gson = Gson()

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


    fun getFloor() {
        viewModelScope.launch {
            val floorEntity = repo.getObject(floorId)
            mEmitter.SendDirective(FLOOR_INFO, floorEntity)

            val floors = repo.getSpaceByFloorId(floorId)
            var maxSerialNumber = 1
            for (floor in floors) {
                val info = gson.fromJson(floor.infos, InfosforFloor::class.java)
                if (!TextUtils.isEmpty(info.digitalDeliveryID)) {
                    val serialNumber = info.digitalDeliveryID.substringAfterLast("-RM").toInt()
                    if (serialNumber > maxSerialNumber) {
                        maxSerialNumber = serialNumber
                    }
                }
            }
            mEmitter.SendDirective(SERIAL_NUMBER, (++maxSerialNumber).toString())
            mEmitter.SendDirective(FLOOR_INFO, floorEntity)
        }
    }

    fun getSpace(spaceId: String) {
        viewModelScope.launch {
            val spaceEntity = repo.getObject(spaceId)
            val info = gson.fromJson(spaceEntity!!.infos, InfosforSpace::class.java)
            mEmitter.SendDirective(SPACE_DELIVER_CODE, info.digitalDeliveryID)
        }
    }

    fun insSerialNumber(spaceId: String, content1: String, content2: String) {
        viewModelScope.launch {
            val spaces = repo.getObjectByObjType("space")
            for (space in spaces) {
                if (space.infos != null && space.infos!!.asJsonObject.keySet().isNotEmpty()) {
                    val hashMap = gson.fromJson(space.infos, HashMap::class.java) as HashMap<*, *>
                    if (hashMap != null && hashMap["digitalDeliveryID"] != null && !TextUtils.isEmpty(hashMap["digitalDeliveryID"].toString())) {
                        if (hashMap["digitalDeliveryID"].toString() == "$content1-RM$content2" && space.id != spaceId) {
                            ToastUtils.showMyToast("数字化交付编码重复！")
                            return@launch
                        }
                    }
                }
            }
            val spaceEntity = repo.getObject(spaceId)
            if (spaceEntity != null) {
                val hashMap = gson.fromJson(spaceEntity.infos, HashMap::class.java) as HashMap<String, Any>
                hashMap["digitalDeliveryID"] = "$content1-RM$content2"
                spaceEntity.infos = gson.toJsonTree(hashMap)
                repo.insObject(spaceEntity)
            }
            val intent = Intent()
            intent.putExtra("digitalDeliveryID", "$content1-RM$content2")
            mActivity.setResult(Activity.RESULT_OK, intent)
            mActivity.finish()
        }
    }


    companion object {
        //楼层
        const val FLOOR_INFO = "FLOOR_INFO"

        //流水号
        const val SERIAL_NUMBER = "SERIAL_NUMBER"

        //空间数字化编码
        const val SPACE_DELIVER_CODE = "SPACE_DELIVER_CODE"

    }
}