package com.sybotan.android.demo.viewmodel

import android.app.Activity.RESULT_OK
import android.app.Application
import android.graphics.PointF
import android.text.TextUtils
import androidx.lifecycle.viewModelScope
import cn.sagacloud.android.cadengine.items.PointItem
import cn.sagacloud.android.cadengine.items.SpaceItem
import cn.sagacloud.android.cadengine.items.TunableSpaceItem
import cn.sagacloud.android.cadengine.types.*
import com.framework.mvvm.model.db.entity.*
import com.framework.mvvm.model.repository.AdmRepository
import com.sybotan.android.demo.bean.result.ProblemsModel
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.sybotan.android.demo.activities.BaseActivity
import com.sybotan.android.demo.bean.request.*
import com.sybotan.android.demo.bean.result.*
import com.sybotan.android.demo.bean.result.Position
import com.sybotan.android.demo.bean.result.Z
import com.sybotan.android.demo.retrofit.BaseObserver
import com.sybotan.android.demo.retrofit.BaseViewModelInterface
import com.sybotan.android.demo.retrofit.RetrofitFactory
import com.sybotan.android.demo.tools.CommonUtils
import com.sybotan.android.demo.tools.ToastUtils
import com.sybotan.base.utils.SJsonUtil
import kotlinx.coroutines.launch
import org.json.JSONObject
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.android.x.closestDI
import org.kodein.di.instance
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import com.alibaba.fastjson.JSON


/**
 * Created by lihao.
 * Date: 2021/6/9
 */
