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

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.bassbooster.equalizer.R
import com.bassbooster.soundeffects.equalizer.adapter.ViewPagerAdapter
import com.bassbooster.equalizer.databinding.ActivityEffectsBinding
import com.bassbooster.soundeffects.equalizer.utils.Utils
import com.bassbooster.soundeffects.equalizer.view.activity.effects.fragment.EffectsFragment
import com.bassbooster.soundeffects.equalizer.view.activity.effects.fragment.MyEffectsFragment
import com.bassbooster.soundeffects.equalizer.viewmodel.MyViewModel
import com.bassbooster.soundeffects.equalizer.utils.AppUtils

class EffectsActivity : AppCompatActivity() {
    private lateinit var binding: ActivityEffectsBinding
    private lateinit var myEffectsFragment: MyEffectsFragment
    private lateinit var viewModel: MyViewModel
//    private var mFirebaseAnalytics: FirebaseAnalytics? = null
//    private var bannerAdHelper: BannerAdHelper? = null

//    val adCallback = object : AdCallback() {
//        override fun onAdClicked() {
//            super.onAdClicked()
//            tracKingFirebaseAnalytics(mFirebaseAnalytics,"Banner_click","click ad tại các màn hình:List Effects ","banner_click")
//        }
//
//        override fun onAdImpression() {
//            super.onAdImpression()
//        }
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityEffectsBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        // Khởi tạo Firebase Analytics
//        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this)
        AppUtils.hideNavigationBar(this)
        init()
        setOnEvent()
//        loadBanner()
//        tracKingFirebaseAnalytics(mFirebaseAnalytics,"home_list_genres_view","View màn List Effect","list_genres")
    }

    override fun onResume() {
        super.onResume()
        AppUtils.hideNavigationBar(this)
    }

    @SuppressLint("NewApi")
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            AppUtils.hideNavigationBar(this)
        }
    }

    override fun onPause() {
        super.onPause()
        Utils.hideAdsResume()
    }

    private fun init() {
        viewModel = ViewModelProvider(this).get(MyViewModel::class.java)

        val intent: Intent = intent
        val listEff = intent.getStringArrayListExtra("listType")
        myEffectsFragment = MyEffectsFragment.newInstance()

        Log.d("qqq", "init: " + listEff?.size)
        val fragmentList = arrayListOf(
            EffectsFragment.newInstance(listEff ?: arrayListOf()),
            myEffectsFragment
        )
        binding.viewPagerEffects.adapter = ViewPagerAdapter(this, fragmentList)
        binding.viewPagerEffects.isUserInputEnabled = false
        changeTab(0)
        binding.cardTab1.setOnClickListener {
            binding.viewPagerEffects.apply {
                if (currentItem != 0) {
//                    if (!FirebaseRemoteConfig.getInstance()
//                            .getBoolean(Constants.Banner) || !AppUtils.haveNetworkConnection(this@EffectsActivity)
//                    ) {
//
//                    }else{
//                        binding.mLlBanner.visibility = View.VISIBLE
//                    }
                    binding.mLlBanner.visibility = View.GONE
                    changeTab(0)
                    this.setCurrentItem(0, true)
                }
            }
        }

        binding.cardTab2.setOnClickListener {
            binding.viewPagerEffects.apply {
                if (currentItem != 1) {
                    viewModel.allData.observe(this@EffectsActivity, Observer { myData ->
                        if (myData.isEmpty()){
                            binding.mLlBanner.visibility = View.GONE
                        }
                    })
                    changeTab(1)
                    this.setCurrentItem(1, true)
                }
            }
        }

    }

    @SuppressLint("ResourceAsColor")
    fun changeTab(position: Int){
        binding.apply {
            if (position == 0) {
                tab1Background.setBackgroundResource(R.color.tab_selected)
                tab2Background.setBackgroundResource(R.color.transparent_00)
            } else {
                tab2Background.setBackgroundResource(R.color.tab_selected)
                tab1Background.setBackgroundResource(R.color.transparent_00)
            }
        }
    }

    private fun setOnEvent() {
        binding.mImgBack.setOnClickListener {
            finish()
        }
    }

//    private fun initBannerAd(): BannerAdHelper {
//        val config = BannerAdConfig(
//            BuildConfig.Banner,
//            FirebaseRemoteConfig.getInstance()
//                .getBoolean(Constants.Banner),
//            FirebaseRemoteConfig.getInstance()
//                .getBoolean(Constants.allow_reload_banner),
//            // with native additional layout native ad
//        )
//        return BannerAdHelper(this,  this, config)
//    }
//
//    private fun loadBanner() {
//        if (!FirebaseRemoteConfig.getInstance()
//                .getBoolean(Constants.Banner) && !AppUtils.haveNetworkConnection(this)) {
//            binding.mLlBanner.visibility = View.GONE
//        } else {
//            tracKingFirebaseAnalytics(mFirebaseAnalytics,"Banner_view","view tại các màn hình: effects","banner_view")
//            bannerAdHelper = initBannerAd()
//            bannerAdHelper?.setBannerContentView(binding.mLlBanner)
//            bannerAdHelper?.registerAdListener(adCallback)
//            if (bannerAdHelper?.bannerAdView != null) {
//                bannerAdHelper?.requestAds(BannerAdParam.Ready(bannerAdHelper?.bannerAdView!!))
//            }else{
//                bannerAdHelper?.requestAds(BannerAdParam.Request.create())
//            }
//            binding.mLlBanner.visibility = View.VISIBLE
//        }
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        bannerAdHelper?.unregisterAdListener(adCallback)
//    }

}