package learn.navdrawbase;


import android.app.TaskStackBuilder;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Activity_Place extends BaseActivity {
    private long id;
    private int position;
    private String titreLieu;
    private View rootView;
    private GoogleMap map;
    private MapView mapView;
    private JSONObject jo;
    protected LatLng TutorialsPoint = null;


    private class MyJsonReponse implements Response.Listener<JSONArray> {
        @Override
        public void onResponse(JSONArray response) throws JSONException {
            for (int i=0; i<response.length(); i++) {
                try {
                    jo = response.getJSONObject(i);

                } catch (Exception e) {
                    Log.v("debug", e.toString());
                }
            }
            affichage();
        }
    }


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

        Intent intent = getIntent();

        id = intent.getLongExtra("id", -1);
        position = intent.getIntExtra("position", -1);

        /**** Appel de l'url ******/
        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "http://omany.fr/SQL/affichage.php?id="+Long.toString(id+1); // https://lecarpentier.users.greyc.fr/restapi/messages.php
        // Request a string response from the provided URL.
        JsonArrayRequest jsonRequest = new JsonArrayRequest(url,
                new MyJsonReponse(), // la classe interne qui sera appelée quand on reçoit la réponse
                new com.android.volley.Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.v("info", "That didn't work!");
                    }

                }

        );
        // Add the request to the RequestQueue.
        queue.add(jsonRequest);

    }



    protected void affichage() {
        /*** Gestion du titre *****/
        String texte = null;
        String text1 = null;
        String text2 = null;

        try {
            texte = jo.getString("NomLieu")+"\n";
            titreLieu = jo.getString("NomLieu");
            Log.v("lol", texte);
        } catch (JSONException e) {
            e.printStackTrace();
        };

        TextView text = (TextView) findViewById(R.id.titre);
        text.setText(texte);


        try {
            float lat = Float.parseFloat(jo.getString("Latitude"));
            float lng = Float.parseFloat(jo.getString("Longitude"));
            TutorialsPoint = new LatLng(lat, lng);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        /***** Partie map *****/
        try {
            if (map == null) {
                map = ((MapFragment) getFragmentManager().
                        findFragmentById(R.id.map)).getMap();
            }
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            Marker TP = map.addMarker(new MarkerOptions().
                    position(TutorialsPoint).title(jo.getString("NomLieu")));

            double zoomLevel = 16.0; //This goes up to 21
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(TutorialsPoint, (float) zoomLevel));


        }
        catch (Exception e) {
            e.printStackTrace();
        }

        try {
            text1 = "\n"+jo.getString("Rue")+"\n"+jo.getString("CodePostal")+", "+jo.getString("Ville")+"\n";
        } catch (JSONException e) {
            e.printStackTrace();
        }
        /*** stats **/
        TextView texto = (TextView) findViewById(R.id.adresseinfo);
        texto.setText(text1);

        /** stats */
        text2 = "Aucunes informations pour le moment !";
        TextView texto1 = (TextView) findViewById(R.id.stats);
        texto1.setText(text2);

    }


    public void taguer(View v){
        Intent myIntent = new Intent(this, Activity_Tag.class);
        myIntent.putExtra("id", id);
        myIntent.putExtra("position", position);
        myIntent.putExtra("titreLieu", titreLieu);
        createBackStack(myIntent);
    }

    private void createBackStack(Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            TaskStackBuilder builder = TaskStackBuilder.create(this);
            builder.addNextIntentWithParentStack(intent);
            builder.startActivities();
        } else {
            startActivity(intent);
            finish();
        }
    }

    /** HIDE TOOLBAR **/
//    @Override
//    protected boolean useToolbar() {
//        return false;
//    }



    /** HIDE hamburger menu **/
//    @Override
//    protected boolean useDrawerToggle() {
//        return false;
//    }

}
