package app.mar.activities;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

import app.mar.game.Game;
import app.mar.utils.files.FileManager;

public class PermissionActivity extends AppCompatActivity {
    private int m_nPerm=3;
    static final int PERM_FINE_LOCATION=1337;
    static final int ALL_PERMISSIONS=1337;
    static final int PERM_CAMERA=1338;
    static final int PERM_COARSE_LOCATION=1339;
    protected TextView mText;

    private static final String DISCLAMER="<h1>Attention :</h1> \n" +
            "<h2>Ce jeu nécessite des déplacement en ville.</h2>\n" +
            "Veillez à respecter les règles suivantes : <br>\n" +
            "Les enfants doivent être accompagnés d’un adulte. <br>\n" +
            "Faites attention à la circulation. <br>\n" +
            "Soyez vigilant sur l’environnement (altitude, marches, etc). <br>\n" +
            "Ne jouer pas en conduisant. <br>\n" +
            "\n" +
            "<h3>Restez toujours conscient de votre environnement.</h3>\n" +
            "<h3>En continuant, vous convenez que l’utilisation du jeu est à vos propres risques et responsabilités.</h3>";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        ArrayList<String> al = new ArrayList<String>();

        

        FileManager.init(this);
        mText = (TextView) findViewById(R.id.disclamer);
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
            mText.setText(Html.fromHtml(DISCLAMER));
        }else
        {
            mText.setText(Html.fromHtml(DISCLAMER, Html.FROM_HTML_MODE_COMPACT));
        }


        if (ContextCompat.checkSelfPermission(this,   Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            al.add(Manifest.permission.ACCESS_FINE_LOCATION);
        } else m_nPerm--;

        if (ContextCompat.checkSelfPermission(this,   Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            al.add(Manifest.permission.CAMERA);
        } else m_nPerm--;


        if (ContextCompat.checkSelfPermission(this,   Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            al.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        } else m_nPerm--;
        try {
            if (al.size() > 0)
                ActivityCompat.requestPermissions(this, al.toArray(new String[m_nPerm]), ALL_PERMISSIONS);
        }catch(Exception e) {
            e.printStackTrace();
        }

    }


    public void askForReset()
    {
        final Activity t = this;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // Add the buttons
                builder.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        startGame();
                    }
                });
                builder.setNegativeButton("Non", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Game.erase(t);
                        Game.setGame(null);
                        startGame();
                    }
                });
        // Set other dialog properties
        builder.setMessage("Voulez vous continuer la partie ?");

        // Create the AlertDialog
        AlertDialog dialog = builder.create();
        dialog.show();
    }


    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        for(int i=0; i<permissions.length; i++) {
            switch (requestCode) {
                case ALL_PERMISSIONS:
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                        m_nPerm--;
                    else
                    {
                        finish();
                        System.exit(0);
                    }
                    break;
            }
        }


    }

    public void startGame()
    {
        Intent intent = new Intent(this, MenuActivity.class);
        startActivity(intent);
        finish();
    }

    public void onUnderstood(View v)
    {
        if(m_nPerm==0)
        {
            askForReset();
        }
    }

}
