package com.sybotan.android.demo.viewmodel

import android.app.Application
import android.graphics.PointF
import android.text.TextUtils
import androidx.lifecycle.viewModelScope
import cn.hutool.core.util.IdUtil
import cn.sagacloud.android.cadengine.Lasso
import cn.sagacloud.android.cadengine.items.PipeLineItem
import cn.sagacloud.android.cadengine.items.PointItem
import cn.sagacloud.android.cadengine.items.TunableSpaceItem
import cn.sagacloud.android.cadengine.types.*
import com.framework.mvvm.model.db.entity.task.*
import com.framework.mvvm.model.repository.AdmRepository
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.reflect.TypeToken
import com.sybotan.android.demo.activities.BaseActivity
import com.sybotan.android.demo.bean.RelConfigEntityPackClass
import com.sybotan.android.demo.bean.RelationPackClass
import com.sybotan.android.demo.bean.SpaceInfo
import com.sybotan.android.demo.bean.result.*
import com.sybotan.android.demo.entitiies.Floor
import com.sybotan.android.demo.entitiies.Info
import com.sybotan.android.demo.entitiies.Statistics
import com.sybotan.android.demo.retrofit.BaseViewModelInterface
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.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.android.x.closestDI
import org.kodein.di.instance
import java.util.*
import kotlin.collections.ArrayList


/**
 * Created by lihao.
 * Date: 2021/6/9
 */
