package com.renrenliao.im.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.renrenliao.im.R;

/**
 * 公用的进度Dialog显示类
 */
public class DialogProgressUtils {

    private Dialog loadDialog;//等待dialog
    private TextView text_show;//等待提示文字
    private View progressView;//等待根布局
    private Context tempContext;//临时的context，用来判断是否是当前的dialog

    private static  DialogProgressUtils dialogProgressUtils;

    public static DialogProgressUtils getInstance(){
        if(dialogProgressUtils == null){
            dialogProgressUtils = new DialogProgressUtils();
        }
        return dialogProgressUtils;
    }

    private DialogProgressUtils() {
    }

    /**
     * 获取进度弹框实例
     */
    public void showProgressDialog(Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return;
        }
        try {
            getProgress(activity);
            if (!loadDialog.isShowing()) {
                loadDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取进度弹框实例
     */
    public void showProgressDialog(Activity activity, String tip) {
        if (activity == null || activity.isFinishing()) {
            return;
        }
        try {

            getProgress(activity);
            if (!TextUtils.isEmpty(tip)) {
                text_show.setText(tip);
            }
            if (!loadDialog.isShowing()) {
                loadDialog.show();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 取消进度弹框
     */
    public void dismissProgressDialog() {

        try {
            if (loadDialog != null && loadDialog.isShowing()) {
                loadDialog.dismiss();
                loadDialog = null;
                text_show = null;
                progressView = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取进度弹框实例
     */
    private void getProgress(Context context) {
        if (progressView == null || this.tempContext != context) {
            progressView = null;
            progressView = LayoutInflater.from(context).inflate(R.layout.dialog_load, null);
            ProgressBar viewById = progressView.findViewById(R.id.progressBar);

            text_show = (TextView) progressView.findViewById(R.id.text_show);
        }
        if (loadDialog == null || this.tempContext != context) {
            loadDialog = null;
            loadDialog = new AlertDialog.Builder(context, R.style.CustomDialogStyle).create();
            loadDialog.setCanceledOnTouchOutside(false);
            loadDialog.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            loadDialog.show();
            loadDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
            loadDialog.setContentView(progressView);
        }
        this.tempContext = context;
    }



}
