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

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.bullhead.equalizer.ButtonController
import com.bassbooster.equalizer.R
import com.bassbooster.soundeffects.equalizer.data.model.EffectsModel
import com.bassbooster.equalizer.databinding.FragmentEffectsBinding
import com.bassbooster.soundeffects.equalizer.event.MessageEvent
import com.bassbooster.soundeffects.equalizer.view.activity.effects.adapter.EffectsAdapter
import org.greenrobot.eventbus.EventBus
import java.util.*


class EffectsFragment : Fragment() {
    val EFFECTS_TYPE = "effects_type"

    private var _binding: FragmentEffectsBinding? = null
    private val binding get() = _binding!!
    private var effectsAdapter : EffectsAdapter? = null
//    private var mFirebaseAnalytics: FirebaseAnalytics? = null

    companion object {
        fun newInstance(listEffectsType: ArrayList<String>): EffectsFragment {
            val args = Bundle()
            args.putStringArrayList(EffectsFragment().EFFECTS_TYPE, listEffectsType)

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

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

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

    @SuppressLint("UseRequireInsteadOfGet")
    private fun init() {
        val buttonController = ButtonController()

        val listEffects = arguments!!.getStringArrayList(EFFECTS_TYPE)
        val listImage: ArrayList<Drawable> = ArrayList()
        val list: ArrayList<EffectsModel> = ArrayList()

        listImage.add(ContextCompat.getDrawable(requireActivity(), R.drawable.bg_normal)!!)
        listImage.add(ContextCompat.getDrawable(requireActivity(), R.drawable.bg_class)!!)
        listImage.add(ContextCompat.getDrawable(requireActivity(), R.drawable.bg_dance)!!)
        listImage.add(ContextCompat.getDrawable(requireActivity(), R.drawable.ic_flat)!!)
        listImage.add(ContextCompat.getDrawable(requireActivity(), R.drawable.ic_heavy)!!)
        listImage.add(ContextCompat.getDrawable(requireActivity(), R.drawable.ic_folk)!!)
        listImage.add(ContextCompat.getDrawable(requireActivity(), R.drawable.bg_hiphop)!!)
        listImage.add(ContextCompat.getDrawable(requireActivity(), R.drawable.ic_jazz)!!)
        listImage.add(ContextCompat.getDrawable(requireActivity(), R.drawable.ic_pop)!!)
        listImage.add(ContextCompat.getDrawable(requireActivity(), R.drawable.ic_rock)!!)

        for (i in 0 until Math.min(listEffects?.size?:0, listImage.size)) {
            list.add(EffectsModel(listEffects?.get(i)?:"", listImage.get(i)))
        }

        val numberOfColumns = 3
        val layoutManager = GridLayoutManager(requireContext(), numberOfColumns)
        binding.mRcyclerEffects.layoutManager =
            layoutManager
        effectsAdapter = EffectsAdapter(
            requireActivity(),
            list,
            object : EffectsAdapter.OnItemClickListener {
                override fun onItemClick(position: Int) {
//                    tracKingFirebaseAnalytics(mFirebaseAnalytics,"home_list_genres_${list[position].name}_click","User clicks vào ${list[position].name}","list_genres_click")
                    val currentActivity: Activity? = activity
                    currentActivity?.finish()
                    buttonController.onUpdateType(position,list[position].name)
                    EventBus.getDefault().post(MessageEvent(list[position].name))
                }
            })
        binding.mRcyclerEffects.adapter = effectsAdapter

    }
}