package macampcorp.macamp.activities;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.KeyguardManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.PowerManager;
import android.os.SystemClock;
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.view.WindowManager;
import android.widget.TextView;

import java.lang.reflect.Method;
import java.util.ArrayList;

import macampcorp.macamp.game.Game;
import macampcorp.macamp.game.Resource;
import macampcorp.macamp.lang.Locale;
import macampcorp.macamp.utils.files.FileManager;



public class PermissionActivity extends AppCompatActivity {
    private int m_nPerm;
    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;

    protected static final String PERMISSIONS[] = {
            Manifest.permission.CAMERA
    };

    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);

        m_nPerm=PERMISSIONS.length;
        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));
        }

        for(int i=0; i<PERMISSIONS.length; i++) {
            if (ContextCompat.checkSelfPermission(this, PERMISSIONS[i])
                    != PackageManager.PERMISSION_GRANTED) {
                al.add(PERMISSIONS[i]);
            } 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(R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        startGame(false);
                    }
                });
                builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        startGame(true);
                    }
                });
        // Set other dialog properties
        builder.setMessage(R.string.toast_continue);

        // 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(boolean  erase)
    {
        HostActivity.startGame(this, erase);
        finish();
    }

    public void onUnderstood(View v)
    {
        //CrashActivity.start(this);

        if(m_nPerm==0)
        {
            askForReset();
        }
    }


}
