package com.za.ext

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.core.app.ActivityCompat
import com.blankj.utilcode.util.ToastUtils


fun ComponentActivity.navigationActivity(destActivity: Class<*>) {
    ActivityCompat.startActivity(this, Intent(this, destActivity), null)
}

fun ComponentActivity.navigationActivity(destActivity: Class<*>, requestCode: Int) {
    ActivityCompat.startActivityForResult(this, Intent(this, destActivity), requestCode, null)
}

fun ComponentActivity.navigationActivity(intent: Intent, requestCode: Int) {
    ActivityCompat.startActivityForResult(this, intent, requestCode, null)
}

fun ComponentActivity.navigationActivity(destActivity: Class<*>, isFinish: Boolean? = false) {
    ActivityCompat.startActivity(this, Intent(this, destActivity), null)
    if (isFinish == true) {
        this.finish()
    }
}

fun ComponentActivity.navigationActivity(destActivity: Class<*>, key: String, value: String?) {
    val intent = Intent(this, destActivity)
    intent.putExtra(key, value)
    ActivityCompat.startActivity(this, intent, null)
}

fun ComponentActivity.callPhone(phone: String?) {
    if (phone.isNullOrBlank()) {
        ToastUtils.showShort("号码错误！")
//        LogUtil.print("call phone","号码错误=$phone")
        return
    }

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_DENIED) {
        ToastUtils.showShort("请先允许通话权限！")
//        LogUtil.print("call phone","拨打电话权限未允许")
        return
    }

    val intentPhone = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phone"))
    ActivityCompat.startActivity(this, intentPhone, null)
}

