package com.base.filerecoveryrecyclebin.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 android.widget.Toast
import androidx.activity.addCallback
import com.base.filerecoveryrecyclebin.BuildConfig
import com.base.filerecoveryrecyclebin.GlobalConfig
import com.base.filerecoveryrecyclebin.bean.ConstObject.fcmNotification
import com.base.filerecoveryrecyclebin.bean.ConstObject.stayNotification
import com.base.filerecoveryrecyclebin.databinding.ActivitySettingBinding
import com.base.filerecoveryrecyclebin.fcm.FCMManager.subscribeToTopic
import com.base.filerecoveryrecyclebin.fcm.FCMManager.unsubscribeFromTopic
import com.base.filerecoveryrecyclebin.fcm.NotificationUtil
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.service.StayJobService
import com.base.filerecoveryrecyclebin.service.StayJobService.Companion.startJob
import com.base.filerecoveryrecyclebin.utils.AppPreferences
import com.base.filerecoveryrecyclebin.utils.BarUtils
import com.base.filerecoveryrecyclebin.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 {
            // copyText(this@SettingActivity, AppPreferences.getInstance().getString("token",""))
            finishToMain()
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }


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

        binding.llPrivacy.setOnClickListener {
            val intent = Intent(
                Intent.ACTION_VIEW,
                Uri.parse(GlobalConfig.URL_PRIVACY)
            )
            startActivity(intent)
            // NotificationUtil.sendNotification(this, ConstObject.ID_WHEATHER)
        }

        binding.llScore.setOnClickListener {
            if (BuildConfig.DEBUG) {
                NotificationUtil.sendNotification(this, "test")
            } else {
                showRateStarPopDialog(mustShow = true)
            }
        }
        if (BuildConfig.DEBUG) {
            val token = AppPreferences.getInstance().getString("token", "null")
            binding.tvToken.text = token

            binding.tvToken.setOnClickListener {
                copyText(this, token)
            }
        }


    }

    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)
        Toast.makeText(context, "copy token", Toast.LENGTH_SHORT).show()
    }
}