package com.dlc.laboratory.ui.lib

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.view.ContextThemeWrapper
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseDelegateMultiAdapter
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dlc.eboard.R
import com.dlc.eboard.common.SafeSignEnum
import com.dlc.eboard.ui.lib.LabDetailActivity
import com.rc.core.ui.widget.decoration.GridSpacingItemDecoration
import com.rc.core.util.DeviceUtils
import com.rc.httpcore.vo.response.LaboratoryVo

/**
 * info
 *
 * @author ReiChin_
 */
class ExLabDetailActivity : LabDetailActivity() {

    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews(savedInstanceState)

        viewBinding.titleBar.setSigned(false)
        viewBinding.titleBar.setShowNotice(false)
        viewBinding.navView.setShowHome(false)
    }

    override fun enabledLogoutCountDown() = false

    override fun initSubSafeVies(laboratoryVo: LaboratoryVo) {
        // 底部信息栏
        viewBinding.specialFlipper.visibility = View.GONE

        viewBinding.content.layoutManager = GridLayoutManager(this, 2)
        val decor = GridSpacingItemDecoration(
            2,
            DeviceUtils.dip2px(this, 35f),
            DeviceUtils.dip2px(this, 20f), false
        )
        viewBinding.content.addItemDecoration(decor)
        val adapter = ExSubSafeAdapter()
        viewBinding.content.adapter = adapter

        // 主要危险类别、风险防控措施、灭火要点、安全警示标示
        val data = mutableListOf<ExSubSafeData>()
        laboratoryVo.subSafeInfo?.let { info ->
            // 主要危险类别
            if (!info.hazardCategory.isNullOrEmpty()) {
                data.add(ExSubSafeData(ExSubSafeAdapter.TYPE_TEXT, ExSubSafeAdapter.SUB_HAZARD, info.hazardCategory))
            }
            // 风险防控措施
            if (!info.riskMeasure.isNullOrEmpty()) {
                data.add(ExSubSafeData(ExSubSafeAdapter.TYPE_TEXT, ExSubSafeAdapter.SUB_RISK, info.riskMeasure))
            }
            // 灭火要点
            if (!info.outfire.isNullOrEmpty()) {
                data.add(ExSubSafeData(ExSubSafeAdapter.TYPE_TEXT, ExSubSafeAdapter.SUB_OUT_FIRE, info.outfire))
            }
            // 安全警示标示
            if (!info.safeSigns.isNullOrEmpty()) {
                data.add(ExSubSafeData(ExSubSafeAdapter.TYPE_IMAGE, data = info.safeSigns))
            }
        }
        adapter.setNewInstance(data)
    }

}

private class ExSubSafeAdapter :
    BaseDelegateMultiAdapter<ExSubSafeData, BaseViewHolder>() {

    companion object {
        const val TYPE_TEXT = 1
        const val TYPE_IMAGE = 2

        const val SUB_HAZARD = 1 // 主要危险类别
        const val SUB_RISK = 2 // 风险防控措施
        const val SUB_OUT_FIRE = 3 // 灭火要点
    }

    init {
        setMultiTypeDelegate(object : BaseMultiTypeDelegate<ExSubSafeData>() {

            override fun getItemType(data: List<ExSubSafeData>, position: Int) = data[position].type
        })
        getMultiTypeDelegate()!!
            .addItemType(TYPE_TEXT, R.layout.item_sub_safe_text)
            .addItemType(TYPE_IMAGE, R.layout.item_sub_safe_image)
    }

    override fun convert(holder: BaseViewHolder, item: ExSubSafeData) {
        when (holder.itemViewType) {
            TYPE_TEXT -> convertTypeText(holder, item)
            TYPE_IMAGE -> convertTypeImage(holder, item)
        }
    }

    private fun convertTypeText(holder: BaseViewHolder, item: ExSubSafeData) {
        // 主要危险类别、风险防控措施、灭火要点
        when (item.subType) {
            SUB_HAZARD -> updateTextHeader(holder, "主要危险类别", R.drawable.shape_rect_round_11_top_solid_133e93)
            SUB_RISK -> updateTextHeader(holder, "风险防控措施", R.drawable.shape_rect_round_11_top_solid_1f8724)
            SUB_OUT_FIRE -> updateTextHeader(holder, "灭火要点", R.drawable.shape_rect_round_11_top_solid_de4848)
        }
        item.data.forEach {
            val textView = TextView(ContextThemeWrapper(context, R.style.SafetyBoardContent))
            textView.text = it
            holder.getView<LinearLayout>(R.id.hazardCategory).addView(textView)
        }
    }

    private fun updateTextHeader(holder: BaseViewHolder, text: String, resId: Int) {
        holder.setText(R.id.header, text)
        holder.setBackgroundResource(R.id.header, resId)
    }

    private fun convertTypeImage(holder: BaseViewHolder, item: ExSubSafeData) {
        // 安全警示标示
        val safeSignMap = HashMap<String, SafeSignEnum>().apply {
            SafeSignEnum.values().forEach {
                put(it.id, it)
            }
        }
        item.data.forEach { key ->
            val width = context.resources.getDimensionPixelSize(R.dimen.safe_sign_width)
            val height = context.resources.getDimensionPixelSize(R.dimen.safe_sign_height)
            val layoutParams = LinearLayout.LayoutParams(width, height)
            val imageView = ImageView(context)
            imageView.scaleType = ImageView.ScaleType.CENTER_INSIDE
            val iconResId = safeSignMap[key]?.icon ?: R.mipmap.ing_sy_jzsb
            imageView.setImageResource(iconResId)
            holder.getView<LinearLayout>(R.id.outfire).addView(imageView, layoutParams)
        }
    }

}

private data class ExSubSafeData(val type: Int, val subType: Int? = null, val data: List<String>)