package com.example.applock.allmanager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import com.example.applock.R;

import com.example.applock.activity.MainActivity;
import com.example.applock.databinding.DialogPermissionBinding;
import com.example.applock.listener.PermissionBtnListener;
import com.example.applock.value.MyAllValues;

public class SoftWarePerssionDialog extends DialogFragment {
    private DialogPermissionBinding dialogPermissionBinding;
    private PermissionBtnListener permissionBtnListener;
    private Activity mContext;


    public SoftWarePerssionDialog(Activity context) {
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dialogPermissionBinding = DialogPermissionBinding.inflate(inflater, null, false);
        init();
        return dialogPermissionBinding.getRoot();
    }

    public void setPermissionBtnListener(PermissionBtnListener permissionBtnListener) {
        this.permissionBtnListener = permissionBtnListener;
    }


    @Override
    public void onResume() {
        super.onResume();
        refreshBtnStatus(mContext);
    }

    public void refreshBtnStatus(Context context) {
        boolean getUsagePermission = SoftLockPermission.isGetUsagePermission(context);
        boolean isOverlay = SoftLockPermission.isOverlays(context);
        dialogPermissionBinding.tvUsageButton.setSelected(getUsagePermission);
        dialogPermissionBinding.tvOverlayButton.setSelected(isOverlay);
    }

    private void init() {

        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(R.color.nobackground);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(wlp);
        setCancelable(true);
        dialogPermissionBinding.tvUsageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!dialogPermissionBinding.tvUsageButton.isSelected()) {
                    SoftLockPermission.requestUsage(mContext, MyAllValues.request_code_usage);
                }
            }
        });

        dialogPermissionBinding.tvOverlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!dialogPermissionBinding.tvOverlayButton.isSelected()) {
                    SoftLockPermission.requestOverlays(mContext, MyAllValues.request_code_overlay);
                }
            }
        });
    }
}
