package app.brest.testmin3d;


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.support.v7.app.ActionBar;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.util.List;

import app.brest.app.brest.ui.CustomToggleButton;
import app.brest.app.brest.ui.OnToggleListener;
import app.brest.app.brest.ui.RotateButton;
import app.brest.app.brest.ui.SelectButton;
import app.brest.utils.Settings;
import app.brest.utils.app.brest.game.Game;

/**
 * A {@link PreferenceActivity} that presents a set of application settings. On
 * handset devices, settings are presented as a single list. On tablets,
 * settings are split by category, with category headers shown to the left of
 * the list of settings.
 * <p/>
 * See <a href="http://developer.android.com/design/patterns/settings.html">
 * Android Design: Settings</a> for design guidelines and the <a
 * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
 * API Guide</a> for more information on developing a Settings UI.
 */
public class SettingsActivity extends Activity implements app.brest.app.brest.ui.OnToggleListener, View.OnClickListener {

    protected Game         mGame;
    protected LinearLayout mRoot;
    protected LinearLayout mRootDev;
    protected CustomToggleButton mAccelerometer;
    protected CustomToggleButton mGPS;
    protected CustomToggleButton mPlaces;
    protected CustomToggleButton mAreas;
    protected CustomToggleButton mDev;
    protected CustomToggleButton mRA;
    protected SelectButton mSensorLatency;
    protected Button mReset;
    protected Button mCheat;
    protected Button mOk;



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        mGame = Game.game();
        mRootDev=new LinearLayout(this);
        mRootDev.setOrientation(LinearLayout.VERTICAL);
        mAccelerometer = new CustomToggleButton(this);
        mGPS = new CustomToggleButton(this);
        mDev = new CustomToggleButton(this);
        mDev.setOnClickListener((app.brest.app.brest.ui.OnToggleListener)this);
        mPlaces=new CustomToggleButton(this);
        mRA=new CustomToggleButton(this);
        mAreas=new CustomToggleButton(this);
        mSensorLatency = new SelectButton(this, Settings.SENSORS_LATENCIES);
        mCheat = newButton("Avoir toutes les ressources");
        mReset = newButton("Supprimer la sauvegarde");
        mRoot = (LinearLayout)findViewById(R.id.root_layout);
        mOk = (Button)findViewById(R.id.sbutok);
        addSelection("Latence des capteurs:", mSensorLatency, mRoot);
        addAction("", mReset, mRoot);
        addOptionBool("Mode Réalitée augmentée", mRA, mRoot);
        addOptionBool("Mode développeur:", mDev, mRoot);
        mRoot.addView(mRootDev);
        addOptionBool("GPS préprogrammé:", mGPS, mRootDev);
        addOptionBool("Afficher les zones:", mAreas, mRootDev);
        addOptionBool("Afficher les ressources:", mPlaces, mRootDev);
        addAction("", mCheat, mRootDev);
        preset();
    }

    protected Button newButton(String text)
    {
        Button b = new Button(this);
        b.setText(text);
        b.setBackgroundResource(R.drawable.uibutton);
        b.setTextColor(getResources().getColor(R.color.dull_4));
        b.setOnClickListener(this);
        return b;
    }

    private void reset()
    {
        final Activity context = this;
        AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
        builder1.setMessage("Cette action va supprimer toute la sauvegarde. La progression dans le jeu reviendra donc a 0. Êtes-vous sûr de vouloir continuer ?");
        builder1.setCancelable(true);

        builder1.setPositiveButton(
                "Supprimer",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Game.setGame(new Game("game_medium", context));
                        Toast.makeText(context, "Suppression effectuée", Toast.LENGTH_SHORT);
                        dialog.cancel();
                    }
                });

        builder1.setNegativeButton(
                "Ne pas supprimer",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alert11 = builder1.create();
        alert11.show();
    }

    public void onClick(View v)
    {
        if(v==mOk)
        {
            setResults();
            finish();
        }
        else if(v==mReset)
        {
            reset();
            setResults();
        }else if(v==mCheat)
        {
            mGame.pickAllResoucres();
        }
    }

    public void addOptionBool(String name, CustomToggleButton v, LinearLayout root)
    {
        LinearLayout ll = new LinearLayout(this);
        TextView tv = new TextView(this);
        tv.setText(name);
        tv.setTextColor(getResources().getColor(R.color.dull_4));

        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.addView(tv);
        ll.addView(v);
        root.addView(ll);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(30, 30, 25, 5);
        ll.setLayoutParams(lp);
        lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 40 , 40);
        tv.setLayoutParams(lp);
        v.setLayoutParams(new LinearLayout.LayoutParams(150, 60));
        newSeparator(root);
    }

    public void addAction(String name, Button v, LinearLayout root)
    {
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.addView(v);
        root.addView(ll);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(30, 30, 25, 5);
        ll.setLayoutParams(lp);
        v.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 60));
        newSeparator(root);
    }

    public void addSelection(String name, SelectButton v, LinearLayout root)
    {
        LinearLayout ll = new LinearLayout(this);
        TextView tv = new TextView(this);
        tv.setText(name);
        tv.setTextColor(getResources().getColor(R.color.dull_4));

        ll.setOrientation(LinearLayout.VERTICAL);
        ll.addView(tv);
        ll.addView(v);
        root.addView(ll);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(30, 30, 25, 5);
        ll.setLayoutParams(lp);
        lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(90, 0, 0 , 0);
        v.setLayoutParams(lp);
        //v.setLayoutParams(new LinearLayout.LayoutParams(150, 60));
        newSeparator(root);
    }

    public View newSeparator(LinearLayout root)
    {
        View v = new View(this);
        v.setBackgroundColor(getResources().getColor(R.color.dull_3));
        root.addView(v);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2);
        lp.setMargins(30, 30, 0,0);
        v.setLayoutParams(lp);
        return v;
    }


    private void setResults()
    {
        mGame.getmSettings().setDevelopperMode(mDev.getState());
        mGame.getmSettings().setGPSDebug(mGPS.getState());
        mGame.getmSettings().setAreaDebug(mAreas.getState());
        mGame.getmSettings().setARMode(mRA.getState());
        mGame.getmSettings().setResourceDebug(mPlaces.getState());
        mGame.getmSettings().setSensorLatency(mSensorLatency.getState());
        Settings.setSettings(mGame.getmSettings());
        mGame.save(this);
        mGame.getPlayer().newSensorManager(this);
    }

    private void updateVisibility()
    {
        int x = mDev.getState()?View.VISIBLE:View.GONE;
        mRootDev.setVisibility(x);
    }

    private void preset()
    {
        mDev.setState(mGame.getmSettings().isDevelopperMode());
        mGPS.setState(mGame.getmSettings().isGPSDebug());
        mAreas.setState(mGame.getmSettings().isAreaDebug());
        mPlaces.setState(mGame.getmSettings().isResourceDebug());
        mRA.setState(mGame.getmSettings().isARMode());
        mSensorLatency.setState(mGame.getmSettings().getSensorLatency());
        updateVisibility();
    }

    @Override
    public void onToggle(View v) {
        if(v==mDev) updateVisibility();
    }

    @Override
    public void onOn(View v) {}

    @Override
    public void onOff(View v) {}


}
