package com.adealink.weparty.ui.home

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.base.AppBase
import com.adealink.frame.log.Log
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.startup.DistributedLoadManager
import com.adealink.frame.util.onClick
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.AppModule
import com.adealink.weparty.R
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.constant.TAG_TIME_APP_START
import com.adealink.weparty.constant.logTime
import com.adealink.weparty.databinding.FragmentGuestHomeBinding
import com.adealink.weparty.debug.Debug
import com.adealink.weparty.module.account.AccountModule
import com.adealink.weparty.module.playmate.Playmate
import java.util.LinkedList
import java.util.Queue

class GuestHomeFragment : BaseFragment(R.layout.fragment_guest_home) {

    companion object {
        const val TAG = "GuestHomeFragment"

        fun newInstance(mainTabKey: String?, subTabKey: String?): GuestHomeFragment {
            return GuestHomeFragment().apply {
                arguments = Bundle().apply {
                    putString(AppModule.Main.EXTRA_MAIN_TAB, mainTabKey)
                    putString(AppModule.Main.EXTRA_MAIN_SUB_TAB, subTabKey)
                }
            }
        }
    }

    private val binding by viewBinding(FragmentGuestHomeBinding::bind)
    private lateinit var dlManager: DistributedLoadManager

    @SuppressLint("MissingSuperCall")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        logTime(TAG_TIME_APP_START, "GuestHomeFragment.onViewCreated()")
        binding.clTopBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = activity.statusBarHeight()
        }
        dlManager = DistributedLoadManager(lifecycle, stepsLoad())
        activity?.let { dlManager.triggerDistributedLoad(it) }
    }

    private fun stepsLoad(): Queue<Runnable> {
        val steps = LinkedList<Runnable>()
        steps.add(Runnable {
            Log.d(TAG, "HomeFragment.step1: initViews, initComponents, observeViewModel")
            initViews()
            initComponents()
            observeViewModel()
        })
        steps.add(Runnable {
            Log.d(TAG, "HomeFragment.step2: loadData")
            loadData()
        })
        return steps
    }

    override fun initViews() {
        super.initViews()
        //顶部栏控件
        //Debug测试按钮
        if (!AppBase.isRelease) {
            binding.ivDebug.show()
            binding.ivDebug.onClick {
                activity?.let { act ->
                    Router.build(act, Debug.Debug.PATH).start()
                }
            }
        }
        binding.root.onClick {
            AccountModule.showLoginDialog(activity?.supportFragmentManager)
        }
        inflatePlaymateHome()
    }

    private fun inflatePlaymateHome() {
        var fragment =
            childFragmentManager.findFragmentByTag(Playmate.Home.GUEST_PATH) as? BaseFragment
        if (fragment?.isAdded == true) {
            return
        }
        fragment = Router.getRouterInstance<BaseFragment>(Playmate.Home.GUEST_PATH) ?: return
        childFragmentManager.beginTransaction()
            .replace(binding.flContent.id, fragment, Playmate.Home.GUEST_PATH)
            .commitAllowingStateLoss()
    }

}