package com.example.defenseapplication.home

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.example.defenseapplication.R
import com.example.defenseapplication.adapter.AddDeviceAdapter
import com.example.defenseapplication.adapter.Device
import com.example.defenseapplication.databinding.AddDeviceActivityBinding
import com.gyf.immersionbar.ImmersionBar

//添加设备页面
class AddDeviceActivity : AppCompatActivity() {

    private lateinit var binding: AddDeviceActivityBinding
    private lateinit var adapter: AddDeviceAdapter
    private val mainHandler = Handler(Looper.getMainLooper())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = AddDeviceActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .titleBar(binding.topBar)
            .init()
        binding.ivBack.setOnClickListener {
            finish()
        }

        // 开始播放扫描动画，动画结束后再显示设备列表
        startScanAnimationThenShowList()
    }

    private fun startScanAnimationThenShowList() {
        // 显示动画层
        binding.animationContainer.visibility = View.VISIBLE
        binding.rvDevices.visibility = View.GONE

        // 设置帧动画并启动
        binding.ivScanAnimation.setImageResource(R.drawable.device_frame_animation)
        val scanDrawable = binding.ivScanAnimation.drawable
        if (scanDrawable is android.graphics.drawable.AnimationDrawable) {
            scanDrawable.start()
        }

        // 模拟扫描过程
        mainHandler.postDelayed({
            // 停止动画
            (binding.ivScanAnimation.drawable as? android.graphics.drawable.AnimationDrawable)?.stop()
            // 隐藏动画层，显示列表
            binding.animationContainer.visibility = View.GONE
            binding.rvDevices.visibility = View.VISIBLE
            setupDeviceList() // 加载设备列表
        }, 3000) // 动画播放时长，可根据实际帧动画总时长调整
    }

    private fun setupDeviceList() {
        val devices = listOf(
            Device(getString(R.string.smart_hanging_defense), "device1"),
            Device(getString(R.string.smart_defense_device), "device2")
        )

        if (devices.isEmpty()) {

            binding.rvDevices.visibility = View.GONE
        } else {

            binding.rvDevices.visibility = View.VISIBLE

            binding.rvDevices.layoutManager = GridLayoutManager(this, 2)
            adapter = AddDeviceAdapter(devices) { device ->
                val intent = Intent(this, AddFamilyActivity::class.java)
                intent.putExtra("device_name", device.name)
                startActivity(intent)
            }
            binding.rvDevices.adapter = adapter
        }
    }
    override fun onPause() {
        super.onPause()
        // 停止所有可见 item 的动画
        for (i in 0 until binding.rvDevices.childCount) {
            val child = binding.rvDevices.getChildAt(i)
            val holder = binding.rvDevices.getChildViewHolder(child) as? AddDeviceAdapter.ViewHolder
            holder?.stopAnimation()
        }
    }

    override fun onResume() {
        super.onResume()
        // 重新启动所有可见 item 的动画
        for (i in 0 until binding.rvDevices.childCount) {
            val child = binding.rvDevices.getChildAt(i)
            val holder = binding.rvDevices.getChildViewHolder(child) as? AddDeviceAdapter.ViewHolder
            holder?.startAnimation()
        }
    }
}