/*
 * ********************************************************************************************************************
 *
 *               iFHS7.
 *              ;BBMBMBMc                  rZMBMBR              BMB
 *              MBEr:;PBM,               7MBMMEOBB:             BBB                       RBW
 *     XK:      BO     SB.     :SZ       MBM.       c;;     ir  BBM :FFr       :SSF:    ;xBMB:r   iuGXv.    i:. iF2;
 *     DBBM0r.  :D     S7   ;XMBMB       GMBMu.     MBM:   BMB  MBMBBBMBMS   WMBMBMBBK  MBMBMBM  BMBRBMBW  .MBMBMBMBB
 *      :JMRMMD  ..    ,  1MMRM1;         ;MBMBBR:   MBM  ;MB:  BMB:   MBM. RMBr   sBMH   BM0         UMB,  BMB.  KMBv
 *     ;.   XOW  B1; :uM: 1RE,   i           .2BMBs  rMB. MBO   MBO    JMB; MBB     MBM   BBS    7MBMBOBM:  MBW   :BMc
 *     OBRJ.SEE  MRDOWOR, 3DE:7OBM       .     ;BMB   RMR7BM    BMB    MBB. BMB    ,BMR  .BBZ   MMB   rMB,  BMM   rMB7
 *     :FBRO0D0  RKXSXPR. JOKOOMPi       BMBSSWBMB;    BMBB:    MBMB0ZMBMS  .BMBOXRBMB    MBMDE RBM2;SMBM;  MBB   xBM2
 *         iZGE  O0SHSPO. uGZ7.          sBMBMBDL      :BMO     OZu:BMBK,     rRBMB0;     ,EBMB  xBMBr:ER.  RDU   :OO;
 *     ,BZ, 1D0  RPSFHXR. xWZ .SMr                  . .BBB
 *      :0BMRDG  RESSSKR. 2WOMBW;                   BMBMR
 *         i0BM: SWKHKGO  MBDv
 *           .UB  OOGDM. MK,                                          Copyright (c) 2015-2019.  斯伯坦机器人
 *              ,  XMW  ..
 *                  r                                                                     All rights reserved.
 *
 * ********************************************************************************************************************
 */

package com.sybotan.android.demo.activities

import android.annotation.SuppressLint
import android.graphics.PointF
import android.os.Bundle
import android.view.Gravity
import android.widget.*
import cn.sagacloud.android.cadengine.FloorScene
import cn.sagacloud.android.cadengine.items.*
import cn.sagacloud.android.cadengine.types.*
import com.framework.mvvm.model.db.entity.task.ObjectEntity
import com.google.gson.Gson
import com.sybotan.android.demo.R
import com.sybotan.android.demo.bean.RelConfigEntityPackClass
import com.sybotan.android.demo.bean.SpaceInfo
import com.sybotan.android.demo.bean.SpaceNameModel
import com.sybotan.android.demo.bean.result.*
import com.sybotan.android.demo.entitiies.Floor
import com.sybotan.android.demo.retrofit.Constant
import com.sybotan.android.demo.tools.CommonUtils
import com.sybotan.android.demo.tools.Lasso
import com.sybotan.android.demo.tools.ObjectNameUtils
import com.sybotan.android.demo.tools.ToastUtils
import com.sybotan.android.demo.view.SelectListSinglePopupWindow
import com.sybotan.android.demo.viewmodel.ChooseSpaceVM
import com.sybotan.android.graphy.SGraphyView
import com.sybotan.base.utils.SJsonUtil
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.android.closestDI
import java.lang.StringBuilder
import kotlin.collections.ArrayList

/**
 * 平面图选择空间
 */
class ChooseSpaceActivity : BaseActivity(), DIAware {

    override val di: DI by closestDI()

    /** viewModel */
    lateinit var chooseSpaceVM: ChooseSpaceVM

    /** 建筑Id */
    private lateinit var buildingId: String

    /** 楼层Id */
    private lateinit var floorId: String
    private lateinit var projectId: String
    private lateinit var deviceId: String
    private var scale: Float = 0f
    private lateinit var area: ServeAreaItem
    private lateinit var centerPoint: PointF
    private lateinit var position: Position
    var chooseSpaceList = ArrayList<TunableSpaceItem>()
    var spaceIdList = ArrayList<String>()

    /** 楼层地图 */
    private lateinit var floorMap: String

