package com.ifavine.pay.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Telephony;
import android.widget.Toast;

/**
 * Created by lin on 2017/5/19.
 */

public class ContactUtil {
    public static final int FILE_SELECT_CODE = 0;

    /**发送邮件*/
    public static void sendEmail(Context context,String emailAddress) {

        Intent email = new Intent(android.content.Intent.ACTION_SEND);
//邮件发送类型：无附件，纯文本
        email.setType("plain/text");
//邮件接收者（数组，可以是多位接收者）
        String[] emailReciver = new String[]{emailAddress};

//        String  emailTitle = "标题";
//        String emailContent = "内容";
//设置邮件地址
        email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);
////设置邮件标题
//        email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitle);
////设置发送的内容
//        email.putExtra(android.content.Intent.EXTRA_TEXT, emailContent);
        //调用系统的邮件系统
        context.startActivity(Intent.createChooser(email, "请选择邮箱app发送邮件"));
//        context.startActivity(email);

    }

    /**
     * 发送短信
     */
    @SuppressLint("NewApi")
    public static void sendSMS(Context context,String phoneNum) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //At least KitKat
        {
            String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context); //Need to change the build to API 19

//	        Intent sendIntent = new Intent(Intent.ACTION_SEND);
//	        sendIntent.setType("text/plain");
//	        sendIntent.putExtra(Intent.EXTRA_TEXT, shareContent);
            Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"));
            sendIntent.putExtra("address", phoneNum);
//            sendIntent.putExtra("sms_body", shareContent);

            if (defaultSmsPackageName != null)//Can be null in case that there is no default, then the user would be able to choose any app that support this intent.
            {
                sendIntent.setPackage(defaultSmsPackageName);
            }
            context.startActivity(sendIntent);

        }else{
            Uri smsToUri = Uri.parse("smsto:");
            Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsToUri);
            sendIntent.putExtra("address", phoneNum); // 电话号码，这行去掉的话，默认就没有电话
//            sendIntent.putExtra("sms_body", shareContent);
            sendIntent.setType("vnd.android-dir/mms-sms");
            context.startActivity(sendIntent);
        }
    }

    public static void telCall(Context context,String phoneNum){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + phoneNum));
        //开启系统拨号器
        context.startActivity(intent);
    }

    /** 调用文件选择软件来选择文件 **/
    public static void showFileChooser(Activity context) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            context.startActivityForResult(Intent.createChooser(intent, "请选择一个要上传的文件"),FILE_SELECT_CODE);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(context, "请安装文件管理器", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}
