package com.gh.gamecenter.subject

import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.gh.gamecenter.common.base.fragment.LazyFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.PageSwitchDataHelper
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.FragmentSubjectBinding
import com.gh.gamecenter.entity.SubjectSettingEntity
import com.gh.gamecenter.subject.rows.SubjectRowsFragment
import com.gh.gamecenter.subject.tab.SubjectTabFragment
import com.gh.gamecenter.subject.tile.SubjectTileFragment
import com.halo.assistant.HaloApp

class SubjectFragment : LazyFragment() {

    private var mBinding: FragmentSubjectBinding? = null
    private var mViewModel: SubjectViewModel? = null

    override fun getRealLayoutId() = R.layout.fragment_subject

    override fun onRealLayoutInflated(inflatedView: View) {
        super.onRealLayoutInflated(inflatedView)
        mBinding = FragmentSubjectBinding.bind(inflatedView)
    }

    override fun onFragmentFirstVisible() {
        initMenu(R.menu.menu_download)
        val factory = SubjectViewModel.Factory(
            HaloApp.getInstance().application,
            arguments?.getParcelable(EntranceConsts.KEY_SUBJECT_DATA)
        )
        mViewModel = ViewModelProviders.of(this, factory).get(SubjectViewModel::class.java)

        super.onFragmentFirstVisible()

        PageSwitchDataHelper.popLastPageData()?.let {
            arguments?.putSerializable(LAST_PAGE_DATA, it)
        }

        // 获取专题配置
        mViewModel?.subjectSettingLD?.observe(this, Observer {
            mBinding?.loadingContainer?.reuseLlLoading?.visibility = View.GONE
            if (it == null) {
                mBinding?.noConnectionContainer?.reuseNoConnection?.visibility = View.VISIBLE
            } else {
                showAdLabel(it.adIconActive)
                initContentView(it)
            }
        })

        // 获取专题名称
        mViewModel?.subjectNameLD?.observe(this, Observer {
            setNavigationTitle(it)
        })
    }

    override fun initRealView() {
        super.initRealView()

        mBinding?.loadingContainer?.reuseLlLoading?.setOnClickListener {}
        mBinding?.noConnectionContainer?.reuseNoConnection?.setOnClickListener {
            mBinding?.loadingContainer?.reuseLlLoading?.visibility = View.VISIBLE
            mBinding?.noConnectionContainer?.reuseNoConnection?.visibility = View.GONE
            mViewModel?.initData()
        }
    }

    private fun initContentView(entity: SubjectSettingEntity) {
        val transaction = childFragmentManager.beginTransaction()
        val tag: String
        val fragment: Fragment = when {
            "tile" == entity.typeEntity.layout -> {
                tag = SubjectTileFragment::class.java.name
                childFragmentManager.findFragmentByTag(tag)
                    ?: SubjectTileFragment()
            }
            "rows" == entity.typeEntity.layout -> {
                tag = SubjectRowsFragment::class.java.name
                childFragmentManager.findFragmentByTag(tag)
                    ?: SubjectRowsFragment()
            }
            else -> {
                tag = SubjectTabFragment::class.java.name
                childFragmentManager.findFragmentByTag(tag)
                    ?: SubjectTabFragment()
            }
        }
        val bundle = arguments
        bundle?.putParcelable(EntranceConsts.KEY_SUBJECT_DATA, mViewModel?.subjectData)
        bundle?.putParcelable(SubjectSettingEntity::class.java.simpleName, entity)
        fragment.arguments = bundle
        transaction.replace(R.id.subject_content, fragment, tag)
        transaction.commitAllowingStateLoss()
    }

    companion object {
        const val LAST_PAGE_DATA = "last_page_data"
    }

}