package com.base.easyfilemanager.activity

import android.content.Intent
import android.net.Uri
import android.os.Build
import com.base.easyfilemanager.databinding.ActivitySettingBinding
import com.base.easyfilemanager.display.NotificationService
import com.base.easyfilemanager.display.fcm.FcmHelper
import com.base.easyfilemanager.helps.BaseActivity
import com.base.easyfilemanager.helps.ConfigHelper
import com.base.easyfilemanager.helps.ConfigHelper.allNotification
import com.base.easyfilemanager.helps.ConfigHelper.remainNotification


class SettingActivity : BaseActivity<ActivitySettingBinding>() {

    override val binding: ActivitySettingBinding by lazy {
        ActivitySettingBinding.inflate(layoutInflater)
    }

    override fun initView() {
        binding.switchRemainNotification.isChecked = remainNotification
        binding.switchAllNotification.isChecked = allNotification
    }

    override fun initListener() {
        binding.ivBack.setOnClickListener {
//            AdmobUtils.showInterstitialAd(this) {
            finishToMain()
//            }
        }
        binding.switchRemainNotification.setOnCheckedChangeListener { buttonView, isChecked ->
            remainNotification = isChecked
            if (isChecked) {
                startNotification()
            } else {
                val serviceIntent = Intent(this, NotificationService::class.java)
                stopService(serviceIntent)
            }
        }
        binding.switchAllNotification.setOnCheckedChangeListener { buttonView, isChecked ->
            allNotification = isChecked
            switchFcm(isChecked)
        }
        binding.cardPrivacy.setOnClickListener {
            val intent = Intent(
                Intent.ACTION_VIEW,
                Uri.parse(ConfigHelper.privacyPolicy)
            )
            startActivity(intent)
        }
    }
    private fun startNotification() {
        val intent = Intent(this, NotificationService::class.java)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intent)
        } else {
            startService(intent)
        }
    }
    private fun switchFcm(checked: Boolean) {
        if (checked) {
            FcmHelper.subscribeToTopic()
        } else {
            FcmHelper.unSubscribeToTopic()
        }
    }
}