package com.bassbooster.soundeffects.equalizer.view.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.bullhead.equalizer.EqualizerFragment
import com.bassbooster.equalizer.R
import com.bassbooster.equalizer.databinding.FragmentHomeBinding


class HomeFragment : Fragment() {
    val ARG_AUDIO_SESSIOIN_ID = "audio_session_id"
    private var sessionId: Int = 0
    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!
    var equalizerFragment: EqualizerFragment? = null
//    private var mFirebaseAnalytics: FirebaseAnalytics? = null

    companion object {
        fun newInstance(audioSessionId: Int): HomeFragment {
            val args = Bundle()
            args.putInt(HomeFragment().ARG_AUDIO_SESSIOIN_ID, audioSessionId)

            val fragment = HomeFragment()
            fragment.arguments = args
            return fragment
        }
    }

    @SuppressLint("UseRequireInsteadOfGet")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Khởi tạo Firebase Analytics
//        mFirebaseAnalytics = FirebaseAnalytics.getInstance(requireContext())
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val view = binding.root
        setUpViewEqualizes()
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        tracKingFirebaseAnalytics(mFirebaseAnalytics,"home_equalizer_view","View màn Home, tab Equalizer","equalizer_view")
    }

    private fun setUpViewEqualizes() {
        equalizerFragment = EqualizerFragment.newBuilder()
            .setAccentColor(Color.parseColor("#4caf50"))
            .setAudioSessionId(sessionId)
            .build()
        childFragmentManager.beginTransaction()
            .replace(R.id.eqFrame, equalizerFragment!!)
            .commit()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
        if (equalizerFragment != null) {
            equalizerFragment = null
        }
    }

}