package com.dlc.eboard.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.media.MediaPlayer
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dlc.eboard.R
import com.dlc.eboard.common.SensorEnum
import com.dlc.eboard.databinding.DialogWaringBinding
import com.rc.core.ui.widget.decoration.GridSpacingItemDecoration
import com.rc.core.util.DeviceUtils
import com.rc.httpcore.vo.response.LabBulletinBoardVo

/**
 * info
 *
 * @author ReiChin_
 */
class WaringDialog(
    context: Context,
    private val waringData: List<LabBulletinBoardVo>
) :
    Dialog(context, R.style.AlertDialogStyle) {

    private val viewBinding: DialogWaringBinding by lazy {
        DialogWaringBinding.inflate(LayoutInflater.from(context))
    }

    private val mWaringAdapter by lazy { WaringAdapter() }

    fun updateWaringValue(waringData: List<LabBulletinBoardVo>) {
        val newlyData = waringData.filter { SensorEnum.FIRE.funNum != it.funNum }
        if (newlyData.size < 3) {
            viewBinding.largeWaring.visibility = View.GONE
            viewBinding.smallWaring.visibility = View.VISIBLE
            when (newlyData.size) {
                1 -> {
                    viewBinding.item1.visibility = View.VISIBLE
                    viewBinding.item2.visibility = View.GONE
                    viewBinding.item1.text = newlyData[0].formatWaring()
                }
                2 -> {
                    viewBinding.item1.visibility = View.VISIBLE
                    viewBinding.item2.visibility = View.VISIBLE
                    viewBinding.item1.text = newlyData[0].formatWaring()
                    viewBinding.item2.text = newlyData[1].formatWaring()
                }
                else -> {
                    viewBinding.largeWaring.visibility = View.GONE
                    viewBinding.smallWaring.visibility = View.GONE
                }
            }
        } else {
            viewBinding.largeWaring.visibility = View.VISIBLE
            viewBinding.smallWaring.visibility = View.GONE
            mWaringAdapter.setNewInstance(newlyData.toMutableList())
        }
        val unFire = newlyData.size == waringData.size
        viewBinding.waringTitle.text = buildWaringTitle(newlyData, unFire)
    }

    private fun buildWaringTitle(newlyData: List<LabBulletinBoardVo>, unFire: Boolean): String {
        if (newlyData.isEmpty()) return "火灾报警！请立即核实处理。"
        return if (unFire) "传感器监测数据异常报警，请立即核实处理。" else "火灾报警！传感器监测数据异常，请立即核实处理。"
    }

    private val mMediaPlayer by lazy {
        MediaPlayer.create(context, audioResId())
    }

    private fun audioResId() : Int {
        val fireWaring = waringData.find { SensorEnum.FIRE.funNum == it.funNum }
        return when {
            null == fireWaring -> R.raw.audio_waring_sensor
            waringData.size == 1 -> R.raw.audio_waring_fire
            else -> R.raw.audio_waring_fire_sensor
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)
        initWindowConfig()
        initViews()

        mMediaPlayer.start()
    }

    override fun dismiss() {
        super.dismiss()
        mMediaPlayer.stop()
        mMediaPlayer.release()
        if (mAnimDrawable.isRunning) mAnimDrawable.stop()
    }

    private fun initWindowConfig() {
        window?.apply {
            setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL)
            setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        }
    }

    private fun initViews() {
        viewBinding.close.setOnClickListener {
            dismiss()
        }

        viewBinding.waringImg.setImageDrawable(mAnimDrawable)
        mAnimDrawable.start()

        viewBinding.largeWaring.layoutManager = GridLayoutManager(context, 3)
        val decor = GridSpacingItemDecoration(
            3,
            DeviceUtils.dip2px(context, 8f), DeviceUtils.dip2px(context, 12f), true
        )
        viewBinding.largeWaring.addItemDecoration(decor)
        viewBinding.largeWaring.adapter = mWaringAdapter

        updateWaringValue(waringData)
    }

    private val mAnimDrawable by lazy {
        AnimationDrawable().apply {
            addFrame(ContextCompat.getDrawable(context, R.mipmap.img_waring1)!!, 200)
            addFrame(ContextCompat.getDrawable(context, R.mipmap.img_waring2)!!, 200)
            isOneShot = false
        }
    }

}

private class WaringAdapter :
    BaseQuickAdapter<LabBulletinBoardVo, BaseViewHolder>(R.layout.item_waring) {

    override fun convert(holder: BaseViewHolder, item: LabBulletinBoardVo) {
        holder.setText(R.id.content, item.formatWaring())
    }

}