package com.tool.applockpro.tool;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.tool.applockpro.ApplockSkin;
import com.tool.applockpro.R;
import com.tool.applockpro.adapter.PinAdapter;


public class Mymanager {
    private static Mymanager myManager;
    private Context mContext;
    private View mView;
    private WindowManager windowManager;
    private TextView btncontinue;
    private PinAdapter pinAdapter;
    private WindowManager.LayoutParams layoutParams;
    private MyinputPWD myinputPWD;
    private StringBuilder stringBuilder = new StringBuilder();

    public Mymanager(Context context) {
        mContext = context;


        mView = LayoutInflater.from(context).inflate(R.layout.lock_view, null, false);
        windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

        myinputPWD = mView.findViewById(R.id.myinput);
        pinAdapter = myinputPWD.getPinAdapter();

        initView();
        initEvent();

    }

    private void initEvent() {
        btncontinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Staticvalue.KEY_NUMBER.length() == 4) {
                    String pwd = Staticvalue.KEY_NUMBER;
                    if (pwd.equals(ApplockSkin.getPWD())) {
                        unLock();
                        Staticvalue.KEY_NUMBER = "";
                        pinAdapter.updateItem(Staticvalue.KEY_NUMBER);
                    } else {
                        showLockView();
                        Staticvalue.KEY_NUMBER = "";
                        pinAdapter.updateItem(Staticvalue.KEY_NUMBER);
                    }
                } else {
                    Staticvalue.KEY_NUMBER = "";
                    pinAdapter.updateItem(Staticvalue.KEY_NUMBER);
                }
            }
        });
    }


    public static synchronized Mymanager getInstance(Context context) {
        if (myManager == null) {
            myManager = new Mymanager(context);
        }
        return myManager;
    }

    @SuppressLint("WrongConstant")
    private void initView() {
        btncontinue = mView.findViewById(R.id.lock_btn_continue);
        int type = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        layoutParams = new WindowManager.LayoutParams();
        layoutParams.type = type;
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        layoutParams.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;

//        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
//                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
////                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
//                WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;


        layoutParams.gravity = Gravity.CENTER;
        Point screenSize = new Point();
        windowManager.getDefaultDisplay().getRealSize(screenSize);
        layoutParams.width = screenSize.x;
        layoutParams.height = screenSize.y;
    }

    public void showLockView() {
        if (!isViewAttached()) {
            stringBuilder.setLength(0);
            Staticvalue.KEY_NUMBER = "";
            windowManager.addView(mView, layoutParams);
            Log.e("zzj", "------showLockView success---");
        }
    }

    public void unLock() {
        try {
            windowManager.removeView(mView);
            Log.e("zzj", "------unlock success---");
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("zzj", "------unlock error---" + e.getMessage());
        }
    }

    private boolean isViewAttached() {
        return mView.getParent() != null;
    }

}
