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

import android.content.Intent
import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.sybotan.android.demo.R
import com.sybotan.android.demo.activities.BaseActivity
import com.sybotan.android.demo.bean.request.ReqDeviceJob
import com.sybotan.android.demo.bean.result.Position
import com.sybotan.android.demo.entitiies.DeviceType
import com.sybotan.android.demo.retrofit.BaseViewModelInterface
import com.sybotan.android.demo.viewmodel.DeviceVM
import com.zbar.lib.CaptureActivity

class DeviceActivity : BaseActivity() {
    companion object {
        const val REQ_CODE_DEVICE = 1
        const val REQ_CODE_QR = 2
        const val REQ_CODE_INFO = 3
        const val REQ_CODE_POS = 4

        const val RES_CODE_DEVICE = 1
        const val RES_CODE_QR = 2
        const val RES_CODE_INFO = 3
        const val RES_CODE_POS = 4
    }

    private lateinit var infoMsgTv: TextView
    private lateinit var infoOkIv: ImageView

    private lateinit var deviceOkIv: ImageView
    private lateinit var deviceInfoTv: TextView

    private lateinit var qrOkIv: ImageView
    private lateinit var qrInfoTv: TextView

    private val qrCodes = mutableSetOf<String>()
    private var spaceId = ""
    private var position: Position? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_device)

        val intent = intent
        val bundle = intent.getBundleExtra("equip")
        spaceId = bundle?.getString("spaceId") ?: ""
        position = bundle?.getSerializable("position") as? Position

        val positionOkIv = findViewById<ImageView>(R.id.positionOkIv)
        positionOkIv.visibility = if (position != null) View.VISIBLE else View.INVISIBLE
        val positionInfoTv = findViewById<TextView>(R.id.positionInfoTv)
        positionInfoTv.text =
            position?.let { "${it.z.region}(${it.x},${it.y},${it.z.offset})" } ?: ""

        val deviceCl = findViewById<ConstraintLayout>(R.id.deviceCl)
        deviceOkIv = findViewById(R.id.deviceOkIv)
        deviceInfoTv = findViewById(R.id.deviceInfoTv)

        deviceCl.setOnClickListener {
            val intent = Intent(this, DeviceTypeActivity::class.java)
            startActivityForResult(intent, REQ_CODE_DEVICE)
        }

        val infoCl = findViewById<ConstraintLayout>(R.id.infoCl)
        infoCl.setOnClickListener {
            val intent = Intent(this, RecordInfoActivity::class.java)
            startActivityForResult(intent, REQ_CODE_INFO)
        }
        infoMsgTv = findViewById(R.id.infoMsgTv)
        infoOkIv = findViewById(R.id.infoOkIv)

        val qrCl = findViewById<ConstraintLayout>(R.id.qrCl)
        qrCl.setOnClickListener {
            val intent = Intent(this, CaptureActivity::class.java)
            startActivityForResult(intent, REQ_CODE_QR)
        }
        qrOkIv = findViewById(R.id.qrOkIv)
        qrInfoTv = findViewById(R.id.qrInfoTv)

        val saveBtn = findViewById<Button>(R.id.saveBtn)
        saveBtn.setOnClickListener {
            val id = spaceId
            if (id.isEmpty()) {
                return@setOnClickListener
            }
            val type = mType
            if (type.isEmpty()) {
                return@setOnClickListener
            }
            val remark = mRemark
            if (remark.isEmpty()) {
                return@setOnClickListener
            }

            val pos = position ?: return@setOnClickListener

            val deivce = ReqDeviceJob(
                spaceId = id,
                type = type,
                remark = remark,
                position = pos
            )

            mDeviceModel.addDevice(deivce)
        }

        initViewModel()
    }

    private lateinit var mDeviceModel: DeviceVM
    private fun initViewModel() {
        mDeviceModel = DeviceVM(BaseViewModelInterface { name, params ->
            if (name == DeviceVM.DEVICE_ADD) {
                setResult(RESULT_OK)
                finish()
            }
        }, this)
    }

    private var mType = ""
    private var mRemark = ""
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // 设备类型信息
        if (requestCode == REQ_CODE_DEVICE && resultCode == RES_CODE_DEVICE) {
            val bundle = data?.extras
            val type = bundle?.getParcelable<DeviceType?>("type")
            type?.let { mType = it.name }
            deviceInfoTv.text = type?.name ?: ""
            deviceOkIv.visibility = if (type != null) View.VISIBLE else View.INVISIBLE
        }

        // 二维码信息
        if (requestCode == REQ_CODE_QR && resultCode == RESULT_OK) {
            val bundle = data?.extras
            val result = bundle?.getString(CaptureActivity.EXTRA_STRING) ?: ""
            qrCodes.add(result)
            qrOkIv.visibility = if (result.isNotEmpty()) View.VISIBLE else View.INVISIBLE
            qrInfoTv.text = "${qrCodes.size} 码"
        }

        // 记录文本信息
        if (requestCode == REQ_CODE_INFO && resultCode == RES_CODE_INFO) {
            val bundle = data?.extras
            mRemark = bundle?.getString("info") ?: ""
            infoMsgTv.text = mRemark
            infoOkIv.visibility = if (mRemark.isNotEmpty()) View.VISIBLE else View.INVISIBLE
        }
    }
}