class ChooseSpaceVM(
    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 getMap() {
        viewModelScope.launch {
            val floors = repo.getFloors()
            for (floor in floors) {
                if (floor.id == floorId) {
                    if (TextUtils.isEmpty(floor.floorMap)) {
                        ToastUtils.showMyToast("没有地图")
                        break
                    }
                    val floorOfflineData =
                        SJsonUtil.fromJson(floor.floorMap!!, FloorData::class.java)
                    mEmitter.SendDirective(MAP_LOAD, gson.toJson(floorOfflineData))
                    break
                }
            }

        }
    }


    fun insServeAreas(deviceId: String, spaces: ArrayList<TunableSpaceItem>) {
        viewModelScope.launch {
            val geom = ArrayList<ArrayList<ArrayList<PointZ>>>()
            for (space in spaces) {
                geom.add(space.data.outLine!!)
            }
            var entity = repo.getServeAreaByObjId(deviceId)
            if (entity == null) {
                entity = ServeAreaEntity(
                    id = IdUtil.simpleUUID(),
                    projectId = projectId,
                    buildingId = buildingId,
                    floorId = floorId,
                    state = "1",
                    uploadFlag = null,
                    valid = 1,
                    creator = null,
                    creationTime = null,
                    modifier = null,
                    modifiedTime = null,
                    objId = deviceId,
                    geomType = "polygon",
                    geom = gson.toJsonTree(geom),
                    serveType = null, remark = null
                )
            } else {
                entity.geom = gson.toJsonTree(geom)
                entity.state = "1"
            }
            repo.insServeArea(entity)
            mActivity.finish()
        }
    }

    fun getServeArea(deviceId: String) {
        viewModelScope.launch {
            val entity = repo.getServeAreaByObjId(deviceId)
            if (entity != null) {
                val array = entity.geom!!.asJsonArray
                if (array[0].isJsonObject) {
                    val ar = gson.fromJson<List<PointF>>(
                        gson.toJson(entity.geom),
                        object : TypeToken<List<PointF?>?>() {}.type
                    )
                    mEmitter.SendDirective(SERVE_AREA, ar)
                } else {
                    val ar = gson.fromJson<List<ArrayList<ArrayList<PointZ>>>>(
                        gson.toJson(entity.geom),
                        object : TypeToken<List<ArrayList<ArrayList<PointZ>>>>() {}.type
                    )
                    mEmitter.SendDirective(SERVE_AREAS, ar)
                }
            }
        }
    }


    fun getFloorList() {
        viewModelScope.launch {
            val floors = repo.getFloorsByBuildingId(buildingId)
            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 }
            mEmitter.SendDirective(FLOORS_LIST, floorList)
        }

    }

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

        }
    }

    fun insRel(deviceId: String, spaceIds: ArrayList<String>, groupRelConfigEntity: RelConfigEntityPackClass) {
        viewModelScope.launch {
            val rels = getRelations(groupRelConfigEntity, deviceId)
            for (rel in rels) {
                rel.entity.valid = 0
                repo.insRelation(rel.entity)
            }
            for (id in spaceIds) {
                val objFrom: String
                val objto: String
                if (groupRelConfigEntity.targetTypes[0].canFrom) {
                    objto = deviceId
                    objFrom = id
                } else {
                    objFrom = deviceId
                    objto = id
                }
                val en = repo.getRelByGraphCodeAndRelCodeAndObjFromAndObjTo(groupRelConfigEntity.entity.graphCode!!, groupRelConfigEntity.entity.relCode!!, objFrom, objto)
                if (en == null) {
                    val relEntity = RelationEntity(
                        id = IdUtil.simpleUUID(),
                        projectId = projectId,
                        buildingId = buildingId,
                        floorId = floorId,
                        graphCode = groupRelConfigEntity.entity.graphCode,
                        graphId = null,
                        groupCode = null,
                        relCode = groupRelConfigEntity.entity.relCode,
                        relValue = null,
                        objFrom = objFrom,
                        objTo = objto,
                        createApp = null,
                        updateApp = null,
                        creator = null,
                        creationTime = "",
                        modifiedTime = "",
                        modifier = null,
                        clientId = null,
                        clientDevice = null,
                        uploadFlag = null,
                        deliveryState = null,
                        valid = 1, d2mState = null, objFromLocalId = null, objToLocalId = null, state = "1"
                    )
                    repo.insRelation(relEntity)
                }
            }
            mActivity.finish()
        }
    }

    fun getRel(deviceId: String, groupRelConfigEntity: RelConfigEntityPackClass) {
        viewModelScope.launch {
            val rels = getRelations(groupRelConfigEntity, deviceId)
            val spaces = ArrayList<TunableSpace>()
            for (rel in rels) {
                val objEntity: ObjectEntity
                if (rel.entity.objFrom == deviceId) {
                    objEntity = repo.getObject(rel.entity.objTo!!)!!
                } else {
                    objEntity = repo.getObject(rel.entity.objFrom!!)!!
                }
                val obj = objEntity.infos!!.asJsonObject
                val info = gson.fromJson(obj, SpaceInfo::class.java)
//                spaces.add(info.outline[0])

                val tunableSpace = TunableSpace()
                tunableSpace.outLine = info.outline[0]
                tunableSpace.name = objEntity.localName
                tunableSpace.id = objEntity.id
                val floor = repo.getFloorById(objEntity.floorId!!)
                tunableSpace.floorName = floor.localName
                tunableSpace.floorId = objEntity.floorId
                spaces.add(tunableSpace)
            }
            mEmitter.SendDirective(SERVE_AREAS, spaces)
        }
    }

    private suspend fun ChooseSpaceVM.getRelations(
        groupRelConfigEntity: RelConfigEntityPackClass,
        deviceId: String
    ): ArrayList<RelationPackClass> {
        val rels = ArrayList<RelationPackClass>()
        for (child in groupRelConfigEntity.targetTypes) {
            if (child.canFrom) {
                val to = repo.getRelByCodeTo(
                    groupRelConfigEntity.entity.graphCode!!,
                    groupRelConfigEntity.entity.relCode!!,
                    deviceId
                )
                loadRelation(to, child, groupRelConfigEntity, rels)
            }

            if (child.canTo) {
                val from = repo.getRelByCodeFrom(
                    groupRelConfigEntity.entity.graphCode!!,
                    groupRelConfigEntity.entity.relCode!!,
                    deviceId
                )
                loadRelation(from, child, groupRelConfigEntity, rels)
            }
        }
        return rels
    }

    private suspend fun loadRelation(
        to: List<RelationEntity>,
        child: RelConfigEntityPackClass.TargetRelType,
        parent: RelConfigEntityPackClass,
        rel: ArrayList<RelationPackClass>
    ) {
        bindRelationEntityName(to)
        if (!CommonUtils.IsNull(to)) {
            for (t in to) {
                if (child.targetCode.equals(parent.entity.classCodeTo)) {
                    val pack = RelationPackClass()
                    pack.entity = t
                    pack.targetPipeType = child
                    rel.add(pack)
                }
            }
        }
    }

    private suspend fun bindRelationEntityName(
        entitys: List<RelationEntity>
    ) {
        for (rel in entitys) {
            val from = repo.getObject(rel.objFrom!!)
            if (from == null) {
                rel.objFromLocalId = "该设备"
            } else {
                rel.objFromLocalId =
                    if (from!!.id.startsWith("Eq")) from!!.localId else from!!.localName
            }
            val to = repo.getObject(rel.objTo!!)
            if (to == null) {
                rel.objToLocalId = "该设备"
            } else {
                rel.objToLocalId = if (to!!.id.startsWith("Eq")) to!!.localId else to!!.localName
            }
        }
    }

    fun getSpaces(floorId: String) {
        viewModelScope.launch {
            val spaces = repo.getSpaceByFloorId(floorId)
            mEmitter.SendDirective(SPACES, spaces)
        }
    }


    companion object {
        const val MAP_LOAD = "MAP_LOAD"

        //服务区域
        const val SERVE_AREA = "SERVE_AREA"
        const val SERVE_AREAS = "SERVE_AREAS"
        const val FLOORS_LIST = "FLOORS_LIST"
        const val FLOOR_MAP = "FLOOR_MAP"
        const val SPACES = "SPACES"

    }
}