package com.gh.gamecenter.gamedetail.dialog

import android.content.Context
import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.GamePermissionItemBinding
import com.gh.gamecenter.gamedetail.entity.Permission
import com.lightgame.adapter.BaseRecyclerAdapter

class GamePermissionAdapter(context: Context, val permissions: List<Permission>) :
    BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return GamePermissionViewHolder(parent.toBinding())
    }

    override fun getItemCount(): Int = permissions.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GamePermissionViewHolder) {
            val permission = permissions[position]
            holder.binding.hintIv.setImageResource(if (position == 0) R.drawable.ic_sensitive_permission else R.drawable.ic_standard_permission)
            holder.binding.permissionNameTV.text = permission.name
            holder.binding.permissionDetailLl.removeAllViews()
            permission.detail?.forEach {
                holder.binding.permissionDetailLl.addView(createTitleView(it.title ?: ""))
                holder.binding.permissionDetailLl.addView(createDescView(it.intro ?: ""))
            }
        }
    }

    private fun createTitleView(title: String): View {
        return TextView(mContext).apply {
            val params = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            params.topMargin = 10F.dip2px()
            layoutParams = params
            textSize = 14F
            typeface = Typeface.DEFAULT_BOLD
            setTextColor(ContextCompat.getColor(mContext, R.color.text_title))
            text = title
        }
    }

    private fun createDescView(desc: String): View {
        return TextView(mContext).apply {
            val params = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            params.topMargin = 6F.dip2px()
            layoutParams = params
            textSize = 12F
            setTextColor(ContextCompat.getColor(mContext, R.color.text_subtitleDesc))
            text = desc
        }
    }

    class GamePermissionViewHolder(val binding: GamePermissionItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}