package com.ama.core.architecture.util

import android.app.Activity
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import com.ama.core.architecture.BaseApp
import kotlin.random.Random


class AndroidUtil private constructor() {

    companion object {


        fun genRandomInt(rangeBegin: Int, rangeEnd: Int): Int {
            return Random.nextInt(rangeBegin, rangeEnd)
        }


        fun showToast(strRes: Int) {
            Toast.makeText(BaseApp.appContext(), strRes, Toast.LENGTH_SHORT).show()
        }

        fun showToast(str: String) {
            Toast.makeText(BaseApp.appContext(), str, Toast.LENGTH_SHORT).show()
        }

        fun openUrl(url: String) {
            val appContext = BaseApp.appContext()
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
            intent.flags = FLAG_ACTIVITY_NEW_TASK
            appContext.startActivity(intent)
        }

        fun openAppNotifySettings() {
            val context = BaseApp.appContext()
            val intent = Intent()
            intent.flags = FLAG_ACTIVITY_NEW_TASK
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Android >=8
                    intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                    intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
                    intent.putExtra(Settings.EXTRA_CHANNEL_ID, context.applicationInfo.uid)

                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Android 5.0-7.1
                    intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
                    intent.putExtra("app_package", context.packageName)
                    intent.putExtra("app_uid", context.applicationInfo.uid)
                }

                context.startActivity(intent)
            } catch (e: Exception) {
                e.printStackTrace()
                val fallbackIntent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                fallbackIntent.data = Uri.fromParts("package", context.packageName, null)
                context.startActivity(fallbackIntent)
            }
        }



    }




}