package com.gh.gamecenter.wrapper


import android.os.Bundle
import androidx.fragment.app.Fragment
import com.gh.common.iinterface.ISuperiorChain
import com.gh.common.util.ViewPagerFragmentHelper
import com.gh.gamecenter.common.base.adapter.BaseDiffFragmentStateAdapter
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.entity.BottomTab
import com.gh.gamecenter.fragment.ReloadFragment

class MainFragmentStateAdapter(private val mFragment: Fragment) : BaseDiffFragmentStateAdapter<BottomTab>(mFragment) {
    override fun createFragment(data: BottomTab?, position: Int): Fragment {
        if (data == null) return ReloadFragment()

        val bundle = Bundle()
        val superiorChain = if (mFragment is ISuperiorChain) mFragment else null
        mFragment.arguments?.let { bundle.putAll(it) }
        bundle.putBoolean(EntranceConsts.KEY_IS_HOME, true)
        bundle.putString(EntranceConsts.KEY_BOTTOM_TAB_ID, data.id)
        bundle.putString(EntranceConsts.KEY_BOTTOM_TAB_NAME, data.name)
        bundle.putInt(EntranceConsts.KEY_BOTTOM_TAB_INDEX, position)
        bundle.putInt(EntranceConsts.KEY_POSITION, position)

        val exposureSourceList = arrayListOf(ExposureSource("底部tab", data.name))
        data.diverter?.let {
            exposureSourceList.add(
                ExposureSource(
                    "分流器",
                    "${it.diverterData.diverterName}+${it.diverterData.diverterId}"
                )
            )
        }
        bundle.putParcelableArrayList(EntranceConsts.KEY_EXPOSURE_SOURCE_LIST, exposureSourceList)
        bundle.putBoolean(EntranceConsts.KEY_IS_FROM_MAIN_WRAPPER, true)
        return if (data.link == null) {
            ReloadFragment()
        } else {
            when (data.link.type) {
                ViewPagerFragmentHelper.TYPE_CUSTOM_PAGE -> {
                    bundle.putParcelable(LinkEntity::class.java.simpleName, data.link)
                    SearchToolbarTabWrapperFragment().setSuperiorChain(superiorChain).apply { arguments = bundle }
                }

                ViewPagerFragmentHelper.TYPE_MULTI_TAB_NAV -> {
                    bundle.putParcelable(BottomTab.SearchStyle::class.java.simpleName, data.searchStyle)
                    bundle.putString(EntranceConsts.KEY_MULTI_TAB_NAV_ID, data.link.link)
                    bundle.putString(EntranceConsts.KEY_MULTI_TAB_NAV_NAME, data.link.text)
                    SearchToolbarTabWrapperFragment().setSuperiorChain(superiorChain).apply { arguments = bundle }
                }

                else -> ViewPagerFragmentHelper.createFragment(mFragment, bundle, data.link, false)
            }
        }
    }

    override fun areItemsTheSame(oldItem: BottomTab?, newItem: BottomTab?): Boolean {
        return oldItem?.link?.type == newItem?.link?.type
    }

    override fun areContentsTheSame(oldItem: BottomTab?, newItem: BottomTab?): Boolean {
        return oldItem?.link?.link == newItem?.link?.link
    }
}
