package com.sybotan.android.demo.activities

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import com.framework.mvvm.model.db.entity.dict.EquipmentEntity
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.retrofit.Constant
import com.sybotan.android.demo.tools.CommonUtils
import com.sybotan.android.demo.view.ObjectRelationDialog
import com.sybotan.android.demo.view.SelectListSinglePopupWindow
import com.sybotan.android.demo.viewmodel.EquipDeliveryCodeVM
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.android.closestDI
import java.util.*
import kotlin.collections.HashMap

class EquipDeliveryCodeActivity : BaseActivity(), DIAware {

    override val di: DI by closestDI()

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

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

    /** 项目Id */
    private lateinit var projectId: String
    private lateinit var spaceId: String
    private lateinit var deviceId: String
    private lateinit var equipment: EquipmentEntity
    private lateinit var hashmap: HashMap<String,Any>

    lateinit var viewModel: EquipDeliveryCodeVM

    private lateinit var backIv: ImageView
    private lateinit var edt1: EditText
    private lateinit var edt2: EditText
    private lateinit var submit: TextView
    private lateinit var tv1: TextView
    private lateinit var tv2: TextView
    private lateinit var edt3: TextView
    private lateinit var titleTv: TextView
    private lateinit var edt4: EditText
    private lateinit var root: LinearLayout
    val gson = Gson()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_equip_delivery_code)
        initView()
        initListener()
        initData()
    }

    private fun initView() {
        val params = intent.getBundleExtra("params")!!
        buildingId = params.getString("buildingId", "")!!
        floorId = params.getString("floorId", "")!!
        projectId = params.getString("projectId", "")!!
        spaceId = params.getString("spaceId", "")!!
        deviceId = params.getString("deviceId", "")!!
        equipment = (params.getSerializable("equipment") as EquipmentEntity?)!!
        hashmap = (params.getSerializable("hashmap") as HashMap<String, Any>?)!!
        backIv = findViewById(R.id.backIv)
        edt1 = findViewById(R.id.edt1)
        edt2 = findViewById(R.id.edt2)
        submit = findViewById(R.id.submit)
        tv1 = findViewById(R.id.tv1)
        tv2 = findViewById(R.id.tv2)
        edt3 = findViewById(R.id.edt3)
        edt4 = findViewById(R.id.edt4)
        root = findViewById(R.id.root)
        titleTv = findViewById(R.id.titleTv)
    }

    private fun initListener() {
        backIv.setOnClickListener { finish() }
        tv1.setOnClickListener {
            edt1.setText(tv1.text.toString())
        }
        tv2.setOnClickListener {
            edt2.setText(tv2.text.toString())
        }
        refreshSubmitEnable(edt1)
        refreshSubmitEnable(edt2)
        refreshSubmitEnable(edt3)
        refreshSubmitEnable(edt4)

        edt3.setOnClickListener {
            val locationList = resources.getStringArray(R.array.install_location_list)
            val pop = SelectListSinglePopupWindow(this@EquipDeliveryCodeActivity)
            pop.setGenList(locationList.asList(), Constant.TYPE6)
            pop.setTitle(true, "安装点标识")
            pop.showAtLocation(root, Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 0)
            pop.setOnItemClickListener {
                edt3.text = locationList[it].substringAfterLast("：")
            }
        }

        submit.setOnClickListener {
            viewModel.insSerialNumber(deviceId, edt1.text.toString(), edt2.text.toString(), edt3.text.toString(), edt4.text.toString())
        }
    }

    private fun refreshSubmitEnable(edt: TextView) {
        edt.addTextChangedListener {
            if (!TextUtils.isEmpty(edt1.text.toString()) && !TextUtils.isEmpty(edt2.text.toString()) && edt2.text.toString()
                    .contains("-RM") && !TextUtils.isEmpty(edt3.text.toString()) && !TextUtils.isEmpty(edt4.text.toString())
            ) {
                submit.isEnabled = true
                submit.alpha = 1f
            } else {
                submit.isEnabled = true
                submit.alpha = 0.5f
            }
        }
    }

    private fun initData() {
        viewModel = EquipDeliveryCodeVM(this.application, { name, param ->
            when (name) {
                EquipDeliveryCodeVM.EQUIP_ENTITY -> {
                    if (equipment != null || !TextUtils.isEmpty(equipment.code)) {
                        tv1.text = equipment.code
                        titleTv.text = equipment.name
                    }
                    if (param != null) {
                        val equipEntity = param as ObjectEntity
                        tv1.text = equipEntity.classCode
                        titleTv.text = "${equipment.name}：${equipEntity.localId}"
                        val info = gson.fromJson(equipEntity.infos, HashMap::class.java)
                        val spaceDeliveryCode = info["digitalDeliveryID"]
                        if (spaceDeliveryCode != null && !TextUtils.isEmpty(spaceDeliveryCode.toString())) {
                            val split = spaceDeliveryCode.toString().split("-")
                            if (split.size >= 5) {
                                edt1.setText(split[0])
                                edt2.setText("${split[1]}-${split[2]}")
                                edt3.text = split[3]
                                edt4.setText(split[4])
                            }
                        }
                    }
                    if (hashmap != null && hashmap["digitalDeliveryID"]!=null &&!TextUtils.isEmpty(hashmap["digitalDeliveryID"].toString())) {
                        val split = hashmap["digitalDeliveryID"].toString().split("-")
                        if (split.size >= 5) {
                            edt1.setText(split[0])
                            edt2.setText("${split[1]}-${split[2]}")
                            edt3.text = split[3]
                            edt4.setText(split[4])
                        }
                    }

                }
                EquipDeliveryCodeVM.SPACE_ENTITY -> {
                    if (param != null) {
                        val spaceEntity = param as ObjectEntity
                        val info = gson.fromJson(spaceEntity.infos, HashMap::class.java)
                        val spaceDeliveryCode = info["digitalDeliveryID"]
                        if (spaceDeliveryCode != null && !TextUtils.isEmpty(spaceDeliveryCode.toString())) {
                            tv2.text = spaceDeliveryCode.toString()
                        } else {
                            tv2.text = "??????-RM???"
                        }
                    } else {
                        tv2.text = "??????-RM???"
                    }
                }
            }
        }, this, buildingId, floorId, projectId)
        viewModel.getEquip(deviceId)
        viewModel.getSpace(spaceId)
    }
}