package com.gh.gamecenter.personalhome.border

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.RelativeLayout
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.GridSpacingItemColorDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.FragmentAvatarListBinding
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.halo.assistant.HaloApp

class ChooseAvatarBorderFragment : ToolbarFragment() {
    private var mHaveLoad = false
    private lateinit var mBinding: FragmentAvatarListBinding
    private var mAdapter: ChooseAvatarBorderAdapter? = null
    private var mViewModel: ChooseAvatarBorderViewModel? = null
    private var mCategoryId = ""
    private var mIsFree = true

    override fun getLayoutId(): Int = R.layout.fragment_avatar_list

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = FragmentAvatarListBinding.bind(mCachedView)
        mCategoryId = arguments?.getString(EntranceConsts.KEY_CATEGORY_ID) ?: ""
        mIsFree = arguments?.getBoolean(EntranceConsts.KEY_IS_FREE) ?: true
        mViewModel =
            viewModelProvider(ChooseAvatarBorderViewModel.Factory(HaloApp.getInstance().application, mCategoryId))
        mViewModel?.pendantsLiveData?.observe(this, Observer {
            mBinding.reuseLoading.root.visibility = View.GONE
            if (it != null) {
                mBinding.reuseNoConnection.root.visibility = View.GONE
                if (it.isNotEmpty()) {
                    mBinding.reuseNoneData.reuseNoneData.visibility = View.GONE
                    mAdapter?.setListData(it)
                } else {
                    mBinding.reuseNoneData.reuseNoneData.visibility = View.VISIBLE
                }
            } else {
                mBinding.reuseNoneData.reuseNoneData.visibility = View.GONE
                mBinding.reuseNoConnection.root.visibility = View.VISIBLE
            }
        })
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.listRefresh.isEnabled = false
        val params = mBinding.listRefresh.layoutParams as RelativeLayout.LayoutParams
        params.leftMargin = 16f.dip2px()
        params.rightMargin = 16f.dip2px()
        params.topMargin = 15f.dip2px()
        mBinding.listRefresh.layoutParams = params
        mBinding.listRv.apply {
            layoutManager = GridLayoutManager(requireContext(), 3)
            mAdapter = ChooseAvatarBorderAdapter(
                requireContext(),
                this@ChooseAvatarBorderFragment,
                mIsFree,
                mCategoryId
            )
            { entity, isSelected ->
                val avatarPendantFragment = parentFragment as? AvatarBorderFragment
                avatarPendantFragment?.choosePendant(entity, isSelected)
            }
            addItemDecoration(
                GridSpacingItemColorDecoration(
                    requireContext(),
                    8,
                    8,
                    com.gh.gamecenter.common.R.color.transparent
                )
            )
            adapter = mAdapter
        }

        mBinding.reuseNoConnection.root.setOnClickListener {
            mViewModel?.getPendants()
        }
    }

    override fun onResume() {
        super.onResume()
        // 光能兑换挂件分类下默认无使用按钮显示
        setParentBottomVisible(mIsFree)
        if (!mHaveLoad) {
            mViewModel?.getPendants()
            mHaveLoad = true
        } else {
            mAdapter?.notifyDataSetChanged()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE) {
            mViewModel?.getPendants()
        }
    }

    fun setParentBottomVisible(visible: Boolean) {
        (parentFragment as? AvatarBorderFragment)?.setBottomVisible(visible)
    }

    companion object {
        const val REQUEST_CODE = 411
    }
}