package com.za.ext

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.za.call.CallLogManager
import com.za.call.ContactRecordBean
import com.za.common.GlobalData
import com.za.common.log.LogUtil

fun Context.navigationActivity(destActivity : Class<*>) {
	if (this !is Activity) {
		return
	}
	ActivityCompat.startActivity(this, Intent(this, destActivity), null)
}

fun Context.navigationActivity(destActivity : Class<*>, requestCode : Int) {
	if (this !is Activity) {
		return
	}
	ActivityCompat.startActivityForResult(this, Intent(this, destActivity), requestCode, null)
}

fun Context.navigationActivity(intent : Intent, requestCode : Int) {
	if (this !is Activity) {
		return
	}
	ActivityCompat.startActivityForResult(this, intent, requestCode, null)
}

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

fun Context.navigationActivity(destActivity : Class<*>, key : String, value : String?) {
	if (this !is Activity) {
		return
	}
	val intent = Intent(this, destActivity)
	intent.putExtra(key, value)
	ActivityCompat.startActivity(this, intent, null)
}

fun Context.finish() {
	if (this !is Activity) {
		return
	}
	this.finish()
}


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

fun Context.callPhone(phone : String?) {
	if (this !is Activity) {
		return
	}
	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
	}

	//如果是联系客户，则记录通话记录
	if (GlobalData.currentOrder != null && GlobalData.currentOrder?.customerPhone == phone) {
		CallLogManager.phoneCallContactBean =
			ContactRecordBean(taskId = GlobalData.currentOrder?.taskId,
				taskCode = GlobalData.currentOrder?.taskCode,
				phone = phone)
	}
	val intentPhone = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phone"))
	ActivityCompat.startActivity(this, intentPhone, null)
}