package com.gh.vspace.shortcut

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.view.Window
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.common.entity.SuggestType
import com.gh.gamecenter.databinding.DialogGameAddShortcutPermissionBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.muugi.shortcut.core.Executor

/**
 * @author : liujiarui
 * date : 2023/3/20
 * description : 创建快捷方式权限弹窗
 */
class ShortcutPermissionDialog(
    context: Context,
    private val mGameEntity: GameEntity,
    private val mExecutor: Executor? = null
) : Dialog(context, com.gh.gamecenter.common.R.style.DialogWindowTransparent) {
    private lateinit var mBinding: DialogGameAddShortcutPermissionBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState)
        DialogGameAddShortcutPermissionBinding.inflate(layoutInflater).apply {
            setContentView(root)
            mBinding = this
        }
        initEvent()
    }

    private fun initEvent() {
        mBinding.closeContainer.setOnClickListener {
            dismiss()
        }
        mBinding.goSettingTv.setOnClickListener {
            jumpToSetting()
            dismiss()
        }
        mBinding.serviceTv.setOnClickListener {
            jumpToFeedback()
            dismiss()
        }
    }

    private fun jumpToSetting() {
        NewFlatLogUtils.logHaloFunShortcutPermissionDialogButtonClick(
            mGameEntity.id,
            mGameEntity.name ?: "",
            mBinding.goSettingTv.text.toString()
        )
        if (mExecutor != null) {
            mExecutor.executeSetting()
            return
        }
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.parse("package:" + context.packageName)
        context.startActivity(intent)
    }

    private fun jumpToFeedback() {
        NewFlatLogUtils.logHaloFunShortcutPermissionDialogButtonClick(
            mGameEntity.id,
            mGameEntity.name ?: "",
            mBinding.serviceTv.text.toString()
        )
        DirectUtils.directToSuggestion(context, SuggestType.APP, content = "无法正常设置桌面快捷方式权限")
    }
}