package com.zhaohaoting.framework.ui.utils;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.ui.dialog.CustomDialog;

/**
 * author：Created by ZHT on 2017/12/7 16:12
 * email：526309416@qq.com
 * desc：
 */
public class DialogUtils {
    /**
     * 等待框
     */
    public static ProgressDialog waitDilog(Context context, String message, String title) {
        final ProgressDialog dialog = new ProgressDialog(context);
        dialog.setMessage(message);
        dialog.setTitle(title);
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
        return dialog;
    }

    /**
     * 进度
     */
    public static ProgressDialog progressDilog(Context context, String title) {
        final ProgressDialog dialog = new ProgressDialog(context);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(100);
        dialog.setProgress(0);
        dialog.setTitle(title);
        dialog.setIndeterminate(false);
        dialog.setCancelable(false);
        dialog.show();
        return dialog;
    }

    /**
     * 拨打电话 必须权限申请
     */
    @SuppressLint("MissingPermission")
    public static void callPhone(final Context context, final String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber)) return;
        new CustomDialog.Builder(context).setMessage(context.getString(R.string.service_call_tips))
                .setTitle(phoneNumber)
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @SuppressLint("MissingPermission")
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String number = phoneNumber.replace('-', '\0');
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
                        context.startActivity(intent);
                        dialog.dismiss();
                    }
                }).setCancelable(true).create().show();
    }

    public static CustomDialog.Builder showCustomDialog(final Context context, String title, String message, boolean cancelable) {
        CustomDialog.Builder builder = new CustomDialog.Builder(context)
                .setMessage(message)
                .setTitle(title)
                .setCancelable(cancelable);
        return builder;
    }
}
