package com.tool.applockpro.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;

import com.tool.applockpro.ApplockSkin;
import com.tool.applockpro.R;
import com.tool.applockpro.databinding.ActivityPermissionBinding;
import com.tool.applockpro.tool.Myservice;
import com.tool.applockpro.tool.Mytools;

public class PermissionActivity extends AppCompatActivity {
    ActivityPermissionBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPermissionBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_Permission), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });



        binding.step2Text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent usageAccessIntent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
                startActivity(usageAccessIntent);
            }
        });
        binding.step1Text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent overlayIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                startActivity(overlayIntent);
            }
        });

    }


    private void starMainactivity() {
        if (Mytools.hasUsagePermission(this) && Mytools.hasOverlayPermission(this)) {
            Intent intent = new Intent(PermissionActivity.this, SetPWDActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.step1Text.setSelected(Mytools.hasOverlayPermission(this));
        binding.step2Text.setSelected(Mytools.hasUsagePermission(this));
        if (ApplockSkin.getPWD().isEmpty()) {
            starMainactivity();
        } else {
            Intent intent = new Intent(PermissionActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }


}