package com.bassbooster.soundeffects.equalizer.view.activity.setting.language

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bassbooster.equalizer.R
import com.bassbooster.soundeffects.equalizer.data.model.Nation
import com.bassbooster.equalizer.databinding.ActivityLanguageBinding
import com.bassbooster.soundeffects.equalizer.utils.SharedPreferencesUtil
import com.bassbooster.soundeffects.equalizer.utils.Utils
import com.bassbooster.soundeffects.equalizer.view.ActivityTutorialSplash
import com.bassbooster.soundeffects.equalizer.view.MainActivity
import com.bassbooster.soundeffects.equalizer.view.MainActivity.Companion.RESTART_APP_FLAG
import com.bassbooster.soundeffects.equalizer.utils.AppUtils
import com.bassbooster.soundeffects.equalizer.view.activity.setting.language.adapter.LanguageAdapter
import kotlin.system.exitProcess


class LanguageActivity : AppCompatActivity() {
    private lateinit var myAdapter: LanguageAdapter
    private var nations: ArrayList<Nation> = ArrayList<Nation>()
    val languageManager = LanguageManager()
    private lateinit var binding: ActivityLanguageBinding
    private lateinit var sharedPreferencesUtil: SharedPreferencesUtil
//    private var mFirebaseAnalytics: FirebaseAnalytics? = null
//    private var nativeAdHelper: NativeAdHelper? = null

//    val adCallback = object : AperoAdCallback() {
//        override fun onAdClicked() {
//            super.onAdClicked()
//            tracKingFirebaseAnalytics(mFirebaseAnalytics,"Native_language_click","click ad tại màn Language","language_click")
//        }
//
//        override fun onAdImpression() {
//            super.onAdImpression()
//        }
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLanguageBinding.inflate(layoutInflater)
        val view = binding.root
        sharedPreferencesUtil = SharedPreferencesUtil(this)
        // Khởi tạo Firebase Analytics
//        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

        // Gửi sự kiện "Chọn ngôn ngữ English" lên Firebase Analytics
        val params = Bundle()
        params.putString("language", "User views Language")
//        mFirebaseAnalytics!!.logEvent("language_view", params)

        if (!sharedPreferencesUtil.getIsFirstSplash("isFirstSplash")) {
            checkFirstTime()
            setRecyclerFirstApp()
        } else {
            checkLanguage()
            setRecyclerView()
            setOnEvent()
        }
        setContentView(view)
        AppUtils.hideNavigationBar(this)

