package com.sybotan.android.demo.viewmodel

import android.graphics.PointF
import cn.sagacloud.android.cadengine.items.PointItem
import cn.sagacloud.android.cadengine.items.TunableSpaceItem
import cn.sagacloud.android.cadengine.types.Point
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 kotlin.collections.ArrayList

/**
 * Created by lihao.
 * Date: 2021/6/9
 */
class GraphyVM(b: BaseViewModelInterface?, activity: BaseActivity?, //建筑id
               private val buildingId: String, //楼层id
               private val floorId: String) : BaseViewModel(b, activity) {

    /**
     * 查询空间和任务
     */
    fun getSpaceJob(from: String) {
        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(choseSpace: TunableSpaceItem, x: Float, y: Float) {
        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?) {
        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?) {
        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) {
        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?) {
        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?, toString: String, jobStatus: String) {
        val bean = ReqSetJob()
        bean.buildingId = buildingId
        bean.floorId = floorId
        bean.spaceId.add(choseSpace!!.data.id)
        bean.jobStatus = jobStatus
        bean.spaceCode = toString
        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?) {
        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() {
        val bean = ReqFloorPipe()
        bean.floorId = floorId
        val observable = RetrofitFactory.getInstance().floorPipe(getRequestBody(bean))
        sendRequest(observable, object : BaseObserver<java.util.ArrayList<SpacePipeModel>>(mActivity, this) {
            override fun onSuccess(models: java.util.ArrayList<SpacePipeModel>) {
                mEmitter.SendDircetive(SPACE_PIPE, models)
            }
        }, true)
    }

    companion object {
        //查询空间和任务
        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"

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

}