package com.appli.gui;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.appli.patoche.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Patrice on 06/01/2018.
 */

public class manage_permissions extends AppCompatActivity
{
    private static final int PERMISSIONS_READ_STORAGE = 0;
    private static final int PERMISSIONS_READ_CONTACTS = 1;


    public static final int MULTIPLE_PERMISSIONS = 10; // code you want.

    String[] permissions_config= new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION};

/*
    READ_EXTERNAL_STORAGE
    WRITE_EXTERNAL_STORAGE
    ACCESS_WIFI_STATE
    ACCESS_NETWORK_STATE
    ACCESS_FINE_LOCATION
    ACCESS_COARSE_LOCATION
    RECORD_AUDIO
    CAPTURE_VIDEO_OUTPUT
    CAPTURE_AUDIO_OUTPUT
    CAMERA
*/

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

        if (checkPermissions())
        {
            //  manage_permissions  granted.
            Toast.makeText(manage_permissions.this, "All manage_permissions are available ",   Toast.LENGTH_LONG).show();
        }

        /*  permisson simple
        if (Build.VERSION.SDK_INT >= 23)
        {
            //checkSelfPermission
            if (ContextCompat.checkSelfPermission(manage_permissions.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
            {
                requestPermissions(new String[]  {Manifest.permission.WRITE_EXTERNAL_STORAGE},  PERMISSIONS_READ_STORAGE);
                return;
            }

        }
        else
        {
            // Pre-Marshmallow
            Toast.makeText(manage_permissions.this, "Not applicable",   Toast.LENGTH_LONG).show();
        }
        */

        /* multiples manage_permissions */
        /*if (Build.VERSION.SDK_INT >= 23)
        {

            List<String> listPermissionsNeeded = new ArrayList<>();

            if (checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)
            {
                listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS);
            }
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
            {
                listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }

            Manifest.permission.



            if (!listPermissionsNeeded.isEmpty())
            {
                ActivityCompat.requestPermissions(manage_permissions.this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), PERMISSIONS_READ_CONTACTS);
            }

        } else {
            // Pre-Marshmallow
        }*/

    }

    /*
    @Override
    public void onRequestPermissionsResult(int requestCode, String manage_permissions[], int[] grantResults)
    {

        switch (requestCode)
        {
            case PERMISSIONS_READ_STORAGE:
            {

                if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    Toast.makeText(manage_permissions.this, "Permission Granted", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(manage_permissions.this, "Permission denied", Toast.LENGTH_LONG).show();
                    // permission denied, Disable
                }
                return;
            }
        }

    }
*/

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
    {
        switch (requestCode)
        {
            case MULTIPLE_PERMISSIONS:
                {
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    // manage_permissions granted.
                }
                else {
                    String aaapermissions = "";
                    for (String per : permissions_config)
                    {
                        aaapermissions += "\n" + per;
                    }
                    // manage_permissions list of don't granted permission
                }
                return;
            }
        }
    }


    private  boolean checkPermissions()
    {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();

        for (String p:permissions_config)
        {
            result = ContextCompat.checkSelfPermission(manage_permissions.this ,p);
            if (result != PackageManager.PERMISSION_GRANTED)
            {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty())
        {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),MULTIPLE_PERMISSIONS );
            return false;
        }
        return true;
    }





}
