package com.kaixed.kchat.view.widget

import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
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
import com.kaixed.kchat.view.activity.ProfileDetailActivity

/**
 * @Author: kaixed
 * @Date: 2024/10/10 22:28
 * @Description: 自定义Item控件，以便于复用以减少重复代码
 */
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 itemIconRound = typedArray.getDimension(R.styleable.CustomItem_iconRound, 0f)
            val itemDesc = typedArray.getString(R.styleable.CustomItem_itemDesc) ?: ""

            val isShowTopDivider =
                typedArray.getBoolean(R.styleable.CustomItem_isTopDividerVisible, false)
            val isShowBottomDivider =
                typedArray.getBoolean(R.styleable.CustomItem_isBottomDividerVisible, false)



            if (itemDesc.isNotEmpty()) {
                binding.tvItemDesc.visibility = View.VISIBLE
                binding.tvItemDesc.text = itemDesc
            } else {
                binding.tvItemDesc.visibility = View.GONE
            }

            if (itemIcon != -1) {
                binding.ivItemIcon.visibility = View.VISIBLE
                binding.ivItemIcon.setImageResource(itemIcon)

                val displayMetrics = context.resources.displayMetrics

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

                if (itemIconSize.toInt() != 0) {

                    binding.ivItemIcon.layoutParams.width =
                        (itemIconSize * displayMetrics.density).toInt()
                    binding.ivItemIcon.layoutParams.height =
                        (itemIconSize * displayMetrics.density).toInt()
                } else {
                    binding.ivItemIcon.visibility = View.GONE
                }

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

            if (itemName.isNotEmpty()) {
                binding.tvItemName.text = itemName
            }

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

            binding.decorationBottom.visibility = if (isShowBottomDivider) {
                View.VISIBLE
            } else {
                View.GONE
            }
            typedArray.recycle()
        }

//        binding.root.setOnClickListener {
//            val activity = ""
//            val fullClassName = "com.kaixed.kchat.view.activity.$activity"
//
//            try {
//                val activityClass = Class.forName(fullClassName)
//                val intent = Intent(context, activityClass)
//                context.startActivity(intent)
//            } catch (e: ClassNotFoundException) {
//                Toast.makeText(context, "跳转出错，请重试", Toast.LENGTH_LONG).show()
//            }
//        }

    }


    fun setOnItemClickListener(listener: OnClickListener) {
        binding.root.setOnClickListener(listener)
    }
}