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

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity

import com.bassbooster.equalizer.databinding.ActivitySettingBinding
import com.bassbooster.soundeffects.equalizer.view.MainService
import com.bassbooster.soundeffects.equalizer.utils.AppUtils
import com.bassbooster.soundeffects.equalizer.view.activity.edgelighting.activity.EdgeLightingActivity
import com.bassbooster.soundeffects.equalizer.view.activity.policy.PolicyActivity
import com.bassbooster.soundeffects.equalizer.view.activity.setting.language.LanguageActivity
import com.bassbooster.soundeffects.equalizer.view.activity.setting.permissions.PermissionsActivity
import com.bassbooster.soundeffects.equalizer.utils.CustomNotificationService
import com.bassbooster.soundeffects.equalizer.utils.SharedPreferencesUtil
import com.bassbooster.soundeffects.equalizer.utils.Utils
import com.bullhead.equalizer.ButtonController
import com.bullhead.equalizer.SharePreferenceUtilEqualizer

@RequiresApi(Build.VERSION_CODES.M)
class SettingActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySettingBinding
    private lateinit var sharedPreferencesUtil: SharedPreferencesUtil
    private lateinit var sharedPreferencesUtil2: SharePreferenceUtilEqualizer
    private val VIBRATE_PERMISSION_REQUEST_CODE = 1
////    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: Setting ","banner_click")
//        }
//
//        override fun onAdImpression() {
//            super.onAdImpression()
//            binding.mLlBanner.setVisibility(View.VISIBLE)
//        }
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySettingBinding.inflate(layoutInflater)
        val view = binding.root
        sharedPreferencesUtil = SharedPreferencesUtil(this)
        sharedPreferencesUtil2 = SharePreferenceUtilEqualizer(this)
        binding.SCVisualizer.isChecked = true
        setContentView(view)
        // Khởi tạo Firebase Analytics
//        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this)
        init()
        setOnEvent()
        loadBanner()
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun init() {
//        tracKingFirebaseAnalytics(mFirebaseAnalytics,"setting_vew","View màn setting","setting_vew")

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

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

    override fun onResume() {
        super.onResume()
        AppUtils.hideNavigationBar(this)
        binding.SCVibrationRights.isChecked = sharedPreferencesUtil.getBoolean("vibrate")
        binding.SCVisualizer.isChecked = sharedPreferencesUtil.getBoolean("visualizer")
    }

    private fun setOnEvent() {
        val buttonController = ButtonController()
//        binding.SCVibrationRights.setOnCheckedChangeListener { buttonView, isChecked ->
//            sharedPreferencesUtil.saveBoolean("vibrate", isChecked)
//            if (isChecked) {
//                if (ContextCompat.checkSelfPermission(
//                        this@SettingActivity,
//                        Manifest.permission.VIBRATE
//                    ) != PackageManager.PERMISSION_GRANTED
//                ) {
//                    ActivityCompat.requestPermissions(
//                        this,
//                        arrayOf(Manifest.permission.VIBRATE),
//                        VIBRATE_PERMISSION_REQUEST_CODE
//                    )
//                    Log.e("PREMISSIONS", "mở quyền rung: ")
//                } else {
//                    Log.e("PREMISSIONS", "quyền rung đã được cấp từ đầu ")
//                    val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
//                    audioManager.ringerMode = AudioManager.RINGER_MODE_VIBRATE
//                }
//            } else {
//                val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
//                audioManager.ringerMode = AudioManager.RINGER_MODE_SILENT
//                Log.e("PREMISSIONS", "bật chế độ im lặng ")
//            }
//        }

        binding.SCVisualizer.setOnCheckedChangeListener { buttonView, isChecked ->
            sharedPreferencesUtil.saveBoolean("visualizer", isChecked)
            val result = if (isChecked) 0 else 1
//            tracKingFirebaseAnalytics(mFirebaseAnalytics,"setting_visualizer_click","${result}","visualizer_click")
        }

        binding.SCControllerEffect.setOnCheckedChangeListener { buttonView, isChecked ->
            sharedPreferencesUtil.saveBoolean("ControllerEffect", isChecked)
            val result = if (isChecked) 0 else 1
//            tracKingFirebaseAnalytics(mFirebaseAnalytics,"setting_eq_controls_all_effect_click","${result}","controls_all_effect")
        }

        binding.imgBack.setOnClickListener {
            finish()
        }

        binding.clLanguage.setOnClickListener {
//            tracKingFirebaseAnalytics(mFirebaseAnalytics,"setting_language_click","setting language click","language_click")
            val intent = Intent(this@SettingActivity, LanguageActivity::class.java)
            startActivity(intent)
        }
        binding.clPermissions.setOnClickListener {
            val intent = Intent(this@SettingActivity, PermissionsActivity::class.java)
            startActivity(intent)
        }

        binding.mEdge.setOnClickListener {
//            tracKingFirebaseAnalytics(mFirebaseAnalytics,"setting_edge_lighting_click","User click vào Edge Lighting trong Setting","edge_lighting")
            val intent = Intent(this@SettingActivity, EdgeLightingActivity::class.java)
            startActivity(intent)
        }

        binding.swPlayControl.apply {
            isChecked = sharedPreferencesUtil.getBooleanPlayControl("swPlayControl")
            setOnCheckedChangeListener { _, isCheck ->
                sharedPreferencesUtil.saveBooleanPlayControl("swPlayControl", isCheck)
                val result = if (isChecked) 0 else 1
//                tracKingFirebaseAnalytics(mFirebaseAnalytics,"setting_play_control_bar_click","${result}","play_control_bar")
            }
        }

        binding.SCControllerEffect.apply {
            isChecked = sharedPreferencesUtil.getBoolean("ControllerEffect")
            setOnCheckedChangeListener { _ , isCheck ->
                sharedPreferencesUtil.saveBoolean("ControllerEffect", isCheck)
                sharedPreferencesUtil2.saveBoolean("ControllerEffect", isCheck)
            }
        }

        binding.mPrivacy.setOnClickListener {
//            tracKingFirebaseAnalytics(mFirebaseAnalytics,"setting_privacy_policy_click","setting privacy policy click","policy_click")

            val intent = Intent(this@SettingActivity, PolicyActivity::class.java)
            startActivity(intent)
        }
    }

    private fun launchMainService(isCheck: Boolean) {
        val svc = Intent(this, MainService::class.java)
        svc.putExtra(CustomNotificationService.EXTRA_BUTTON_TEXT,"Normal")
        stopService(svc)
        if (isCheck){
            startService(svc)
        }
//        finish()
    }

    private fun loadBanner() {
    }

//    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)
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        bannerAdHelper?.unregisterAdListener(adCallback)
//    }
}
