package com.sybotan.android.demo.activities

import android.content.Intent
import android.graphics.PointF
import android.os.Bundle
import android.widget.TextView
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.adapter.ContainerExpandAdapter
import com.sybotan.android.demo.bean.ContainerParentBean
import com.sybotan.android.demo.bean.result.Position
import com.sybotan.android.demo.bean.result.Z
import com.sybotan.android.demo.view.SuperExpandableListView
import com.sybotan.android.demo.viewmodel.ContainerVM
import java.util.HashMap

/**
 * 无模型设备添加设备页
 */
class EquipContainerActivity : BaseActivity() {
    private lateinit var title: TextView
    private lateinit var expandlv: SuperExpandableListView

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

    /** 楼层Id */
    private lateinit var floorId: String

    /** 项目Id */
    private lateinit var projectId: String

    /** 设备Id */
    private lateinit var deviceId: String
    private lateinit var spaceId: String
    private var position: Position? = null
    lateinit var containerVM: ContainerVM
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_equip_container)
        initView()
        initData()
    }

    private fun initData() {
        containerVM = ContainerVM(this.application, { name, param ->
            when (name) {
                ContainerVM.CONTAINER -> {
                    val hashMap = param as HashMap<String, Any>
                    title.setText(hashMap["title"].toString())
                    val parent = hashMap["parent"] as ArrayList<ContainerParentBean>
                    val children = hashMap["child"] as MutableList<List<ObjectEntity>>
                    val adapter = ContainerExpandAdapter(this, parent, children)
                    expandlv.setAdapter(adapter)
                    for (index in 0 until expandlv.count) {
                        expandlv.expandGroup(index)
                    }
                    adapter.setOnAdd(object : ContainerExpandAdapter.AddRel {
                        override fun onAdd(group: ContainerParentBean) {
                            val intent =
                                Intent(this@EquipContainerActivity, EquipDetailActivity::class.java)
                            val bundle = Bundle()
                            bundle.putSerializable("position", position)
                            bundle.putString("buildingId", buildingId)
                            bundle.putString("floorId", floorId)
                            bundle.putString("spaceId", spaceId)
                            bundle.putString("containerId", deviceId)
                            bundle.putString("widgetCode", group.widgetCode)
                            bundle.putString("projectId", projectId)
                            bundle.putParcelable("centerPoint", PointF(0f, 0f))
                            intent.putExtra("equip", bundle)
                            startActivity(intent)
                        }
                    })
                    adapter.setOnDelete(object : ContainerExpandAdapter.DeleteRel {
                        override fun ondelete(item: ObjectEntity) {
                            containerVM.deleteObject(item, deviceId)
                        }
                    })
                    adapter.setOnItemClick(object : ContainerExpandAdapter.ItemClick {
                        override fun onclick(item: ObjectEntity) {
                            val intent =
                                Intent(this@EquipContainerActivity, EquipDetailActivity::class.java)
                            val bundle = Bundle()
                            val positon = Position()
                            positon.x = position!!.x.toString()
                            positon.y = position!!.y.toString()
                            val z = Z()
                            z.offset = position!!.z.offset
                            z.region = position!!.z.region
                            positon.z = z
                            bundle.putString("spaceId", spaceId)
                            bundle.putSerializable("position", positon)
                            bundle.putString("buildingId", buildingId)
                            bundle.putString("floorId", floorId)
                            bundle.putString("deviceId", item.id)
                            bundle.putString("projectId", projectId)
                            bundle.putString("containerId", deviceId)
                            bundle.putParcelable("centerPoint", PointF(0f, 0f))
                            intent.putExtra("equip", bundle)
                            startActivity(intent)
                        }
                    })

                }
            }
        }, this, buildingId, floorId, projectId)
        expandlv.setGroupIndicator(null)
        expandlv.divider = null
    }

    override fun onResume() {
        super.onResume()
        containerVM.getConfig(deviceId)
    }

    private fun initView() {
        title = findViewById(R.id.title)
        expandlv = findViewById(R.id.expandlv)
        val params = intent.getBundleExtra("params")!!
        buildingId = params.getString("buildingId", "")!!
        floorId = params.getString("floorId", "")!!
        projectId = params.getString("projectId", "")!!
        deviceId = params.getString("deviceId", "")!!
        spaceId = params.getString("spaceId", "")!!
        position = params?.getSerializable("position") as? Position
    }
}