package com.gh.gamecenter.personalhome

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.view.SpacingItemDecoration
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.entity.FollowersOrFansEntity
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.message.MessageUnreadViewModel
import com.gh.gamecenter.personalhome.fans.FansActivity
import com.halo.assistant.HaloApp

class FollowersOrFansFragment : ListFragment<FollowersOrFansEntity, FollowersOrFansViewModel>() {

    companion object {
        const val PAGE_SOURCE = "PAGE_SOURCE"
    }

    private var mUnreadViewModel: MessageUnreadViewModel? = null

    private var mAdapter: FollowersOrFansAdapter? = null

    override fun provideListAdapter(): FollowersOrFansAdapter {
        if (mAdapter == null) mAdapter = FollowersOrFansAdapter(requireContext(), mListViewModel, mEntrance)
        return mAdapter!!
    }

    override fun provideListViewModel(): FollowersOrFansViewModel {
        val factory = FollowersOrFansViewModel.Factory(
            HaloApp.getInstance().application,
            arguments?.getString(PAGE_SOURCE)!!,
            arguments?.getString(EntranceConsts.KEY_USER_ID)!!
        )
        return ViewModelProviders.of(this, factory).get(FollowersOrFansViewModel::class.java)
    }

    override fun getItemDecoration() = SpacingItemDecoration(onlyDecorateTheFirstItem = true, top = 8F.dip2px())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val factory = MessageUnreadViewModel.Factory(HaloApp.getInstance().application)
        mUnreadViewModel = ViewModelProviders.of(this, factory).get(MessageUnreadViewModel::class.java)
        mListViewModel.followingLiveData.observe(this, Observer {
            if (it != null && mAdapter != null && mAdapter!!.itemCount > it) {
                mAdapter?.notifyItemChanged(it)
            }
        })
        if (mListViewModel.mPageSource == FansActivity::class.java.name) {
            if (mListViewModel.userId == UserManager.getInstance().userId) {
                setNavigationTitle("我的粉丝")
            } else {
                setNavigationTitle("Ta的粉丝")
            }
        } else {
            if (mListViewModel.userId == UserManager.getInstance().userId) {
                setNavigationTitle("我的关注")
            } else {
                setNavigationTitle("Ta的关注")
            }
        }

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mAdapter?.onBadgeClickListener = {
            val key = if (mListViewModel.mPageSource == FansActivity::class.java.name) {
                "个人主页-粉丝页面"
            } else {
                "个人主页-关注页面"
            }
            MtaHelper.onEvent("进入徽章墙_用户记录", key, "${it.name}（${it.id}）")
            MtaHelper.onEvent("徽章中心", "进入徽章中心", key)
        }
    }

    override fun onLoadDone() {
        super.onLoadDone()
        if (mListViewModel.mPageSource == FansActivity::class.java.name) {
            mUnreadViewModel?.markRead(MessageUnreadViewModel.ReadType.FANS)
        }
    }

}