/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

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.ui.Sound;
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,
            Manifest.permission.VIBRATE
    };

    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(getResources().getString(R.string.text_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();
        }

        Sound.init(this);
    }






    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 onUnderstood(View v)
    {
        //CrashActivity.start(this);

        if(m_nPerm==0)
        {
            MenuActivity.start(this);
        }
    }




}
