package com.gh.gamecenter.qa.dialog

import android.app.Activity
import android.content.Context
import android.graphics.Point
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewsUtils
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.databinding.DialogAskFollowMoreBinding
import com.gh.gamecenter.databinding.ItemAskFollowMoreBinding
import com.gh.gamecenter.entity.UserEntity

class AskFollowMoreDialog : BaseDialogFragment() {

    private var mType = ""
    private var mPath = ""
    private var mUserList = ArrayList<UserEntity>()
    private lateinit var mBinding: DialogAskFollowMoreBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requireArguments().run {
            mType = getString(KEY_TYPE) ?: ""
            mPath = getString(KEY_PATH) ?: ""
            mUserList = getParcelableArrayList(KEY_USER_LIST) ?: arrayListOf()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return DialogAskFollowMoreBinding.inflate(LayoutInflater.from(requireContext()), null, false).apply {
            mBinding = this
        }.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        dialog?.setCanceledOnTouchOutside(true)

        val lp = mBinding.container.layoutParams
        lp.width = ((getScreenWidth(requireActivity())) * 0.8).toInt()
        mBinding.container.layoutParams = lp

        mBinding.tvTitle.text = mType
        mBinding.recyclerview.adapter = AskFollowMoreAdapter(requireContext(), mUserList, mPath)
        mBinding.recyclerview.layoutManager = LinearLayoutManager(mBinding.recyclerview.context)
    }

    private fun getScreenWidth(activity: Activity): Int {
        val size = Point()
        activity.windowManager.defaultDisplay.getSize(size)
        return size.x
    }

    class AskFollowMoreAdapter(var context: Context, var list: ArrayList<UserEntity>, var path: String) :
        RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return ItemViewHolder(parent.toBinding())
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            (holder as ItemViewHolder).run {
                val user = list[position]
                ImageUtils.display(binding.ivIcon, user.icon)
                binding.tvUsername.text = user.name
                binding.tvTime.text = NewsUtils.getFormattedTime(user.time ?: 0)
                binding.root.setOnClickListener {
                    DirectUtils.directToHomeActivity(context, list[position].id, "问答-关注", path)
                }
            }
        }

        class ItemViewHolder(var binding: ItemAskFollowMoreBinding) : RecyclerView.ViewHolder(binding.root)
    }

    companion object {
        const val KEY_TYPE = "type"
        const val KEY_PATH = "path"
        const val KEY_USER_LIST = "user_list"

        @JvmStatic
        fun getInstance(type: String, path: String, userList: ArrayList<UserEntity>) = AskFollowMoreDialog().apply {
            arguments = Bundle().apply {
                putString(KEY_TYPE, type)
                putString(KEY_PATH, path)
                putParcelableArrayList(KEY_USER_LIST, userList)
            }
        }
    }
}