class GraphyVM(
    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()
    val repo: AdmRepository by instance()

    /**
     * 查询空间和任务
     */
    fun getSpaceJob(from: String, spaceList: ArrayList<SpaceItem>) {
        viewModelScope.launch {
            var spaceList: ArrayList<SpaceEntity> = ArrayList()
            if ("space".equals(from)) {
                val spaceByArchState1 = repo.getSpaceByArchStateFloorId(floorId, "1")
                val spaceByArchState2 = repo.getSpaceByArchStateFloorId(floorId, "2")
                spaceList.addAll(spaceByArchState1)
                spaceList.addAll(spaceByArchState2)
            } else {
                val spaceByEquipState1 = repo.getSpaceByEquipStateFloorId(floorId, "1")
                val spaceByEquipState2 = repo.getSpaceByEquipStateFloorId(floorId, "2")
                spaceList.addAll(spaceByEquipState1)
                spaceList.addAll(spaceByEquipState2)
            }
            var spaceJobList = ArrayList<SpaceJobModel>()
            for (spaceEntity in spaceList) {
                var spaceJobModel = SpaceJobModel()
                val objectEntity = repo.getObject(spaceEntity.spaceId)
                spaceJobModel.buildingId = buildingId
                spaceJobModel.id = spaceEntity.spaceId
                spaceJobModel.localName = objectEntity!!.localName
                spaceJobModel.name = objectEntity.name
                spaceJobModel.projectId = projectId
                if (spaceEntity.archState == 1) {
                    spaceJobModel.job = "01"
                } else if (spaceEntity.archState == 2 && spaceEntity.equipState == 1) {
                    if (from.equals("space")) {
                        spaceJobModel.job = "03"
                    } else {
                        spaceJobModel.job = "04"
                    }
                } else if (spaceEntity.archState == 2 && spaceEntity.equipState == 2) {
                    spaceJobModel.job = "05"
                }
                val strInfo = gson.fromJson(objectEntity.strInfos, StrInfos::class.java)

                spaceJobModel.outline = strInfo.outline
                spaceJobModel.objectEntity = objectEntity
                spaceJobList.add(spaceJobModel)
            }
            mEmitter.SendDircetive(SPACE_JOB, spaceJobList)

        }
//        val bean = ReqSpaceJob()
//        bean.buildingId = buildingId
//        bean.floorId = floorId
//        if ("space" == from) {
//            bean.jobStatus.add("01")
//            bean.jobStatus.add("02")
//            bean.jobStatus.add("03")
//        } else {
//            bean.jobStatus.add("04")
//        }
//        val observable = RetrofitFactory.getInstance().getSpaceJob(getRequestBody(bean))
//        sendRequest(observable, object : BaseObserver<List<SpaceJobModel>>(mActivity, this) {
//            override fun onSuccess(spaceJobModels: List<SpaceJobModel>) {
//                mEmitter.SendDircetive(SPACE_JOB, spaceJobModels)
//            }
//        }, true)
    }

    /**
     * 添加空间核查问题
     *
     * @param choseSpace 选中的空间
     * @param x          x坐标
     * @param y          y坐标
     */
    fun addProblem(
        point: PointItem?,
        photos: ArrayList<Photos>,
        content: String,
        choseSpace: TunableSpaceItem,
        x: Float,
        y: Float
    ) {
        viewModelScope.launch {
            //插入问题数据
            val position = PointF()
            position.x = x
            position.y = y
            var toJson = ""
            val array = ArrayList<PointF>()
            if (point != null && point.data.mX != 0f) {
                position.x = point.data.mX
                position.y = point.data.mY
            }
            array.add(position)
            toJson = gson.toJson(array)
            var time = SimpleDateFormat("yyyyMMddHHmmss").format(Date(System.currentTimeMillis()))
            var toString = ""
            if (point != null && !TextUtils.isEmpty(point.data.id)) {
                toString = point.data.id!!
            } else {
                toString = UUID.randomUUID().toString().replace("-", "")
            }

            var problem = ProblemArchEntity(
                id = toString,
                floorId = floorId,
                projectId = projectId,
                buildingId = buildingId,
                geomType = "point", workId = "",
                problemType = "",
                problemState = 1,
                message = content,
                creator = "",
                creationTime = time,
                modifier = "android",
                modifiedTime = null,
                uploadFlag = "",
                valid = 1,
                strGeom = toJson,
                state = "1",
                geom = null
            )
            repo.insProblem(problem)


            //插入文件数据
            var order = 0
            for (photo in photos) {
                if (order != 0) {
                    var uuid = ""
                    if (TextUtils.isEmpty(photo.id)) {
                        uuid = UUID.randomUUID().toString().replace("-", "")
                    } else {
                        uuid = photo.id
                    }

                    val file = FileEntity(
                        id = uuid,
                        bizType = "problem_arch",
                        filePath = photo.key,
                        fileType = "photo",
                        clientPath = photo.path,
                        refObjId = toString,
                        refInfoCode = "",
                        remark = "",
                        orderNum = order,
                        projectId = projectId,
                        buildingId = buildingId,
                        floorId = floorId,
                        creator = "android",
                        creationTime = time,
                        modifier = "",
                        modifiedTime = null,
                        valid = 1,
                        uploadFlag = "", state = "1"
                    )
                    repo.insFile(file)
                }
                order++
            }
            mEmitter.SendDircetive(ADD_PROBLEM, null)
        }
//        val bean = ReqAddProblem()
//        bean.spaceId = choseSpace.data.id
//        bean.floorId = floorId
//        val position = Position()
//        position.x = x.toString() + ""
//        position.y = y.toString() + ""
//        bean.position = position
//        val observable = RetrofitFactory.getInstance().addProblem(getRequestBody(bean))
//        sendRequest(observable, object : BaseObserver<AddProblemModel>(mActivity, this) {
//            override fun onSuccess(model: AddProblemModel) {
//                mEmitter.SendDircetive(ADD_PROBLEM, model)
//            }
//        }, true)
    }

    /**
     * 查询空间核查问题
     *
     * @param choseSpace 选中的空间
     */
    fun getProblem(choseSpace: TunableSpaceItem?) {
        viewModelScope.launch {
            val problems = repo.getProblem()
            var models: ArrayList<ProblemsModel> = ArrayList()
            for (problem in problems) {
                if (problem.problemState == 1) {
                    val promodel = ProblemsModel()
                    promodel.content = problem.message
                    promodel.floorId = floorId
                    promodel.spaceId = choseSpace!!.data.id
                    promodel.id = problem.id

                    val geomList = gson.fromJson<List<PointF>>(
                        problem.strGeom,
                        object : TypeToken<List<PointF?>?>() {}.type
                    )

                    promodel.position = gson.toJson(geomList.get(0))
                    val refFiles = repo.getRefFile(problem.id)
                    for (file in refFiles) {
                        var photo = Photos()
                        photo.key = file.filePath
                        photo.id = file.id
                        photo.itemType = 0
                        photo.path = file.clientPath
                        promodel.photos.add(photo)
                    }
                    models.add(promodel)
                }
            }
            mEmitter.SendDircetive(GET_PROBLEM, models)
        }

//        val bean = ReqProblems()
//        bean.floorId = floorId
////        bean.spaceId = choseSpace!!.data.id
//        val observable = RetrofitFactory.getInstance().problems(getRequestBody(bean))
//        sendRequest(observable, object : BaseObserver<List<ProblemsModel>>(mActivity, this) {
//            override fun onSuccess(models: List<ProblemsModel>) {
//                mEmitter.SendDircetive(GET_PROBLEM, models)
//            }
//        }, true)
    }

    /**
     * 关闭空间核查问题
     *
     * @param point      点位
     * @param choseSpace 选中的空间
     */
    fun closeProblem(point: PointItem, choseSpace: TunableSpaceItem?) {
        viewModelScope.launch {
            val problem = repo.getProblemById(point.data.id!!)
            problem.problemState = 2
            repo.insProblem(problem)
            mEmitter.SendDircetive(CLOSE_PROBLEM, null)
        }
//        val bean = ReqCloseProblem()
//        bean.problemId = point.data.id
//        bean.spaceId = choseSpace!!.data.id
//        val observable = RetrofitFactory.getInstance().closeProblem(getRequestBody(bean))
//        sendRequest(observable, object : BaseObserver<CloseProblemModel>(mActivity, this) {
//            override fun onSuccess(setProblemModel: CloseProblemModel) {
//                mEmitter.SendDircetive(CLOSE_PROBLEM, null)
//            }
//        }, true)
    }

    /**
     * 绑定二维码
     *
     * @param choseSpace 选中的空间
     * @param result     二维码uuid
     * @param point      点位信息
     * @param toString   z轴描述
     */
    fun bindQrcode(choseSpace: TunableSpaceItem?, result: String?, point: Point, toString: String) {
        viewModelScope.launch {
            var pointZ = PointZ()
            pointZ.x = point.mX
            pointZ.y = point.mY
            pointZ.z = toString.toFloat()
            val toJson = gson.toJson(pointZ)
            var time = SimpleDateFormat("yyyyMMddHHmmss").format(Date(System.currentTimeMillis()))
            val qrcode = QrCodeEntity(
                id = UUID.randomUUID().toString(),
                qrCode = result!!,
                objId = choseSpace!!.data.id!!,
                projectId = projectId,
                buildingId = buildingId,
                floorId = floorId,
                strLocation = toJson,
                remark = "wall",
                creator = "Anroid",
                creationTime = time,
                modifier = "Android",
                modifiedTime = null,
                valid = 1,
                uploadFlag = "", state = "1", location = null
            )
            repo.insQrcode(qrcode)
            mEmitter.SendDircetive(BIND_QECODE, null)
        }
//        val bean = ReqBindQrcode()
//        bean.objId = choseSpace!!.data.id
//        bean.objType = "space"
//        bean.uuid = result
//        val position = Position()
//        position.x = java.lang.String.valueOf(point.mX)
//        position.y = java.lang.String.valueOf(point.mY)
//        position.z = Z()
//        position.z.offset = toString
//        position.z.region = "wall"
//        bean.position = position
//        val observable = RetrofitFactory.getInstance().bindQrcode(getRequestBody(bean))
//        sendRequest(observable, object : BaseObserver<BindQrcodeModel?>(mActivity, this) {
//            override fun onSuccess(bindQrcodeModel: BindQrcodeModel?) {
//                mEmitter.SendDircetive(BIND_QECODE, null)
//            }
//        }, true)
    }

    /**
     * 查询对象绑定的二维码
     *
     * @param choseSpace 选中的空间
     */
    fun getQrcode(choseSpace: TunableSpaceItem?) {
        viewModelScope.launch {
            val qrcodes = repo.getQrcode(choseSpace!!.data.id!!)
            val qrcodeModels = ArrayList<QrcodeModel>()
            for (qrcode in qrcodes) {
                var qrcodeModel = QrcodeModel()
                qrcodeModel.obj_id = qrcode.objId
                qrcodeModel.qr_code = qrcode.qrCode
                var pointZ = gson.fromJson(qrcode.strLocation, PointZ::class.java)
                val position = Position()
                position.x = pointZ.x.toString()
                position.y = pointZ.y.toString()
                position.z = Z()
                position.z.offset = pointZ.z.toString()
                position.z.region = "wall"
                qrcodeModel.position = gson.toJson(position)
                qrcodeModels.add(qrcodeModel)
            }
            mEmitter.SendDircetive(GET_QECODE, qrcodeModels)
        }
//        val bean = ReqQrcode()
//        bean.objId = choseSpace!!.data.id
//        val observable = RetrofitFactory.getInstance().qrcode(getRequestBody(bean))
//        sendRequest(observable, object : BaseObserver<ArrayList<QrcodeModel>>(mActivity, this) {
//            override fun onSuccess(qrcodeModels: ArrayList<QrcodeModel>) {
//                mEmitter.SendDircetive(GET_QECODE, qrcodeModels)
//            }
//        }, true)
    }

    /**
     * 设置空间的任务状态(新建和修改)
     *
     * @param choseSpace 选中的空间
     * @param toString   数字化交付编码
     */
    fun setJob(choseSpace: TunableSpaceItem?, jobStatus: String) {
        viewModelScope.launch {
            var space = repo.getSpaceBySpaceId(choseSpace!!.data.id!!)
            if (jobStatus.equals("01")) {
                space.archState = 1
            } else if (jobStatus.equals("03")) {
                space.archState = 2
            } else if (jobStatus.equals("04")) {
                space.archState = 2
                space.equipState = 1
            } else if (jobStatus.equals("05")) {
                space.archState = 2
                space.equipState = 2
            }
            space.state = "1"
            repo.insSpace(space)
            mEmitter.SendDircetive(SET_JOB, null)
        }
//        val bean = ReqSetJob()
//        bean.buildingId = buildingId
//        bean.floorId = floorId
//        bean.spaceId.add(choseSpace!!.data.id)
//        bean.jobStatus = jobStatus
//        bean.spaceCode = "test"
//        val observable = RetrofitFactory.getInstance().setJob(getRequestBody(bean))
//        sendRequest(observable, object : BaseObserver<SetJobModel>(mActivity, this) {
//            override fun onSuccess(model: SetJobModel) {
//                mEmitter.SendDircetive(SET_JOB, null)
//            }
//        }, true)
    }

    /**
     * 查询空间下的设备(所在关系)
     *
     * @param choseSpace 选中的空间
     */
    fun getSpaceEq(choseSpace: TunableSpaceItem?) {
        viewModelScope.launch {
            var equips = repo.getEquipByFloorId(floorId)

            var models: ArrayList<SpaceEqModel> = ArrayList()
            for (equip in equips) {
                var hasParent = false
                for (model in models) {
                    if (model.type!!.equals(equip.classCode)) {
                        hasParent = true
                        val child = Children()
                        child.deviceId = equip.id
                        child.name = equip.name
                        val strInfo = gson.fromJson(equip.strInfos, StrInfosforEquip::class.java)
                        val eqProblems = repo.getEqProblem(equip.id)
                        if (eqProblems != null && !TextUtils.isEmpty(eqProblems.id)) {
                            child.type = 3
                        } else if (strInfo.equipCheck) {
                            child.type = 2
                        } else {
                            child.type = 1
                        }
                        val position = Position()
                        position.x = strInfo.location.x.toString()
                        position.y = strInfo.location.y.toString()
                        val z = Z()
                        z.offset = strInfo.location.z.toString()
                        z.region = strInfo.zRegion
                        position.z = z
                        child.position = gson.toJson(position)
                        model.children.add(child)
                    }
                }
                if (!hasParent) {
                    val spaceModel = SpaceEqModel()
                    spaceModel.type = equip.classCode
                    var equipEntity = repo.getEquipByCode(equip.classCode)
                    spaceModel.typeName = equipEntity.name
                    val child = Children()
                    child.deviceId = equip.id
                    child.name = equip.name
                    val strInfo = gson.fromJson(equip.strInfos, StrInfosforEquip::class.java)
                    val eqProblems = repo.getEqProblem(equip.id)
                    if (eqProblems != null && !TextUtils.isEmpty(eqProblems.id)) {
                        child.type = 3
                    } else if (strInfo.equipCheck) {
                        child.type = 2
                    } else {
                        child.type = 1
                    }
                    val position = Position()
                    position.x = strInfo.location.x.toString()
                    position.y = strInfo.location.y.toString()
                    val z = Z()
                    z.offset = strInfo.location.z.toString()
                    z.region = strInfo.zRegion
                    position.z = z
                    child.position = gson.toJson(position)
                    spaceModel.children.add(child)
                    models.add(spaceModel)
                }
            }
            mEmitter.SendDircetive(SPACE_EQ, models)
        }
//        val bean = ReqSpaceEq()
//        bean.group = true
//        bean.spaceId = choseSpace!!.data.id
//        val observable = RetrofitFactory.getInstance().getSpaceEq(getRequestBody(bean))
//        sendRequest(observable, object : BaseObserver<List<SpaceEqModel>>(mActivity, this) {
//            override fun onSuccess(models: List<SpaceEqModel>) {
//                mEmitter.SendDircetive(SPACE_EQ, models)
//            }
//        }, true)
    }

    /**
     * 获取管线类型
     */
    fun pipeList() {
        val bean = ReqPipeList()
        val observable = RetrofitFactory.getInstance().pipeList(getRequestBody(bean))
        sendRequest(observable, object : BaseObserver<ArrayList<PipeListModel>>(mActivity, this) {
            override fun onSuccess(models: ArrayList<PipeListModel>) {
                mEmitter.SendDircetive(PIPE_LIST, models)
            }
        }, true)
    }

    /**
     * 添加管线
     * @param model         管线类型
     * @param pipeLineList  位置坐标
     * @param remark        描述
     * @param choseSpace    被选中的空间
     */
    fun addPipe(
//        model: PipeListModel,
//        pipeLineList: java.util.ArrayList<PointF>,
//        remark: String,
//        choseSpace: TunableSpaceItem?
    ) {
//        val bean = ReqAddPipe()
//        bean.spaceId = choseSpace!!.data.id
//        bean.sys = model.sysCode
//        bean.type = model.code
//        bean.position = pipeLineList
//        bean.remark = remark
//        bean.name = model.name
//        val observable = RetrofitFactory.getInstance().addPipe(getRequestBody(bean))
//        sendRequest(observable, object : BaseObserver<AddPipeModel>(mActivity, this) {
//            override fun onSuccess(models: AddPipeModel) {
//                mEmitter.SendDircetive(ADD_PIPE, null)
//            }
//        }, true)
    }

    /**
     * 查询楼层下的管道数据
     */
    fun floorPipe() {
        viewModelScope.launch {
            val pipeModels = ArrayList<SpacePipeModel>()
            val pipes = repo.getPipeByFloorId(floorId)
            for (pipe in pipes) {
                var hasParent = false
                for (model in pipeModels) {
                    if (model.type!!.equals(pipe.pipeType)) {
                        val child = ChildrenZ()
                        val objFrom = repo.getObject(pipe.objFrom!!)
                        val objTo = repo.getObject(pipe.objTo!!)
                        val strInfoFrom =
                            gson.fromJson(objFrom!!.strInfos, StrInfosforEquip::class.java)
                        child.position.add(PointF(strInfoFrom.location.x, strInfoFrom.location.y))

                        val strInfoTo =
                            gson.fromJson(objTo!!.strInfos, StrInfosforEquip::class.java)
                        child.position.add(PointF(strInfoTo.location.x, strInfoTo.location.y))
                        model.children.add(child)
                        hasParent = true
                    }
                }
                if (!hasParent) {
                    val pipeModel = SpacePipeModel()
                    pipeModel.typeName = pipe.pipeSubType + "--" + pipe.pipeType
                    pipeModel.type = pipe.pipeType
                    val child = ChildrenZ()
                    val objFrom = repo.getObject(pipe.objFrom!!)
                    val objTo = repo.getObject(pipe.objTo!!)
                    val strInfoFrom =
                        gson.fromJson(objFrom!!.strInfos, StrInfosforEquip::class.java)
                    child.position.add(PointF(strInfoFrom.location.x, strInfoFrom.location.y))

                    val strInfoTo = gson.fromJson(objTo!!.strInfos, StrInfosforEquip::class.java)
                    child.position.add(PointF(strInfoTo.location.x, strInfoTo.location.y))
                    pipeModel.children.add(child)
                    pipeModels.add(pipeModel)
                }

            }
            mEmitter.SendDircetive(SPACE_PIPE, pipeModels)
        }
//        val bean = ReqFloorPipe()
//        bean.floorId = floorId
//        val observable = RetrofitFactory.getInstance().floorPipe(getRequestBody(bean))
//        sendRequest(
//            observable,
//            object : BaseObserver<ArrayList<SpacePipeModel>>(mActivity, this) {
//                override fun onSuccess(models: java.util.ArrayList<SpacePipeModel>) {
//                    mEmitter.SendDircetive(SPACE_PIPE, models)
//                }
//            },
//            true
//        )
    }

    fun getInfosConfig(spaceId: String) {
        viewModelScope.launch {
            val infosConfig = repo.getInfosConfigByObjType("space")
            val strInfos = infosConfig.get(0).strInfos

//            var infoList = gson.fromJson(strInfos, ArrayList<Infos>()::class.java)
            var infoList = gson.fromJson<List<Infos>>(
                strInfos,
                object : TypeToken<List<Infos?>?>() {}.type
            )
            var spaceObject = repo.getObject(spaceId)
            val infos = gson.fromJson(spaceObject!!.strInfos, HashMap::class.java)

            mEmitter.SendDircetive(SPACE_INFOS, infos)
            mEmitter.SendDircetive(SPACE_INFO, infoList)
        }
    }

    fun jsonObjectToHashMap(jsonObj: JSONObject?): HashMap<String, String>? {
        if (jsonObj == null) {
            return null
        }
        val data = HashMap<String, String>()
        val it: Iterator<*> = jsonObj.keys()
        while (it.hasNext()) {
            val key = it.next().toString()
            data[key] = jsonObj[key].toString()
        }
        System.out.println(data)
        return data
    }

    fun refreshObject(space: TunableSpace, hashMap: HashMap<String, Any>) {
        viewModelScope.launch {
            var space = repo.getObject(space.id!!)
//            space!!.infos = JSONObject(hashMap as Map<*, *>)
            space!!.strInfos = gson.toJson(hashMap)
            space.state = "1"
            repo.insObject(space)
            mActivity.setResult(RESULT_OK)
            mActivity.finish()
//            var objectEntity = ObjectEntity(
//                id = space.id!!,
//                name = space.name!!,
//
//            )
//            repo.insObject()
        }
    }


    fun getEquipment(deviceId: String) {
        viewModelScope.launch {
            val equips = repo.getEquipment()
            for (equip in equips) {
                if (!TextUtils.isEmpty(equip.systemCode)) {
                    val systemEntity = repo.getSysByCode(equip.systemCode!!)
                    equip.systemName = systemEntity.name
                }
            }
            mEmitter.SendDircetive(EQUIP_CLASS, equips)

            if (!TextUtils.isEmpty(deviceId)) {
                var equip = repo.getObject(deviceId)
                val infos = gson.fromJson(equip!!.strInfos, HashMap::class.java)
                mEmitter.SendDircetive(SPACE_INFOS, infos)
                mEmitter.SendDircetive(Equip_INFOS, equip)
            }


//            var infoList = gson.fromJson(strInfos, ArrayList<Infos>()::class.java)

        }
    }

    fun getEquipInfosConfig(equipmentEntity: EquipmentEntity) {
        viewModelScope.launch {
            val infosConfig = repo.getInfosConfigByEquipCode(equipmentEntity.code!!)
            if (CommonUtils.IsNull(infosConfig)) {
                mEmitter.SendDircetive(EQUIP_INFO, null)
                return@launch
            }
            val strInfos = infosConfig.get(0).strInfos
            val infoList = gson.fromJson<List<Infos>>(
                strInfos,
                object : TypeToken<List<Infos?>?>() {}.type
            )
            mEmitter.SendDircetive(EQUIP_INFO, infoList)

        }
    }

    fun insEquip(
        uuid: String,
        deviceId: String?,
        hashMap: HashMap<String, Any>?,
        equipContent: String,
        beans: ArrayList<Photos>,
        equipEntity: EquipmentEntity,
        localName: String,
        localId: String,
        checked: Boolean,
        position: Position
    ) {
        viewModelScope.launch {
            hashMap!!.put("equipContent", equipContent)
            hashMap!!.put("equipPhoto", gson.toJson(beans))
            hashMap!!.put("equipCheck", checked)
            val z = PointZ()
            z.x = position.x.toFloat()
            z.y = -position.y.toFloat()
            z.z = position.z.offset.toFloat()
            hashMap!!.put("location", z)
            hashMap!!.put("zRegion", position.z.region)
            if (TextUtils.isEmpty(deviceId)) {
                val objectEntity = ObjectEntity(
                    id = uuid,
                    name = localName,
                    projectId = projectId,
                    objType = "equipment",
                    classCode = equipEntity.code!!,
                    localId = localId,
                    localName = localName,
                    groupCode = "",
                    grouping = 1,
                    buildingId = buildingId,
                    floorId = floorId,
                    creator = "Android",
                    createApp = "",
                    creationTime = SimpleDateFormat("yyyyMMddHHmmss").format(Date(System.currentTimeMillis()))
                        .toLong(),
                    modifier = "Android",
                    modifiedTime = null,
                    clientDevice = "",
                    clientId = uuid,
                    updateApp = "",
                    uploadFlag = "",
                    valid = 1,
                    deliveryState = null,
                    d2mState = null,
                    virtualCodes = null,
                    infos = null,
                    strInfos = gson.toJson(hashMap),
                    floorMap = null, state = "1"
                )
                repo.insObject(objectEntity)
            } else {
                val equip = repo.getObject(deviceId!!)
//                equip!!.infos = JSONObject(hashMap as Map<*, *>)
                equip!!.strInfos = gson.toJson(hashMap)
                repo.insObject(equip)
            }
            //插入文件数据
            var order = 0
            for (photo in beans) {
                if (photo.itemType != 1) {
                    if (order != 0) {
                        var phUuid = ""
                        if (TextUtils.isEmpty(photo.id)) {
                            phUuid = UUID.randomUUID().toString().replace("-", "")
                        } else {
                            phUuid = photo.id
                        }

                        val file = FileEntity(
                            id = phUuid,
                            bizType = "problem_arch",
                            filePath = photo.key,
                            fileType = "photo",
                            clientPath = photo.path,
                            refObjId = if (TextUtils.isEmpty(deviceId)) uuid else deviceId,
                            refInfoCode = "",
                            remark = "",
                            orderNum = order,
                            projectId = projectId,
                            buildingId = buildingId,
                            floorId = floorId,
                            creator = "android",
                            creationTime = "",
                            modifier = "",
                            modifiedTime = null,
                            valid = 1,
                            uploadFlag = "", state = "1"
                        )
                        repo.insFile(file)
                    }
                    order++
                }
            }
            mActivity.setResult(RESULT_OK)
            mActivity.finish()
        }
    }

    fun getMap() {
        viewModelScope.launch {
            val floors = repo.getFloors()
            for (floor in floors) {
                if (floor.id.equals(floorId)) {
                    if (TextUtils.isEmpty(floor.floorMap)) {
                        ToastUtils.showMyToast("没有地图")
                        break
                    }
                    val floorOfflineData =
                        SJsonUtil.fromJson(floor.floorMap!!, FloorData::class.java)
                    mEmitter.SendDircetive(MAP_LOAD, gson.toJson(floorOfflineData))
                    break
                }
            }

        }
    }

    fun getRelConfig(entity: EquipmentEntity, deviceId: String, uuid: String) {
        viewModelScope.launch {
            val equipId = if (TextUtils.isEmpty(deviceId)) uuid else deviceId
            //获取关系二级列表
            val configParent = ArrayList<RelConfigEntity>()
            val relList = mutableListOf<List<RelationEntity>>()
            val fromList = repo.getRelConfigByCodeFrom(entity.code!!)
            val toList = repo.getRelConfigByCodeTo(entity.code!!)
            configParent.addAll(fromList)
            for (config in fromList) {
                val relsFrom = repo.getRelByCodeFrom(config.graphCode!!, config.relCode!!, equipId)
                if (!CommonUtils.IsNull(relsFrom)) {
                    for (rel in relsFrom) {
                        val objFrom = repo.getObject(rel.objFrom!!)
                        val objTo = repo.getObject(rel.objTo!!)
                        if (objFrom == null) {
                            rel.objFromLocalId = "该设备"
                        } else {
                            if (TextUtils.isEmpty(objFrom.localId)) {
                                rel.objFromLocalId = objFrom.localName
                            } else {
                                rel.objFromLocalId = objFrom.localId
                            }
                        }
                        if (objTo == null) {
                            rel.objToLocalId = "该设备"
                        } else {
                            if (TextUtils.isEmpty(objTo.localId)) {
                                rel.objToLocalId = objTo.localName
                            } else {
                                rel.objToLocalId = objTo.localId
                            }
                        }
                    }
                    relList.add(relsFrom)
                } else {
                    relList.add(ArrayList<RelationEntity>())
                }
            }
            configParent.addAll(toList)
            for (config in toList) {
                val relsTo = repo.getRelByCodeTo(config.graphCode!!, config.relCode!!, equipId)
                if (!CommonUtils.IsNull(relsTo)) {
                    for (rel in relsTo) {
                        val objFrom = repo.getObject(rel.objFrom!!)
                        val objTo = repo.getObject(rel.objTo!!)
                        if (objFrom == null) {
                            rel.objFromLocalId = "该设备"
                        } else {
                            if (TextUtils.isEmpty(objFrom.localId)) {
                                rel.objFromLocalId = objFrom.localName
                            } else {
                                rel.objFromLocalId = objFrom.localId
                            }
                        }
                        if (objTo == null) {
                            rel.objToLocalId = "该设备"
                        } else {
                            if (TextUtils.isEmpty(objTo.localId)) {
                                rel.objToLocalId = objTo.localName
                            } else {
                                rel.objToLocalId = objTo.localId
                            }
                        }
                    }
                    relList.add(relsTo)
                } else {
                    relList.add(ArrayList<RelationEntity>())
                }
            }
            val hashMap = HashMap<String, Any>()
            hashMap.put("parent", configParent)
            hashMap.put("child", relList)
            mEmitter.SendDircetive(EQUIP_REL, hashMap)

            //获取管道二级列表
            val pipeParent = ArrayList<PipeConfigEntity>()
            val pipeChildren = mutableListOf<List<PipeEntity>>()
            val pipeFromList = repo.getPipeConfigByCodeFrom(entity.code!!)
            val pipeToList = repo.getPipeConfigByCodeTo(entity.code!!)

            pipeParent.addAll(pipeFromList)
            for (config in pipeFromList) {
                val pipeFrom =
                    repo.getPipeByIdFrom(equipId, config.pipeType!!, config.pipeSubType!!)
                if (!CommonUtils.IsNull(pipeFrom)) {
                    for (rel in pipeFrom) {
                        val objFrom = repo.getObject(rel.objFrom!!)
                        val objTo = repo.getObject(rel.objTo!!)
                        if (objFrom == null) {
                            rel.objFromLocalId = "该设备"
                        } else {
                            if (TextUtils.isEmpty(objFrom.localId)) {
                                rel.objFromLocalId = objFrom.localName
                            } else {
                                rel.objFromLocalId = objFrom.localId
                            }
                        }
                        if (objTo == null) {
                            rel.objToLocalId = "该设备"
                        } else {
                            if (TextUtils.isEmpty(objTo.localId)) {
                                rel.objToLocalId = objTo.localName
                            } else {
                                rel.objToLocalId = objTo.localId
                            }
                        }
                    }
                    pipeChildren.add(pipeFrom)
                } else {
                    pipeChildren.add(ArrayList<PipeEntity>())
                }
            }

            pipeParent.addAll(pipeToList)
            for (config in pipeToList) {
                val pipeFrom = repo.getPipeByIdTo(equipId, config.pipeType!!, config.pipeSubType!!)
                if (!CommonUtils.IsNull(pipeFrom)) {
                    for (rel in pipeFrom) {
                        val objFrom = repo.getObject(rel.objFrom!!)
                        val objTo = repo.getObject(rel.objTo!!)
                        if (objFrom == null) {
                            rel.objFromLocalId = "该设备"
                        } else {
                            if (TextUtils.isEmpty(objFrom.localId)) {
                                rel.objFromLocalId = objFrom.localName
                            } else {
                                rel.objFromLocalId = objFrom.localId
                            }
                        }
                        if (objTo == null) {
                            rel.objToLocalId = "该设备"
                        } else {
                            if (TextUtils.isEmpty(objTo.localId)) {
                                rel.objToLocalId = objTo.localName
                            } else {
                                rel.objToLocalId = objTo.localId
                            }
                        }
                    }
                    pipeChildren.add(pipeFrom)
                } else {
                    pipeChildren.add(ArrayList<PipeEntity>())
                }
            }
            val hashMap1 = HashMap<String, Any>()
            hashMap1.put("parent", pipeParent)
            hashMap1.put("child", pipeChildren)
            mEmitter.SendDircetive(PIPE, hashMap1)

        }
    }

    fun getRelObj(group: RelConfigEntity, code: String) {
        viewModelScope.launch {
            val objects: List<ObjectEntity>
            if (group.classCodeFrom.equals(code)) {
                objects = repo.getObjectByCode(group.classCodeTo!!)
            } else {
                objects = repo.getObjectByCode(group.classCodeFrom!!)
            }
            mEmitter.SendDircetive(REL_OBJECT, objects)
        }
    }

    fun getPipeObj(group: PipeConfigEntity, code: String) {
        viewModelScope.launch {
            val objects: List<ObjectEntity>
            if (group.classCodeFrom.equals(code)) {
                objects = repo.getObjectByCode(group.classCodeTo!!)
            } else {
                objects = repo.getObjectByCode(group.classCodeFrom!!)
            }
            mEmitter.SendDircetive(PIPE_OBJECT, objects)
        }
    }

    fun addRel(
        deviceId: String, uuid: String, equipEntity: EquipmentEntity,
        equip: ObjectEntity,
        groupRelConfigEntity: RelConfigEntity
    ) {
        viewModelScope.launch {
            var equipId: String
            if (TextUtils.isEmpty(deviceId)) {
                equipId = uuid
            } else {
                equipId = deviceId
            }
            val objFrom: String
            val objto: String
            if (groupRelConfigEntity.classCodeFrom.equals(equipEntity!!.code)) {
                objFrom = equipId
                objto = equip.id
            } else {
                objto = equipId
                objFrom = equip.id
            }

            val relEntity = RelationEntity(
                id = UUID.randomUUID().toString(),
                projectId = projectId,
                buildingId = buildingId,
                floorId = floorId,
                graphCode = groupRelConfigEntity.graphCode,
                graphId = null,
                groupCode = null,
                relCode = groupRelConfigEntity.relCode,
                relValue = null,
                objFrom = objFrom,
                objTo = objto,
                createApp = null,
                updateApp = null,
                creator = null,
                creationTime = null,
                modifiedTime = null,
                modifier = null,
                clientId = null,
                clientDevice = null,
                uploadFlag = null,
                deliveryState = null,
                valid = 1, d2mState = null, objFromLocalId = null, objToLocalId = null, state = "1"
            )
            repo.insRelation(relEntity)
            getRelConfig(equipEntity, deviceId, uuid)
        }
    }

    fun addPipe(
        deviceId: String, uuid: String, equipEntity: EquipmentEntity,
        equip: ObjectEntity,
        groupPipeConfigEntity: PipeConfigEntity
    ) {
        viewModelScope.launch {
            var equipId: String
            if (TextUtils.isEmpty(deviceId)) {
                equipId = uuid
            } else {
                equipId = deviceId
            }
            val objFrom: String
            val objto: String
            if (groupPipeConfigEntity.classCodeFrom.equals(equipEntity!!.code)) {
                objFrom = equipId
                objto = equip.id
            } else {
                objto = equipId
                objFrom = equip.id
            }

            val relEntity = PipeEntity(
                id = UUID.randomUUID().toString(),
                projectId = projectId,
                buildingId = buildingId,
                floorId = floorId,

                objFrom = objFrom,
                objTo = objto,

                creator = null,
                creationTime = null,
                modifiedTime = null,
                modifier = null,
                uploadFlag = null,
                deliveryState = null,
                valid = 1,
                d2mState = null,
                objFromLocalId = null,
                objToLocalId = null,
                direction = groupPipeConfigEntity.pipeDirection,
                typeFrom = "equipment",
                typeTo = "equipment",
                pipeType = groupPipeConfigEntity.pipeType,
                pipeSubType = groupPipeConfigEntity.pipeSubType,
                route = null,
                infos = null, state = "1"
            )
            repo.insPipe(relEntity)
            getRelConfig(equipEntity, deviceId, uuid)
        }
    }

    fun insProblemEquip(space: TunableSpaceItem, deviceId: String) {
        viewModelScope.launch {
            val proEntity = repo.getEqProblem(deviceId)
            if (proEntity == null) {
                val entity = ProblemEquipEntity(
                    id = UUID.randomUUID().toString(),
                    floorId = floorId,
                    projectId = projectId,
                    buildingId = buildingId,
                    equipId = deviceId,
                    workId = null,
                    problemType = null,
                    problemState = 1,
                    message = null,
                    creator = null,
                    creationTime = null,
                    modifier = null,
                    modifiedTime = null,
                    uploadFlag = null,
                    valid = 1, state = "1"
                )
                repo.insProblemEquipEntity(entity)
            }
        }
        getSpaceEq(space)
    }

    fun deleteRel(
        item: RelationEntity,
        equipEntity: EquipmentEntity?,
        deviceId: String,
        uuid: String
    ) {
        viewModelScope.launch {
            var rel = repo.getRelById(item.id)
            rel.valid = 0
            repo.insRelation(rel)
            getRelConfig(equipEntity!!, deviceId, uuid)
        }
    }

    fun deletePipe(
        item: PipeEntity,
        equipEntity: EquipmentEntity?,
        deviceId: String,
        uuid: String
    ) {
        viewModelScope.launch {
            var rel = repo.getPipeById(item.id)
            rel.valid = 0
            repo.insPipe(rel)
            getRelConfig(equipEntity!!, deviceId, uuid)
        }
    }

    companion object {
        const val MAP_LOAD = "MAP_LOAD"
        const val EQUIP_CLASS = "EQUIP_CLASS"

        //
        const val REFRESH_OBJECT = "SPACE_JOB"

        //查询空间和任务
        const val SPACE_JOB = "SPACE_JOB"

        //添加空间核查问题
        const val ADD_PROBLEM = "ADD_PROBLEM"

        //查询空间核查问题
        const val GET_PROBLEM = "GET_PROBLEM"

        //设置空间核查问题的信息和状态
        const val SET_PROBLEM = "SET_PROBLEM"

        //关闭空间核查问题
        const val CLOSE_PROBLEM = "CLOSE_PROBLEM"

        //绑定二维码
        const val BIND_QECODE = "BIND_QECODE"

        //查询对象绑定的二维码
        const val GET_QECODE = "GET_QECODE"

        //设置空间的任务状态(新建和修改)
        const val SET_JOB = "SET_JOB"

        //设置空间的任务状态(新建和修改)
        const val SPACE_EQ = "SPACE_EQ"

        //查询管道类型树
        const val PIPE_LIST = "PIPE_LIST"

        //添加管线
        const val ADD_PIPE = "ADD_PIPE"

        //查询空间下的管道Equip_INFOS
        const val SPACE_PIPE = "SPACE_PIPE"

        //查询空间详情
        const val SPACE_INFO = "SPACE_INFO"
        const val SPACE_INFOS = "SPACE_INFOS"


        //查询设备详情
        const val EQUIP_INFO = "EQUIP_INFO"
        const val Equip_INFOS = "Equip_INFOS"
        const val EQUIP_REL = "EQUIP_REL"

        const val REL_OBJECT = "REL_OBJECT"
        const val PIPE_OBJECT = "PIPE_OBJECT"
        const val PIPE = "PIPE"
    }

}