package com.adealink.weparty.commonui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.annotation.CallSuper
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import com.adealink.frame.aab.util.installSplitCompat
import com.adealink.frame.ext.isViewBindingValid
import com.adealink.frame.log.Log
import com.google.firebase.Firebase
import com.google.firebase.crashlytics.crashlytics

open class BaseFragment : Fragment {

    constructor() : super()

    constructor(@LayoutRes contentLayoutId: Int) : super(contentLayoutId)

    open val isLazyInit: Boolean = false //是否懒加载UI
    open val isLazyLoad: Boolean = false //是否懒加载数据
        get() = isLazyInit || field

    open val loadDataWhenResume: Boolean = false //是否每次onResume加载数据

    override fun onAttach(context: Context) {
        super.onAttach(context)
        installSplitCompat(context)
    }

    @CallSuper
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.d("BaseFragment", "BaseFragment($this), onViewCreated")
        if (!isLazyInit) {
            Log.d("BaseFragment", "BaseFragment($this), onViewCreated, init")
            initViews()
            initComponents()
            observeViewModel()
        }

        if (!isLazyLoad) {
            Log.d("BaseFragment", "BaseFragment($this), onViewCreated, loadData")
            loadData()
        }
    }

    private var isFirstInit = true
    private var isFirstResume = true

    override fun onResume() {
        super.onResume()
        Log.i("BaseFragment", "BaseFragment($this), onResume: ${javaClass.simpleName}")
        Firebase.crashlytics.log("BaseFragment onResume: ${javaClass.simpleName}")
        if (isLazyInit && isFirstInit) {
            isFirstInit = false
            Log.d("BaseFragment", "BaseFragment($this), onResume, init")
            initViews()
            initComponents()
            observeViewModel()
        }
        if (isLazyLoad && isFirstResume) {
            isFirstResume = false
            Log.d("BaseFragment", "BaseFragment($this), onResume(lazyLoad), loadData")
            loadData()
        } else if (loadDataWhenResume) {
            Log.d("BaseFragment", "BaseFragment($this), onResume(loadDataWhenResume), loadData")
            loadData()
        }
    }

    @CallSuper
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
    }

    fun isViewCreated(): Boolean {
        return isAdded && view != null
    }

    open fun initViews() {

    }

    open fun observeViewModel() {

    }

    open fun loadData() {

    }

    open fun initComponents() {

    }

    open fun onNewIntent(intent: Intent?) {

    }

    fun showLoading() {
        (activity as? BaseActivity)?.showLoading()
    }

    fun dismissLoading() {
        (activity as? BaseActivity)?.dismissLoading()
    }

    @CallSuper
    override fun onDestroyView() {
        super.onDestroyView()
        dismissLoading()
    }

    open fun onBackPressed(): Boolean {
        return false
    }

    open fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (!isViewBindingValid()) {
            return false
        }
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return onBackPressed()
        }
        return false
    }
}