package com.kfzs.duanduan.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import com.sheep.jiuyan.samllsheep.R;
import com.kfzs.duanduan.utils.dlg.ViewFindUtils;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  DialogStorageLow</p>
 * @ <p>Description: 存储不足时的弹窗类</p>
 * @ date:  2017/7/3 17:48
 * @ QQ:    315096953
 */

public class DialogStorageLow {

    private static AlertDialog mAlertDialog;

    private static AlertDialog mAlertDialogs;

    public static AlertDialog showDialog(final Context activity) {
        if (mAlertDialog == null) {
            View view = View.inflate(activity, R.layout.diag_show_install, null);
            ViewFindUtils.find(view, R.id.btn_diag_show_install_cancle)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mAlertDialog.dismiss();
                        }
                    });
            ViewFindUtils.find(view, R.id.btn_diag_show_install_manage)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mAlertDialog.dismiss();
                            Intent intent = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            activity.startActivity(intent);
                        }
                    });
            mAlertDialog = new AlertDialog.Builder(activity)
                    .setView(view)
                    .create();

        }
        //设置弹出全局对话框
        mAlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
//        if (!HelperUtils.isActivityOnTop((Activity) activity)) {
//            return mAlertDialog;//如果不是顶层窗口，那就不显示了。
//        }

        mAlertDialog.show();
        return mAlertDialog;
    }

    /**
     * 显示弹出框
     *
     * @param context
     */
    public static void showPopupWindow(final Context context) {
        // 获取WindowManager
        final WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);


        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        // 类型
        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        // 设置flag
        params.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        // 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE，弹出的View收不到Back键的事件
        // 不设置这个弹出框的透明遮罩显示为黑色
        params.format = PixelFormat.TRANSLUCENT;
        // FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口
        // 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时，后面的应用图标由不可长按变为可长按
        // 不设置这个flag的话，home页的划屏会有问题
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;

        final View mView = LayoutInflater.from(context).inflate(R.layout.diag_show_install, null);
        Button tv_itemdialog_ok = mView.findViewById(R.id.btn_diag_show_install_manage);
        Button tv_itemdialog_close = mView.findViewById(R.id.btn_diag_show_install_cancle);

        tv_itemdialog_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 隐藏弹窗
                mWindowManager.removeView(mView);
                Intent intent = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });

        tv_itemdialog_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWindowManager.removeView(mView);
            }
        });

        mWindowManager.addView(mView, params);
    }
}
