package stickeradmin.signalstickers.trending;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import stickeradmin.signalstickers.trending.stickermaker.NewUserIntroActivity;
import stickeradmin.signalstickers.trending.stickermaker.WhatsAppBasedCode.StickerPackListActivity2;


public class RequestPermissions extends AppCompatActivity {

    private final String[] PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request_permissions);

        getPermissions();
    }

    private void getPermissions() {
        int permission = ActivityCompat.checkSelfPermission(RequestPermissions.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    RequestPermissions.this,
                    new String[]{
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE
                    },
                    1
            );
        }
        else
        {
            ShowMainActivity();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                try
                {
                    if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                        AlertDialog alertDialog = new AlertDialog.Builder(this)
                                .setPositiveButton("Let's Go", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        NewUserIntroActivity.verifyStoragePermissions(RequestPermissions.this);
                                    }
                                })
                                .create();
                        alertDialog.setTitle("Notice!");
                        alertDialog.setMessage("We've recognized you denied the storage access permission for this app."
                                + "\n\nIn order for this app to work, storage access is required.");
                        alertDialog.show();
                        getPermissions();
                    }
                    else {
                        ShowMainActivity();
                    }
                }
                catch(Exception ex)
                {}
                break;
        }

    }

    void ShowMainActivity()
    {
        Intent intent = new Intent(this, StickerPackListActivity2.class);// New activity
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish(); // Call once you redirect to another activity
    }

}