    var scene = FloorScene()
    private val gson = Gson()
    private lateinit var floors: ArrayList<Floor>
    private lateinit var groupRelConfigEntity: RelConfigEntityPackClass

    private lateinit var graphyView: SGraphyView
    private lateinit var close: TextView
    private lateinit var save: TextView
    private lateinit var floorTv: TextView
    private lateinit var spaceName: TextView
    private lateinit var direction: TextView
    private lateinit var root: LinearLayout
    private lateinit var spaces: ArrayList<ObjectEntity>

    /**
     * 创建Activity时的回调函数
     *
     * @param   savedInstanceState
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_choose_space)
        initView()

        /** 初始化数据 */
        initData()
        graphyView.scene = scene

        /** 点击监听 */
        initListener()
        /** 图形点击-poc业务 */
        initSingleTopUp()
        /** 网络请求回调处理 */
        initViewModel()

        return
    } // Function onCreate()


    private fun initView() {
        graphyView = findViewById(R.id.graphyView)
        close = findViewById(R.id.close)
        save = findViewById(R.id.save)
        floorTv = findViewById(R.id.floorTv)
        root = findViewById(R.id.root)
        spaceName = findViewById(R.id.spaceName)
        direction = findViewById(R.id.direction)
    }

    /**
     * 初始化数据
     */
    private fun initData() {
        val params = intent.getBundleExtra("params")!!
        buildingId = params.getString("buildingId", "")!!
        floorId = params.getString("floorId", "")!!
        floorMap = params.getString("floorMap", "")!!
        projectId = params.getString("projectId", "")!!
        deviceId = params.getString("deviceId", "")!!
        scale = params.getFloat("scale")!!
        centerPoint = params.getParcelable("centerPoint")!!
        groupRelConfigEntity = (params.getSerializable("groupRelConfigEntity") as RelConfigEntityPackClass?)!!
        position = (params.getSerializable("position") as Position?)!!
        val p = Point()
        p.mX = position.x.toFloat()
        p.mY = position.y.toFloat()
        val equipItem = EquipItem(this, p)
        equipItem.zOrder = 100000f
        scene.addItem(equipItem)
        if (groupRelConfigEntity.targetTypes[0].canFrom) {
            direction.text = "<-"
        } else {
            direction.text = "->"
        }
        if (groupRelConfigEntity.targetTypes[0].canFrom && groupRelConfigEntity.targetTypes[0].canTo) {
            direction.setOnClickListener {
                if (direction.text.equals("->")) {
                    direction.text = "<-"
                } else {
                    direction.text = "->"
                }
            }
        }
    }


    /**
     * 图形点击事件
     * */
    private fun initSingleTopUp() {
        scene.setOnSingleTopUp(object : FloorScene.OnSingleTopUp {
            @SuppressLint("SetJavaScriptEnabled")
            override fun onSingUp(x: Float, y: Float) {

                for (space in chooseSpaceList) {
                    val isContain = Lasso.isPolygonContainsPoint(space.data.outLine, android.graphics.Point(x.toInt(), -y.toInt()))
                    if (isContain && space.data.floorName == floorTv.text.toString()) {
                        scene.removeItem(space)
                        chooseSpaceList.remove(space)
                        spaceIdList.remove(space.data.id)
                        initSpaceName()
                        return
                    }
                }

                for (space in scene.tunableSpaceList) {
                    val isContain = Lasso.isPolygonContainsPoint(space.data.outLine, android.graphics.Point(x.toInt(), -y.toInt()))
                    if (isContain) {
                        val tunableSpace = TunableSpace()
                        tunableSpace.outLine = space.data.outLine
                        tunableSpace.name = space.data.name
                        tunableSpace.floorName = floorTv.text.toString()
                        tunableSpace.floorId = floorId
                        val item = TunableSpaceItem(tunableSpace)
                        item.zOrder = 99996f
                        chooseSpaceList.add(item)
                        scene.addItem(item)
                        spaceIdList.add(space.data.id!!)
                        initSpaceName()
                        break
                    }
                }
            }

        })
    }

    /**
     * 页面全部监听事件
     */
    private fun initListener() {
        floorTv.setOnClickListener {
            val pop = SelectListSinglePopupWindow(this)
            pop.setGenList(floors, Constant.TYPE5)
            pop.setTitle(true, "选择对象")
            pop.showAtLocation(root, Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 0)
            pop.setOnItemClickListener {
                val floor = floors[it]
                floorTv.text = floor.localName
                floorId = floor.id
                chooseSpaceVM.getFloorMap(floorId)
            }
        }
        close.setOnClickListener { finish() }
        save.setOnClickListener {
            if (CommonUtils.IsNull(chooseSpaceList)) {
                ToastUtils.showMyToast("请绘制服务区域")
                return@setOnClickListener
            }
            chooseSpaceVM.insRel(deviceId, spaceIdList, groupRelConfigEntity, direction.text.toString())
        }
    }

    /**
     * 初始化viewModel
     */
    private fun initViewModel() {
        chooseSpaceVM = ChooseSpaceVM(this.application, { name, params ->
            when (name) {
                /** 加载地图 */
                ChooseSpaceVM.MAP_LOAD -> {
                    val floorOfflineData = gson.fromJson(params.toString(), FloorData::class.java)
                    doAsync {
                        scene.loadData(floorOfflineData)
                        uiThread {
                            graphyView.setCenterPoint(centerPoint, scale)
                            chooseSpaceVM.getFloorList()
                            chooseSpaceVM.getRel(deviceId, groupRelConfigEntity)
                        }
                    }
                }

                /** 物业空间 */
                ChooseSpaceVM.SERVE_AREAS -> {
                    val param = params as ArrayList<TunableSpace>
                    for (space in param) {
                        spaceIdList.add(space.id!!)
                        val item = TunableSpaceItem(space)
                        item.zOrder = 99996f
                        if (space.floorId == floorId) {
                            scene.addItem(item)
                        }
                        chooseSpaceList.add(item)
                    }
                    initSpaceName()
                }
                ChooseSpaceVM.FLOORS_LIST -> {
                    floors = params as ArrayList<Floor>
                    for (floor in floors) {
                        if (floorId == floor.id) {
                            floorTv.setText(floor.localName)
                            chooseSpaceVM.getSpaces(floorId)
                        }
                    }
                }
                ChooseSpaceVM.FLOOR_MAP -> {
                    val floorOfflineData = SJsonUtil.fromJson(params.toString(), FloorData::class.java)
                    doAsync {
                        scene = FloorScene()
                        graphyView.scene = scene
                        scene.loadData(floorOfflineData!!)
                        uiThread {
                            initSingleTopUp()
                            for (space in chooseSpaceList) {
                                if (space.data.floorId == floorId) {
                                    scene.addItem(space)
                                }
                            }
                            initSpaceName()
                            chooseSpaceVM.getSpaces(floorId)
                            graphyView.fitSceneToView()
                        }
                    }
                }
                ChooseSpaceVM.SPACES -> {
                    spaces = params as ArrayList<ObjectEntity>
                    for (item in scene.tunableSpaceList) {
                        scene.removeItem(item)
                    }
                    scene.tunableSpaceList.clear()
                    for (space in spaces) {
                        if (space.infos != null && space.infos!!.asJsonObject.keySet().isNotEmpty()) {
                            val info = gson.fromJson(space.infos, SpaceInfo::class.java)
                            if (!CommonUtils.IsNull(info.outline)) {
                                val tunableSpace = TunableSpace()
                                tunableSpace.outLine = info.outline
                                tunableSpace.isShow = true
                                tunableSpace.id = space.id
                                tunableSpace.name = ObjectNameUtils.getEquipName(space)
                                val item = TunableSpaceItem(tunableSpace)
                                item.zOrder = 99995f
                                scene.tunableSpaceList.add(item)
                                scene.addItem(item)
                            }
                        }
                    }
                }

            }
        }, this, buildingId, floorId, projectId)
        chooseSpaceVM.getMap()

    }

    private fun initSpaceName() {
        val sb = StringBuilder()
        val others = ArrayList<SpaceNameModel>()
        sb.append(floorTv.text.toString()).append("：")
        for (space in chooseSpaceList) {
            if (space.data.floorName == floorTv.text.toString()) {
                sb.append(space.data.name).append("、")
            } else {
                var has = false
                for (other in others) {
                    if (other.name == space.data.floorName) {
                        other.size++
                        has = true
                    }
                }
                if (!has) {
                    others.add(SpaceNameModel(space.data.floorName, 1))
                }
            }
        }
        sb.append("\n\n其他层：\n")
        for (other in others) {
            sb.append("${other.name}(${other.size})、")
        }
        spaceName.text = sb.toString()
    }


} // Class GraphyActivity
