package com.player.musicoo.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.player.musicoo.databinding.FragmentMoHomeBinding
import com.player.musicoo.sp.AppStore
import com.player.musicoo.util.LogTag
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

abstract class MoBaseFragment<T : ViewBinding> : Fragment(), CoroutineScope by MainScope() {

    protected abstract val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> T
    protected lateinit var binding: T
    protected val TAG = LogTag.VO_FRAGMENT_LOG
    protected val appStore by lazy { AppStore(requireContext()) }

    protected abstract suspend fun onViewCreated()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = bindingInflater(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        launch {
            onViewCreated()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        cancel()
    }

    override fun onDestroy() {
        super.onDestroy()
        cancel()
    }
}
