package com.gh.gamecenter.fragment

import android.os.Bundle
import android.view.View
import com.gh.gamecenter.common.base.fragment.BaseLazyFragment
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.viewModelProviderFromParent
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.FragmentReloadBinding
import com.gh.gamecenter.wrapper.MainWrapperRepository
import com.gh.gamecenter.wrapper.MainWrapperViewModel
import com.halo.assistant.HaloApp

class ReloadFragment: BaseLazyFragment() {

    private val mBinding by lazy { FragmentReloadBinding.inflate(layoutInflater) }
    private val mViewModel: MainWrapperViewModel by lazy { viewModelProviderFromParent(MainWrapperViewModel.Factory(HaloApp.getInstance())) }

    override fun getLayoutId(): Int = 0
    override fun getInflatedLayout(): View = mBinding.root

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding.reuseLoading.root.visibility = View.VISIBLE
        mBinding.reuseNoConnection.connectionReloadTv.setOnClickListener {
            MainWrapperRepository.getInstance().init()
            mBinding.reuseNoConnection.root.visibility = View.GONE
            mBinding.reuseLoading.root.visibility = View.VISIBLE
        }
        mViewModel.errorLiveData.observeNonNull(this) {
            mBinding.reuseNoConnection.root.visibility = View.VISIBLE
            mBinding.reuseLoading.root.visibility = View.GONE
        }
    }

    override fun onFragmentFirstVisible() {
        super.onFragmentFirstVisible()
        mBaseHandler.post { DisplayUtils.setLightStatusBar(requireActivity(), !mIsDarkModeOn) }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        if (isSupportVisible) {
            DisplayUtils.setLightStatusBar(requireActivity(), !mIsDarkModeOn)
        }
    }
}