package com.example.applock.allmanager;

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.example.applock.R;

import com.example.applock.databinding.LockActionViewBinding;
import com.example.applock.listener.InputKeyListener;

public class LockAction {

    private Context context;

    private LockActionViewBinding lockActionViewBinding;
    private WindowManager manager;

    private static LockAction lockAction;
    private WindowManager.LayoutParams layParams;

    public LockAction(Context context) {
        this.context = context;
        manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        lockActionViewBinding = LockActionViewBinding.inflate(LayoutInflater.from(context), null, false);
        setLocation();
        lockActionViewBinding.inputView.setInputKeyListener(new InputKeyListener() {
            @Override
            public void onInputComplete(String pin) {

                String curPin = Datamanager.getTopKey();
                if (curPin.equals(pin)) {
                    HideLockView();
                } else {
                    Toast.makeText(context, context.getString(R.string.not_key), Toast.LENGTH_SHORT).show();
                    AppManager.startVibrator(context);
                    lockActionViewBinding.inputView.clearTopKey();

                }
            }

            @Override
            public void onInputUnComplete(String pin) {

            }
        });
    }

    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 (lockAction == null) {
            lockAction = new LockAction(context);
        }
        return lockAction;
    }

    public void showLockView() {
        lockActionViewBinding.inputView.clearTopKey();
        if (lockActionViewBinding.getRoot().getWindowToken() == null) {
            manager.addView(lockActionViewBinding.getRoot(), layParams);
        }
    }


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