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

import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.bullhead.equalizer.ButtonController
import com.bassbooster.equalizer.databinding.FragmentMyEffectsBinding
import com.bassbooster.soundeffects.equalizer.event.MessageEvent
import com.bassbooster.soundeffects.equalizer.view.activity.effects.adapter.MyEffectsAdapter
import com.bassbooster.soundeffects.equalizer.viewmodel.MyViewModel
import org.greenrobot.eventbus.EventBus

class MyEffectsFragment : Fragment() {
    private var _binding: FragmentMyEffectsBinding? = null
    private val binding get() = _binding!!
    private var myEffectsAdapter: MyEffectsAdapter? = null
    private lateinit var viewModel: MyViewModel
    private var isCheck = false
//    private var mFirebaseAnalytics: FirebaseAnalytics? = null

    companion object {
        fun newInstance() = MyEffectsFragment()
    }

    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 = FragmentMyEffectsBinding.inflate(inflater, container, false)
        val view = binding.root
        return view
    }

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

    override fun onResume() {
        super.onResume()
    }

    private fun init() {
//        tracKingFirebaseAnalytics(mFirebaseAnalytics,"home_list_genres_view","View màn My List Effect","list_genres")

        val buttonController = ButtonController()
        val numberOfColumns = 3
        val layoutManager = GridLayoutManager(requireContext(), numberOfColumns)
        binding.mRcyclerEffects.layoutManager =
            layoutManager
        viewModel = ViewModelProvider(this).get(MyViewModel::class.java)

        viewModel.allData.observe(requireActivity(), Observer { myData ->
            if (myData.isEmpty()) {
                binding.imgIsEmpty.visibility = View.VISIBLE
                binding.tvIsEmpty.visibility = View.VISIBLE
                myEffectsAdapter?.updateData(myData)
                binding.mRcyclerEffects.adapter = myEffectsAdapter
            } else {
                binding.imgIsEmpty.visibility = View.GONE
                binding.tvIsEmpty.visibility = View.GONE
                Log.e("qqq", "setOnEvent: " + myData.size)
                myEffectsAdapter = MyEffectsAdapter(
                    requireActivity(),
                    myData ?: listOf(),
                    object : MyEffectsAdapter.OnItemClickListener {
                        override fun onItemClick(position: Int) {
                            EventBus.getDefault().post(MessageEvent(myData[position].name))

                            val currentActivity: Activity? = activity
                            currentActivity?.finish()
                            buttonController.onUpdateMyType(position,myData[position].name,myData[position].seekbar0,myData[position].seekbar1,myData[position].seekbar2,myData[position].seekbar3,myData[position].seekbar4)
                        }

                        override fun onDelete(position: Int) {
                            viewModel.deleteData(myData[position])
                            isCheck = true
                            myEffectsAdapter?.notifyDataSetChanged()
                        }
                    })
                if (isCheck) {
                    myEffectsAdapter?.setDeleteIconVisibility(true)
                } else {
                    myEffectsAdapter?.setDeleteIconVisibility(false)
                }
            }
            binding.mRcyclerEffects.adapter = myEffectsAdapter
            myEffectsAdapter?.notifyDataSetChanged()

        })
    }

    fun getMyEffectsAdapter(): MyEffectsAdapter? {
        val recyclerView = binding.mRcyclerEffects
        return recyclerView.adapter as? MyEffectsAdapter
    }
}