package com.secspace.lib.common.function;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;

import com.secspace.lib.common.R;
import com.secspace.lib.common.dialog.DotRotateDialog;
import com.secspace.lib.common.dialog.OptimizeDialog;
import com.secspace.lib.common.dialog.SecSpaceUpdateDialog;
import com.secspace.lib.common.dialog.SimpleDialog;
import com.secspace.lib.common.dialog.SwitcherDialog;
import com.secspace.lib.common.env.BlackandWhiteList;
import com.secspace.lib.common.event.ExitEvent;
import com.secspace.lib.common.event.ResetPwdEvent;
import com.secspace.lib.common.utils.DpmHelper;
import com.secspace.lib.common.utils.PkgManager;
import com.secspace.lib.common.utils.Prefs;
import com.secspace.lib.common.utils.ToastUtil;
import com.secspace.mdmengine.api.manager.MdmFactoryManager;

import org.greenrobot.eventbus.EventBus;

import static com.secspace.lib.common.env.Packages.CompSecSpace.CLASSNAME_DPM_ADMIN;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_MIDDLEWARE;

/**
 * Created by cyw_m on 2018/4/6.
 */
@SuppressLint("StaticFieldLeak")
public class DialogManager {
    private static DialogManager mInstance = null;
    private SimpleDialog mSimpleDialog;
    private DotRotateDialog mDotRotateDialog;
    private OptimizeDialog mOptimizeDialog;
    private SecSpaceUpdateDialog mPresetAppUpdateDialog;


    private DialogManager() {
    }

    public static DialogManager getInstance() {
        if (mInstance == null) {
            synchronized (DialogManager.class) {
                if (mInstance == null) {
                    mInstance = new DialogManager();
                }
            }
        }
        return mInstance;
    }


    /**
     * 安装中间件弹窗
     */
    public void showInstallMiddleWare(Activity activity) {
        final Context context = activity.getApplication();
        mSimpleDialog = new SimpleDialog(activity).show(context.getResources().getString(R.string.common_remind)
                , context.getResources().getString(R.string.common_need_install_middleware)
                , false
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        WorkManager.INSTANCE.installMiddleWare(context);
                        dialog.dismiss();
                    }
                }
                , null
        );
    }

    public void showMiddleWareDeviceOwnerConfirmDialog(Activity activity) {
        if (DpmOwnerHelper.INSTANCE.isSettingDeviceOwner()) {
            return;
        }
        final Context context = activity.getApplicationContext();
        dismissSimpleDialog();
        mSimpleDialog = new SimpleDialog(activity).show(activity.getResources().getString(R.string.common_remind)
                , activity.getResources().getString(R.string.work_confirm_device_owner)
                , false
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DpmOwnerHelper.INSTANCE.disableAccountsAndRemoveProfileUsers(context);
                        SwitcherDialog.getInstance(context).show(context.getString(R.string.work_del_account), "Clear Account");
                        dialog.dismiss();
                    }
                }
                , null
        );
    }

    public void showRemoveUserConfirmDialog(Activity activity) {
        mSimpleDialog = new SimpleDialog(activity).show(activity.getResources().getString(R.string.common_remind)
                , activity.getResources().getString(R.string.work_remove_user_confirm_device_owner)
                , false
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DpmOwnerHelper.INSTANCE.removeOtherUsers();
                    }
                }
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        EventBus.getDefault().post(new ExitEvent());
                    }
                }
        );
    }

    /**
     * 关闭普通dialog
     */
    public void dismissSimpleDialog() {
        if (mSimpleDialog != null) {
            mSimpleDialog.dismiss();
        }
    }

    public void showPrivacyPolicyDialog(Activity activity) {
        new PrivacyPolicyDialog(activity).show();
    }

    public void showBuyCodeDialog(Activity activity) {
        new BuyCodeDialog(activity).show();
    }
}
