package com.adealink.weparty.im.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.im.databinding.LayoutSessionOpenNotificationBinding
import com.adealink.weparty.push.NotificationUtil

class SessionPermissionComp(
    lifecycleOwner: LifecycleOwner,
    val notificationBinding: LayoutSessionOpenNotificationBinding
) : ViewComponent(lifecycleOwner) {

    override fun onCreate() {
        super.onCreate()
        initView()
    }

    private fun initView() {
        notificationBinding.ivClose.onClick {
            ignoreNotificationPermission()
        }
        notificationBinding.btnTurnOn.onClick {
            requestNotificationPermission()
        }
    }

    override fun onResume() {
        super.onResume()
        checkPermission()
    }

    private fun checkPermission() {
        checkNotification()
    }

    /**
     * 检查通知权限是否打开
     */
    private fun checkNotification() {
        val activity = activity ?: return
        if (NotificationUtil.isNotificationPermissionGranted(activity)) {
            notificationBinding.root.gone()
            return
        }
        notificationBinding.root.show()
    }

    private fun ignoreNotificationPermission() {
        notificationBinding.root.gone()
    }

    private fun requestNotificationPermission() {
        val activity = activity ?: return
        NotificationUtil.requestPostNotificationPermission(activity)
    }

}