/**
 * com.leadingsoft.leaderaide.activity
 *
 * @ClassName: LoadingDialog
 * @Description: LoadingDialog等待页面
 * @author: macq    macq@leadingsoft.cn
 * @date: 2014-06-16
 */
package com.xuqm.base.dialog.loading;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Handler;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xuqm.base.R;
import com.xuqm.base.common.AppManager;
import com.xuqm.base.common.LogHelper;
import com.xuqm.base.common.ToolsHelper;


public class LoadingDialog {
    private static Dialog mDialog;

    public static void showDialog(String msg) {
        showDialog(AppManager.getInstance().getActivity(), msg);
    }

    public static void showDialog(Context context, String msg) {
        if (isShowing())
            return;
        try {

            OnKeyListener keyListener = (dialog, keyCode, event) -> {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    mDialog.dismiss();
                }
                return false;

            };
            mDialog = new Dialog(context, R.style.dialog);
            mDialog.setOnKeyListener(keyListener);
            mDialog.setCancelable(false);
            mDialog.show();
            mDialog.setContentView(R.layout.loading_process_dialog_icon);
            TextView TvLoading = (TextView) mDialog
                    .findViewById(R.id.loading_process_dialog_text);
            TvLoading.setText(msg);
            if (ToolsHelper.isNull(msg)) {
                TvLoading.setVisibility(View.GONE);
            }
            Window window = mDialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.CENTER;
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;//宽高可设置具体大小
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            mDialog.getWindow().setAttributes(lp);
        } catch (Exception e) {
            LogHelper.e(LoadingDialog.class.getSimpleName(), e);
        }
    }

    public static void showDialog(Context context, String msg, boolean isShowBackGround) {
        OnKeyListener keyListener = new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    mDialog.dismiss();
                }
                return false;

            }
        };
        mDialog = new Dialog(context, R.style.dialog);
        mDialog.setOnKeyListener(keyListener);
        mDialog.setCancelable(false);
        mDialog.show();
        mDialog.setContentView(R.layout.loading_process_dialog_icon);
        LinearLayout ll_loading_process_dialog = (LinearLayout) mDialog.findViewById(R.id.ll_loading_process_dialog);
        // 清空背景
        if (!isShowBackGround) {
            ll_loading_process_dialog.setBackgroundResource(0);
        }
        TextView TvLoading = (TextView) mDialog.findViewById(R.id.loading_process_dialog_text);
        TvLoading.setText(msg);
        if (ToolsHelper.isNull(msg)) {
            TvLoading.setVisibility(View.GONE);
        }
        Window window = mDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;//宽高可设置具体大小
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mDialog.getWindow().setAttributes(lp);
    }

    public static void updataTV(String text) {
        if (null == mDialog)
            return;
        TextView TvLoading = (TextView) mDialog.findViewById(R.id.loading_process_dialog_text);
        TvLoading.setText(text);
    }

    public static void dismissDialog() {
        if (mDialog != null) {
            if (mDialog.isShowing()) {
                try {
                    mDialog.dismiss();
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
    }

    public static boolean isShowing() {
        if (mDialog != null) {
            return mDialog.isShowing();
        }
        return false;
    }


    /**
     * 获取进度条的Dialog
     */
    public static Dialog getCircleProgressDialog(Context context) {
        return new Dialog(context, R.style.dialog);
    }

    /**
     * 获取进度条的VIEW
     */
    public static View getCircleProgressView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        return inflater.inflate(R.layout.loading_circle_process_dialog_icon, null);
    }

    /**
     * 显示带进度条的dialog
     */
    public static void showCircleProgressDialog(Context context, Dialog dialog, View view) {
        OnKeyListener keyListener = new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    dialog.dismiss();
                }
                return false;

            }
        };
        try {
            //dialog = new AlertDialog.Builder(context).create();
            dialog.setOnKeyListener(keyListener);
            dialog.setCancelable(false);
            dialog.show();
            dialog.setContentView(view);
        } catch (Exception ex) {
            LogHelper.e("创建圆形进度条", ex);
        }
    }

    /**
     * 显示带进度条的dialog
     */
    public static void updateCircleProgressDialog(int percent) {
        if (null== mView)return;
        TextView tvPercent = (TextView) mView.findViewById(R.id.tvPercent);
        ColorfulRingProgressView isDownload = (ColorfulRingProgressView) mView.findViewById(R.id.crpv);
        isDownload.setPercent(percent);
        tvPercent.setText("" + percent);
    }

    /********************************* APK下载圆形进度条 ***********************************************/

    /**
     * 获取进度条的VIEW
     */
    public static View getProgressViewByTextView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.loading_circle_process_dialog_icon_by_textview, null);
        return view;
    }

    private static View mView;
    /**
     * 显示带进度条的dialog
     */
    public static void showCircleProgressDialog(final Context context, final Handler handler, String msg) {

        mDialog=getCircleProgressDialog(context);
        mView=getProgressViewByTextView(context);

        OnKeyListener keyListener = (dialog, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK
                    && event.getAction() == KeyEvent.ACTION_DOWN) {
                handler.obtainMessage(9999).sendToTarget();
            }
            return false;
        };
        try {
            //dialog = new AlertDialog.Builder(context).create();
            mDialog.setOnKeyListener(keyListener);
            mDialog.setCancelable(false);
            mDialog.show();

            TextView textView = (TextView) mView.findViewById(R.id.tip_msg);
            textView.setText(msg);
            TextView tvPercent = (TextView) mView.findViewById(R.id.tvPercent);
            tvPercent.setText("0");
            mDialog.setContentView(mView);
            Window window = mDialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.CENTER;
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;//宽高可设置具体大小
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            mDialog.getWindow().setAttributes(lp);
        } catch (Exception ex) {
            LogHelper.e("创建圆形进度条", ex);
        }
    }
}
