package com.example.defenseapplication.liveVideo

import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.defenseapplication.adapter.AlarmLogAdapter
import com.example.defenseapplication.databinding.AlarmLogActivityBinding
import com.gyf.immersionbar.ImmersionBar
import java.util.*
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.example.defenseapplication.R

//告警记录
class AlarmLogActivity : AppCompatActivity() {
    private lateinit var binding: AlarmLogActivityBinding
    private lateinit var calendar: Calendar
    private var adapter: AlarmLogAdapter? = null
    private val alarmList = mutableListOf<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = AlarmLogActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .titleBar(binding.topBar)
            .init()

        calendar = Calendar.getInstance()
        updateDateDisplay()
        initRecyclerView()
        loadAlarmList()
        initListener()
    }

    private fun initRecyclerView() {
        adapter = AlarmLogAdapter(alarmList)
        binding.alarmListRecyclerView.layoutManager = LinearLayoutManager(this)
        binding.alarmListRecyclerView.adapter = adapter
    }

    private fun loadAlarmList() {
        alarmList.clear()
        alarmList.add("入侵告警 - 10:30")
        alarmList.add("入侵告警 - 09:15")
        alarmList.add("入侵告警 - 08:45")
        alarmList.add("入侵告警 - 07:20")
        alarmList.add("入侵告警 - 06:50")
        adapter?.notifyDataSetChanged()
    }

    private fun updateDateDisplay() {
        val month = calendar.get(Calendar.MONTH) + 1
        val day = calendar.get(Calendar.DAY_OF_MONTH)
        binding.tvDate.text = "${month}月${day}日"
    }

    private fun initListener() {
        binding.btnBack.setOnClickListener {
            finish()
        }
        binding.expandImg.setOnClickListener {
            showDatePicker()
        }
    }

    private fun showDatePicker() {
        val timePicker = TimePickerBuilder(this, OnTimeSelectListener { date, _ ->
            calendar.time = date
            updateDateDisplay()
            loadAlarmList()
        })
            .setType(booleanArrayOf(true, true, true, false, false, false)) // 年月日
            .setTitleText(getString(R.string.select_date))
            .setSubmitColor(Color.parseColor("#09C2BD"))//确定按钮文字颜色
            .setCancelColor(Color.parseColor("#09C2BD"))//取消按钮文字颜色
            .setSubCalSize(18)//确定和取消文字大小
            .setTextColorCenter(Color.parseColor("#09C2BD"))//选中颜色
            .setLineSpacingMultiplier(2.5f)//行间距倍数
            .setItemVisibleCount(5)//可见项数量
            .build()
        timePicker.show()
    }
}
