package com.test.basd.supercleanermax.activity

import android.content.Intent
import com.test.basd.supercleanermax.databinding.ActivitySettingBinding
import com.test.basd.supercleanermax.display.NotificationService
import com.test.basd.supercleanermax.display.fcm.FcmHelper
import com.test.basd.supercleanermax.display.fcm.NotificationJobService.Companion.initJob
import com.test.basd.supercleanermax.helps.BaseActivity
import com.test.basd.supercleanermax.helps.ConfigHelper.allNotification
import com.test.basd.supercleanermax.helps.ConfigHelper.remainNotification
import com.test.basd.supercleanermax.helps.ads.AdmobUtils


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) {
                initJob()
            } else {
                val serviceIntent = Intent(this, NotificationService::class.java)
                stopService(serviceIntent)
            }
        }
        binding.switchAllNotification.setOnCheckedChangeListener { buttonView, isChecked ->
            allNotification = isChecked
            switchFcm(isChecked)
        }
    }

    private fun switchFcm(checked: Boolean) {
        if (checked) {
            FcmHelper.subscribeToTopic()
        } else {
            FcmHelper.unSubscribeToTopic()
        }
    }
}