        loadNativeAds()
    }

    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()
    }

    fun isFirst(context: Context): Boolean {
        val sharedPreferences: SharedPreferences =
            context.getSharedPreferences("MyPre", Context.MODE_PRIVATE)
        val isFirstLogin = sharedPreferences.getBoolean("isFirst", true)

        if (isFirstLogin) {
            val editor: SharedPreferences.Editor = sharedPreferences.edit()
            editor.putBoolean("isFirst", false)
            editor.apply()
        }

        return isFirstLogin
    }

    private fun checkFirstTime() {
        binding.imgBack.visibility = View.GONE
        binding.tvTitleLanguage.visibility = View.GONE
        binding.tvTitleLanguageFirst.visibility = View.VISIBLE
        binding.tvLine.visibility = View.VISIBLE
        binding.icCheckFirst.visibility = View.VISIBLE
    }

    private fun checkLanguage() {
        binding.imgBack.visibility = View.VISIBLE
        binding.tvTitleLanguage.visibility = View.VISIBLE
        binding.tvTitleLanguageFirst.visibility = View.GONE
        binding.tvLine.visibility = View.GONE
        binding.icCheckFirst.visibility = View.GONE
    }

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

    private fun setRecyclerView() {
        var index = sharedPreferencesUtil.getInt("isSeletedIndex")
        languageManager.languageManager(this@LanguageActivity)
        nations.add(Nation(R.color.color_neutral, R.drawable.ic_english, "English"))
        nations.add(Nation(R.color.color_neutral, R.drawable.img_hindi, "Hindi"))
        nations.add(Nation(R.color.color_neutral, R.drawable.img_france, "France"))
        nations.add(Nation(R.color.color_neutral, R.drawable.img_spanish, "Spanish"))
        nations.add(Nation(R.color.color_neutral, R.drawable.ic_flag, "Portuguese"))
        nations.add(Nation(R.color.color_neutral, R.drawable.ic_dutch, "Russian"))
        binding.rvLanguage.layoutManager =
            LinearLayoutManager(this@LanguageActivity, RecyclerView.VERTICAL, false)
        myAdapter = LanguageAdapter(
            nations, object : LanguageAdapter.OnItemClickListener {
                override fun onItemClick(position: Int) {
                    when (position) {
                        0 -> {
                            newSplash("en","English")
                        }

                        1 -> {
                            newSplash("hi","Hindi")
                        }

                        2 -> {
                            newSplash("fr","France")
                        }

                        3 -> {
                            newSplash("es","Spanish")
                        }

                        4 -> {
                            newSplash("de","Portuguese")
                        }

                        5 -> {
                            newSplash("zh","Russian")
                        }
                    }
                    sharedPreferencesUtil.saveInt("isSeletedIndex", position)
                    myAdapter?.notifyDataSetChanged()

                }
            }, index, this@LanguageActivity
        )
        binding.rvLanguage.adapter = myAdapter
    }

    private fun setRecyclerFirstApp() {
        val index = sharedPreferencesUtil.getInt("isSeletedIndex")
        languageManager.languageManager(this@LanguageActivity)
        nations.add(Nation(R.color.color_neutral, R.drawable.ic_english, "English"))
        nations.add(Nation(R.color.color_neutral, R.drawable.img_hindi, "Hindi"))
        nations.add(Nation(R.color.color_neutral, R.drawable.img_france, "France"))
        nations.add(Nation(R.color.color_neutral, R.drawable.img_spanish, "Spanish"))
        nations.add(Nation(R.color.color_neutral, R.drawable.ic_flag, "Portuguese"))
        nations.add(Nation(R.color.color_neutral, R.drawable.ic_dutch, "Russian"))
        binding.rvLanguage.layoutManager =
            LinearLayoutManager(this@LanguageActivity, RecyclerView.VERTICAL, false)
        myAdapter = LanguageAdapter(
            nations, object : LanguageAdapter.OnItemClickListener {
                override fun onItemClick(position: Int) {
                    when (position) {
                        0 -> {
                        }

                        1 -> {
                        }

                        2 -> {
                        }

                        3 -> {
                        }

                        4 -> {
                        }

                        5 -> {
                        }

                    }
                    sharedPreferencesUtil.saveInt("isSeletedIndex", position)
                    myAdapter?.notifyDataSetChanged()

                }
            }, index, this@LanguageActivity
        )


        binding.rvLanguage.adapter = myAdapter
        binding.icCheckFirst.setOnClickListener {
            when (sharedPreferencesUtil.getInt("isSeletedIndex")) {
                0 -> {
                    newTutorial("en","English")
                }

                1 -> {
                    newTutorial("hi","Hindi")
                }

                2 -> {
                    newTutorial("fr","France")
                }

                3 -> {
                    newTutorial("es","Spanish")
                }

                4 -> {
                    newTutorial("de","Portuguese")
                }

                5 -> {
                    newTutorial("zh","Russian")
                }
            }

        }
    }

    fun newSplash(language: String,nameLang: String) {
        // Gửi sự kiện "Chọn ngôn ngữ English" lên Firebase Analytics
        val params = Bundle()
        params.putString("language", "Chọn ngôn ngữ $nameLang")
//        mFirebaseAnalytics!!.logEvent("language_choose_language_click", params)

        sharedPreferencesUtil.saveString("lang", language)
        languageManager.updateResource(language)
        sharedPreferencesUtil.saveStatusRestart(RESTART_APP_FLAG,true)
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    fun newTutorial(language: String,nameLang: String) {
        // Gửi sự kiện "Chọn ngôn ngữ English" lên Firebase Analytics
//        tracKingFirebaseAnalytics(mFirebaseAnalytics,"language","User clicks vào 'v' button $nameLang","language_v_click")


        sharedPreferencesUtil.saveString("lang", language)
        languageManager.updateResource(language)
        val intent = Intent(this@LanguageActivity, ActivityTutorialSplash::class.java)
//        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        startActivity(intent)
        finish()
    }

    private fun loadNativeAds() {
    }

    override fun onBackPressed() {
        if (!sharedPreferencesUtil.getIsFirstSplash("isFirstSplash")) {
            exitProcess(0)
        }else{
            finish()
        }
    }

//    private fun initNativeAd(): NativeAdHelper {
//        val config = NativeAdConfig(
//            BuildConfig.Native_language,
//            true,
//            FirebaseRemoteConfig.getInstance()
//                .getBoolean(Constants.allow_reload_native_language) ,
//            // with native additional layout native ad
//            R.layout.layout_language_native
//        )
//        return NativeAdHelper(this,  this, config)
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        nativeAdHelper?.unregisterAdListener(adCallback)
//    }
}
