package com.lc.myapplication.security.locks.tool;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.lc.myapplication.security.locks.R;
import com.lc.myapplication.security.locks.databinding.MyServiceViewBinding;
import com.lc.myapplication.security.locks.listener.InputCodeFinishListener;

public class LockAction {
        private Context context;

        private MyServiceViewBinding myServiceViewBinding;
        private WindowManager manager;

        private static LockAction myIntentSerivice;
        private WindowManager.LayoutParams layParams;

        public LockAction(Context context) {
            this.context = context;
            manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            myServiceViewBinding = MyServiceViewBinding.inflate(LayoutInflater.from(context), null, false);
            setLocation();
            myServiceViewBinding.passcodeDrawInput.setInputFinish(new InputCodeFinishListener() {
                @Override
                public void CodeFinish(String code) {
                    String curPin = MyTools.getPassword();
                    if (curPin.equals(code)) {
                        HideLockView();
                    } else {
                        Toast.makeText(context, context.getString(R.string.not_key), Toast.LENGTH_SHORT).show();
                        AppsManager.vir(context);
                        myServiceViewBinding.passcodeDrawInput.ClearInputCode();

                    }
                }

                @Override
                public void CodeNotFinish(String code) {

                }
            });
        }

        private void setLocation() {
            int type = 0;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                type = WindowManager.LayoutParams.TYPE_PHONE;
            }
            DisplayMetrics displayMetrics = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(displayMetrics);
            int flag = 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;
            layParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, type, flag, PixelFormat.RGBA_8888);

        }

        public static LockAction getActionInstance(Context context) {
            if (myIntentSerivice == null) {
                myIntentSerivice = new LockAction(context);
            }
            return myIntentSerivice;
        }

        public void showLockView() {
            myServiceViewBinding.passcodeDrawInput.ClearInputCode();
            if (myServiceViewBinding.getRoot().getWindowToken() == null) {
                manager.addView(myServiceViewBinding.getRoot(), layParams);
            }
        }


        public void HideLockView() {
            if (myServiceViewBinding.getRoot().getWindowToken() != null) {
                manager.removeView(myServiceViewBinding.getRoot());
            }
        }
}
