package com.gh.gamecenter.feedback.view.suggest

import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.entity.SuggestType
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.feedback.databinding.ItemSuggestionBinding
import com.gh.gamecenter.feedback.entity.SuggestionItem

class SuggestionCategoryAdapter(context: Context, private val mArguments: Bundle?) :
    ListAdapter<SuggestionItem>(context) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return SuggestionItemViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is SuggestionItemViewHolder) {
            val entity = mEntityList.safelyGetInRelease(position) ?: return
            holder.binding.run {
                divider.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(mContext))
                titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(mContext))
                desTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))
                titleTv.text = entity.name
                desTv.text = entity.des
                ImageUtils.display(iconIv, entity.icon)

                root.setOnClickListener {
                    (mContext as? ToolBarActivity)?.addFragment(getSuggestFragment(entity.name))
                    SensorsBridge.trackEvent("FeedBackTypeClick", "feedback_type", entity.name)
                }
            }
        }
    }

    private fun getSuggestFragment(name: String): Fragment = when (name) {
        SuggestType.APP.type -> SuggestAppFragment().with(mArguments)
        SuggestType.GAME.type -> SuggestGameFragment().with(mArguments)
        SuggestType.UPDATE.type -> SuggestUpdateFragment().with(mArguments)
        SuggestType.FUNCTION.type -> SuggestFunctionFragment().with(mArguments)
        else -> SuggestCopyrightFragment().with(mArguments)
    }

    override fun getItemCount(): Int = mEntityList.size

    class SuggestionItemViewHolder(val binding: ItemSuggestionBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}