package com.test.basd.fastcleanerjunk.activity

import android.content.Intent
import android.net.Uri
import com.test.basd.fastcleanerjunk.databinding.ActivitySettingBinding
import com.test.basd.fastcleanerjunk.display.NotificationService
import com.test.basd.fastcleanerjunk.display.fcm.FcmHelper
import com.test.basd.fastcleanerjunk.display.fcm.NotificationJobService.Companion.initJob
import com.test.basd.fastcleanerjunk.helps.BaseActivity
import com.test.basd.fastcleanerjunk.helps.ConfigHelper.allNotification
import com.test.basd.fastcleanerjunk.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) {
                initJob()
            } 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("https://sites.google.com/view/super-cleaner-max/super-cleaner-max")
            )
            startActivity(intent)
        }
    }

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