package com.mytatvarnsdk.myView.progress;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Looper;

import com.mytatvarnsdk.R;

/***
 * 进度框管理工具
 * 程序只能显示一个进度弹框
 * @author minyuchun
 */
public class ProgressManagement {

    /**
     * 显示的进度弹框
     */
    private BaseCustomProgress mProgress;

    private static class SingletonInstance {
        private static final ProgressManagement INSTANCE = new ProgressManagement();
    }

    public static ProgressManagement getInstance() {
        return SingletonInstance.INSTANCE;
    }

    private synchronized void dismiss() {
        if (mProgress != null) {
            if (mProgress.isShowing()) {
                mProgress.dismiss();
            }
            mProgress = null;
        }
    }

    public synchronized void showWait(Activity activity, @ProgressType int type, DialogInterface.OnCancelListener listener) {
        try {
            if (Looper.getMainLooper() == Looper.myLooper()) {
                if (activity == null || activity.isFinishing()) {
                    return;
                }
                dismiss();
                mProgress = new WaitProgress(activity);
                String message = null;
                switch (type) {
                    case ProgressType.PROGRESSDIALOG_SCAN:
                        message = activity.getResources().getString(R.string.bt_searching);
                        break;
                    case ProgressType.PROGRESSDIALOG_CONNECT:
                        message = activity.getResources().getString(R.string.bt_connecting);
                        break;
                    case ProgressType.PROGRESSDIALOG_BLUETOOTH_ON:
                        message = activity.getResources().getString(R.string.bt_ble_action_open);
                        break;
                    case ProgressType.PROGRESSDIALOG_BLUETOOTH_OFF:
                        message = activity.getResources().getString(R.string.bt_ble_action_close);
                        break;
                    case ProgressType.PROGRESSDIALOG_UNBIND:
                        message = activity.getResources().getString(R.string.progressdialog_unBind);
                        break;
                    case ProgressType.PROGRESSDIALOG_RECEIVE:
                        message = activity.getResources().getString(R.string.sync);
                        break;
                    case ProgressType.PROGRESSDIALOG_EXPORT:
                        message = activity.getResources().getString(R.string.history_export_executing);
                        break;
                    default:
                        break;
                }
                mProgress.setMessage(message)
                        .setType(type)
                        .setCancelListener(listener)
                        .show();
            } else {
                activity.runOnUiThread(() -> showWait(activity, type, listener));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void dismissWait(Activity activity) {
        if (activity != null && !activity.isDestroyed()) {
            if (Looper.getMainLooper() == Looper.myLooper()) {
                if (activity.isFinishing()) {
                    return;
                }
                dismiss();
            } else {
                activity.runOnUiThread(() -> dismissWait(activity));
            }
        }
    }

    public synchronized boolean isShow() {
        return mProgress != null && mProgress.isShowing();
    }
}
