package com.sybotan.android.demo.activities

import android.os.Bundle
import android.text.TextUtils
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import com.framework.mvvm.model.db.entity.task.ObjectEntity
import com.sybotan.android.demo.R
import com.sybotan.android.demo.tools.ObjectNameUtils
import com.sybotan.android.demo.viewmodel.SpaceDeliveryCodeVM
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.android.closestDI

/**
 * 空间数字化编码
 */
class SpaceDeliveryCodeActivity : 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

    /** 空间Id */
    private lateinit var spaceId: String

    lateinit var viewModel: SpaceDeliveryCodeVM

    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 titleTv: TextView


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

    private fun initListener() {
        backIv.setOnClickListener { finish() }
        tv1.setOnClickListener {
            edt1.setText(tv1.text.toString())
        }
        tv2.setOnClickListener {
            edt2.setText(tv2.text.toString())
        }
        edt1.addTextChangedListener {
            if (!TextUtils.isEmpty(edt1.text.toString()) && !TextUtils.isEmpty(edt2.text.toString())) {
                submit.isEnabled = true
                submit.alpha = 1f
            } else {
                submit.isEnabled = true
                submit.alpha = 0.5f
            }
        }
        edt2.addTextChangedListener {
            if (!TextUtils.isEmpty(edt1.text.toString()) && !TextUtils.isEmpty(edt2.text.toString())) {
                submit.isEnabled = true
                submit.alpha = 1f
            } else {
                submit.isEnabled = true
                submit.alpha = 0.5f
            }
        }
        submit.setOnClickListener {
            viewModel.insSerialNumber(spaceId, edt1.text.toString(), edt2.text.toString())
        }
    }

    private fun initView() {
        val params = intent.getBundleExtra("params")!!
        buildingId = params.getString("buildingId", "")!!
        floorId = params.getString("floorId", "")!!
        projectId = params.getString("projectId", "")!!
        spaceId = params.getString("spaceId", "")!!
        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)
        titleTv = findViewById(R.id.titleTv)
    }

    private fun initData() {
        viewModel = SpaceDeliveryCodeVM(this.application, { name, param ->
            when (name) {
                SpaceDeliveryCodeVM.FLOOR_INFO -> {
                    if (param != null) {
                        val floorEntity = param as ObjectEntity
                        if (!TextUtils.isEmpty(floorEntity.localId)) {
                            tv1.text = (floorEntity.localId)
                        } else {
                            tv1.text = "??????"
                        }
                        titleTv.text = ("物业分区-${ObjectNameUtils.getEquipName(floorEntity)}")
                    }
                }
                SpaceDeliveryCodeVM.SERIAL_NUMBER -> {
                    if (param != null) {
                        val serialNumber = param as String
                        tv2.text = serialNumber
                    }
                }
                SpaceDeliveryCodeVM.SPACE_DELIVER_CODE -> {
                    if (param != null) {
                        val digitalDeliveryID = param as String
                        edt1.setText(digitalDeliveryID.substringBeforeLast("-RM"))
                        edt2.setText(digitalDeliveryID.substringAfterLast("-RM"))
                    }
                }
            }
        }, this, buildingId, floorId, projectId)
        viewModel.getFloor()
        viewModel.getSpace(spaceId)
    }
}