package com.kaixed.kchat.ui.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.kaixed.kchat.R
import com.kaixed.kchat.databinding.ItemCustomBinding

/**
 * @Author: kaixed
 * @Date: 2024/10/10 22:28
 */
class CustomItem @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private var binding: ItemCustomBinding =
        ItemCustomBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        attrs?.let {
            val typedArray = context.obtainStyledAttributes(it, R.styleable.CustomItem, 0, 0)

            // 获取自定义属性
            val itemName = typedArray.getString(R.styleable.CustomItem_itemName) ?: ""
            val itemIconSize = typedArray.getDimension(R.styleable.CustomItem_iconSize, 0f)
            val itemIcon = typedArray.getResourceId(R.styleable.CustomItem_itemIcon, -1)
            val itemLeftIcon = typedArray.getResourceId(R.styleable.CustomItem_itemLeftIcon, -1)
            val itemIconRound = typedArray.getDimension(R.styleable.CustomItem_iconRound, 0f)
            val itemDesc = typedArray.getString(R.styleable.CustomItem_itemDesc) ?: ""
            val itemRedTip = typedArray.getBoolean(R.styleable.CustomItem_itemRedTip, false)

            // 设置视图
            setupRedTip(itemRedTip)
            setupItemDesc(itemDesc)
            setupLeftIcon(itemLeftIcon)
            setupItemIcon(itemIcon, itemIconSize, itemIconRound)
            setupItemName(itemName)
            setupDividers(typedArray)

            typedArray.recycle()
        }
    }

    // 设置红点显示
    private fun setupRedTip(itemRedTip: Boolean) {
        binding.viewRedTip.visibility = if (itemRedTip) View.VISIBLE else View.GONE
    }

    // 设置描述
    private fun setupItemDesc(itemDesc: String) {
        if (itemDesc.isNotEmpty()) {
            binding.tvItemDesc.visibility = View.VISIBLE
            binding.tvItemDesc.text = itemDesc
        } else {
            binding.tvItemDesc.visibility = View.GONE
        }
    }

    // 设置左侧图标
    private fun setupLeftIcon(itemLeftIcon: Int) {
        if (itemLeftIcon != -1) {
            binding.ivItemLeftIcon.visibility = View.VISIBLE
            binding.ivItemLeftIcon.setImageResource(itemLeftIcon)
        } else {
            binding.ivItemLeftIcon.visibility = View.GONE
        }
    }

    // 设置主图标
    private fun setupItemIcon(itemIcon: Int, itemIconSize: Float, itemIconRound: Float) {
        if (itemIcon != -1) {
            binding.ivItemIcon.visibility = View.VISIBLE
            binding.ivItemIcon.setImageResource(itemIcon)

            if (itemIconSize > 0) {
                binding.ivItemIcon.layoutParams.width = itemIconSize.toInt()
                binding.ivItemIcon.layoutParams.height = itemIconSize.toInt()
            }

            if (itemIconRound > 0) {
                val requestOptions =
                    RequestOptions().transform(RoundedCorners(itemIconRound.toInt()))
                Glide.with(context).load(itemIcon).apply(requestOptions)
                    .into(binding.ivItemIcon)
                binding.ivItemIcon.clipToOutline = true
            }
        } else {
            binding.ivItemIcon.visibility = View.GONE
        }
    }

    // 设置项名称
    private fun setupItemName(itemName: String) {
        if (itemName.isNotEmpty()) {
            binding.tvItemName.text = itemName
        }
    }

    // 设置分割线
    private fun setupDividers(typedArray: TypedArray) {
        val isShowTopDivider =
            typedArray.getBoolean(R.styleable.CustomItem_isTopDividerVisible, false)
        val isShowBottomDivider =
            typedArray.getBoolean(R.styleable.CustomItem_isBottomDividerVisible, false)

        binding.decorationTop.visibility = if (isShowTopDivider) View.VISIBLE else View.GONE
        binding.decorationBottom.visibility = if (isShowBottomDivider) View.VISIBLE else View.GONE
    }

    // 设置点击事件
    fun setOnItemClickListener(listener: OnClickListener) {
        binding.root.setOnClickListener(listener)
    }

    // 设置红点的可见性
    fun setRedTipVisibility(visible: Boolean) {
        binding.viewRedTip.visibility = if (visible) View.VISIBLE else View.GONE
    }

    // 设置描述
    fun setItemDesc(str: String) {
        binding.tvItemDesc.text = str
    }
}
