package com.sybotan.android.demo.activities.poc

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import com.sybotan.android.demo.R
import com.sybotan.android.demo.activities.BaseActivity
import com.sybotan.android.demo.adapter.ExpandAdapter
import com.sybotan.android.demo.adapter.RelationAdapter
import com.sybotan.android.demo.adapter.ServerRelAdapter
import com.sybotan.android.demo.bean.request.ReqEqToRel
import com.sybotan.android.demo.bean.result.SpaceEqModel
import com.sybotan.android.demo.entitiies.Build
import com.sybotan.android.demo.entitiies.EqEntity
import com.sybotan.android.demo.entitiies.RelationEq
import com.sybotan.android.demo.retrofit.BaseViewModelInterface
import com.sybotan.android.demo.viewmodel.BuildVM
import com.sybotan.android.demo.viewmodel.RelationVM


class RelationActivity : BaseActivity() {
    companion object {
        // 设备服务于空间
        const val EQ_SP_SERVE = "eq_sp_serve"

        // 设备 阀门 设备
        const val EQ_EQ_VV = "eq_eq_vv"

        // 设备 控制 设备
        const val EQ_EQ_CTRL = "eq_eq_ctrl"

        // 设备 供电 设备
        const val EQ_EQ_POWER = "eq_eq_power"

        // 设备 测量 设备
        const val EQ_EQ_SS = "eq_eq_ss"

        // 设备连接管道
        const val EQ_PIPE_CNCT = "eq_pipe_cnct"

        // 设备服务于空间
        const val TYPE_EQ_SP = "eq_sp"

        // 设备与设备之间的关系
        const val TYPE_EQ_EQ = "eq_eq"

        // 设备与管道之间的关系
        const val TYPE_EQ_PIPE = "eq_pipe"
    }

    private lateinit var mSpaceId: String
    private lateinit var mMajorTv: TextView
    private lateinit var mMinorTv: TextView

    private lateinit var mMajorLv: ListView
    private lateinit var mMinorLv: ListView

    private lateinit var mAddBtn: Button
    private lateinit var type: String
    private lateinit var code: String

    private lateinit var mBackIv: ImageView
    private lateinit var mTitleTv: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_relation)
        findView()
        initView()
        initViewModel()
        setListener()
    }

    private fun findView() {
        mBackIv = findViewById(R.id.backIv)
        mTitleTv = findViewById(R.id.titleTv)
        mAddBtn = findViewById(R.id.addBtn)
        mMajorTv = findViewById(R.id.majorTv)
        mMajorLv = findViewById(R.id.majorLv)
        mMajorLv.divider = null
        mMinorTv = findViewById(R.id.minorTv)
        mMinorLv = findViewById(R.id.minorLv)
        mMinorLv.divider = null
    }

    private fun initView() {
        val bundle = intent.getBundleExtra("params")
        mSpaceId = bundle?.getString("spaceId") ?: ""
        type = bundle?.getString("type") ?: ""
        code = bundle?.getString("code") ?: ""

        when (code) {
            EQ_SP_SERVE -> {
                mMinorTv.visibility = View.GONE
                mMinorLv.visibility = View.GONE
                mMajorTv.visibility = View.GONE
                mTitleTv.text = "选择服务于当前空间的设备"
            }

            EQ_EQ_VV -> {
                mMinorTv.visibility = View.VISIBLE
                mMinorLv.visibility = View.VISIBLE
                mMajorTv.visibility = View.VISIBLE
                mMajorTv.text = "主"
                mMinorTv.text = "从"
                mTitleTv.text = "当前空间内设备与设备(阀门)"
            }

            EQ_EQ_CTRL -> {
                mMinorTv.visibility = View.VISIBLE
                mMinorLv.visibility = View.VISIBLE
                mMajorTv.visibility = View.VISIBLE
                mMajorTv.text = "主"
                mMinorTv.text = "从"
                mTitleTv.text = "当前空间内设备与设备(控制)"

            }
            EQ_EQ_POWER -> {
                mMinorTv.visibility = View.VISIBLE
                mMinorLv.visibility = View.VISIBLE
                mMajorTv.visibility = View.VISIBLE
                mMajorTv.text = "主"
                mMinorTv.text = "从"
                mTitleTv.text = "当前空间内设备与设备(电量)"
            }
            EQ_EQ_SS -> {
                mMinorTv.visibility = View.VISIBLE
                mMinorLv.visibility = View.VISIBLE
                mMajorTv.visibility = View.VISIBLE
                mMajorTv.text = "主"
                mMinorTv.text = "从"
                mTitleTv.text = "当前空间内设备与设备(测量)"
            }

            EQ_PIPE_CNCT -> {
                mMinorTv.visibility = View.VISIBLE
                mMinorLv.visibility = View.VISIBLE
                mMajorTv.visibility = View.VISIBLE
                mMajorTv.text = "设备"
                mMinorTv.text = "管道类型"
                mTitleTv.text = "当前空间内设备与管道"
            }
        }
    }

    private fun setListener() {

        mAddBtn.setOnClickListener {
            when (code) {
                EQ_SP_SERVE -> {
                    val list = serverRelEqs.map { it.deviceId }
                    val rel = ReqEqToRel(eq_id = list, target_id = mSpaceId, rel = EQ_SP_SERVE)
                    mVm.addEq(rel)
                }
            }

        }
        mBackIv.setOnClickListener {
            finish()
        }

    }


    private lateinit var mEqs: List<RelationEq>
    private var serverRelEqs = mutableListOf<RelationEq>()

    private lateinit var mVm: RelationVM
    private fun initViewModel() {
        mVm = RelationVM(BaseViewModelInterface { name, params ->
            mEqs = params as List<RelationEq>
            when (name) {
                EQ_SP_SERVE -> {
                    val adapter = ServerRelAdapter(mEqs, this@RelationActivity) { eq ->
                        serverRelEqs.clear()
                        serverRelEqs.addAll(eq)
                        println("serverRelEqs size : ${serverRelEqs.size}")
                    }
                    mMajorLv.setAdapter(adapter)
                }
            }

        }, this)
        mVm.getEqs(mSpaceId)
    }

}