package com.example.defenseapplication.liveVideo

import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.defenseapplication.databinding.DeviceInfoActivityBinding
import com.example.defenseapplication.utils.DeviceIdEvent
import com.example.defenseapplication.utils.RetrofitNetwork
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
//设备信息
class DeviceInfoActivity : AppCompatActivity() {
    private lateinit var binding: DeviceInfoActivityBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DeviceInfoActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .titleBar(binding.topBar)
            .init()
        binding.ivBack.setOnClickListener {
            finish()
        }
        fetchDeviceInfo()
    }

    private fun fetchDeviceInfo() {
        val deviceId = DeviceIdEvent.getDeviceId()
        if (deviceId.isNullOrEmpty()) {
            Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
            return
        }

        CoroutineScope(Dispatchers.IO).launch {
            try {
                val response = RetrofitNetwork.getNetworkService().getDeviceInfo(deviceId)
                withContext(Dispatchers.Main) {
                    if (response.code == 200 && response.data != null) {
                        updateDeviceInfo(response.data)
                    } else {
                        Toast.makeText(this@DeviceInfoActivity, response.msg, Toast.LENGTH_SHORT).show()
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
                withContext(Dispatchers.Main) {
                    Toast.makeText(this@DeviceInfoActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    private fun updateDeviceInfo(device: com.example.defenseapplication.bean.DeviceBean) {
        binding.tvDeviceModelValue.text = device.deviceNo.toString()
        binding.tvDeviceNoValue.text = device.deviceSn.toString()
        binding.tvFwVersionValue.text = device.fwVer.toString()
    }
}