package com.base.datarecovery.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import androidx.activity.addCallback
import com.base.datarecovery.bean.ConstObject.fcmNotification
import com.base.datarecovery.bean.ConstObject.stayNotification
import com.base.datarecovery.databinding.ActivitySettingBinding
import com.base.datarecovery.fcm.FCMManager.subscribeToTopic
import com.base.datarecovery.fcm.FCMManager.unsubscribeFromTopic
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.ConfigHelper
import com.base.datarecovery.service.StayJobService
import com.base.datarecovery.service.StayJobService.Companion.startJob
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.view.RateStarPop.showRateStarPopDialog

class SettingActivity : BaseActivity<ActivitySettingBinding>() {

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

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)

        binding.switchStayNotification.isChecked = stayNotification
        binding.switchFcmNotification.isChecked = fcmNotification
    }

    override fun initListener() {
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

        binding.switchStayNotification.setOnCheckedChangeListener { buttonView, isChecked ->
            stayNotification = isChecked
            if (isChecked) {
//                startStayNotification()
                startJob()
            } else {
//                val serviceIntent = Intent(this, StayNotificationService::class.java)
//                stopService(serviceIntent)
                StayJobService.isCancel = true
            }
        }
        binding.switchFcmNotification.setOnCheckedChangeListener { buttonView, isChecked ->
            fcmNotification = isChecked
            val topic = "${ConfigHelper.packageName}_push"
            if (isChecked) {
                subscribeToTopic(topic)
            } else {
                unsubscribeFromTopic(topic)
            }
        }

        binding.llPrivacy.setOnClickListener {
            val intent = Intent(
                Intent.ACTION_VIEW,
                Uri.parse(ConfigHelper.privacyPolicy)
            )
            startActivity(intent)
          //  NotificationUtil.sendNotification(this, actionId = ID_WEATHER,"weather")
        }

        binding.llScore.setOnClickListener {
            showRateStarPopDialog(mustShow = true)
           // NotificationUtil.sendNotification(this, actionId = ID_XINGZUO,"xingzuo")
        }
//        binding.idToken.text= AppPreferences.getInstance().getString("token","")
//        binding.idCopy.setOnClickListener {
//            copyText(this,binding.idToken.text.toString())
//        }
    }
    fun copyText(context: Context, text: String?) {
        val clipboardManager: ClipboardManager =
            context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val clip = ClipData.newPlainText("simple text", text)
        clipboardManager.setPrimaryClip(clip)
    }
}