package com.sybotan.android.demo.viewmodel

import android.app.Activity
import android.app.Application
import android.content.Intent
import android.os.Bundle
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 com.zbar.lib.CaptureActivity
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 EquipDeliveryCodeVM(
    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 {

    fun getEquip(deviceId: String) {
        viewModelScope.launch {
            val equipEntity = repo.getObject(deviceId)
            mEmitter.SendDirective(EQUIP_ENTITY, equipEntity)
        }
    }

    fun getSpace(spaceId: String) {
        viewModelScope.launch {
            val spaceEntity = repo.getObject(spaceId)
            mEmitter.SendDirective(SPACE_ENTITY, spaceEntity)
        }
    }

    fun insSerialNumber(deviceId: String, content1: String, content2: String, content3: String, content4: String) {
        viewModelScope.launch {

            val equips = repo.getObjectByObjType("equipment")
            for (equip in equips) {
                if (equip.infos != null && !equip.infos!!.asJsonObject.keySet().isEmpty()) {
                    val hashMap = gson.fromJson(equip.infos, HashMap::class.java) as HashMap<String, Any>
                    if (hashMap != null && hashMap["digitalDeliveryID"] != null && !TextUtils.isEmpty(hashMap["digitalDeliveryID"].toString())) {
                        if (hashMap["digitalDeliveryID"].toString() == "$content1-$content2-$content3-$content4" && equip.id != deviceId) {
                            ToastUtils.showMyToast("数字化交付编码重复！")
                            return@launch
                        }
                    }
                }
            }

            val equipEntity = repo.getObject(deviceId)
            val code = "$content1-$content2-$content3-$content4"
            if (equipEntity != null) {
                val hashMap = gson.fromJson(equipEntity.infos, HashMap::class.java) as HashMap<String, Any>
                hashMap.put("digitalDeliveryID", code)
                equipEntity.infos = gson.toJsonTree(hashMap)
                repo.insObject(equipEntity)
            }
            val intent = Intent()
            intent.putExtra("digitalDeliveryID", code)
            mActivity.setResult(Activity.RESULT_OK, intent)
            mActivity.finish()
        }
    }

    private val gson = Gson()

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


    companion object {
        const val EQUIP_ENTITY = "EQUIP_ENTITY"
        const val SPACE_ENTITY = "SPACE_ENTITY"
    }
}