package com.example.musedroid.musedroid;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.PlaceBuffer;
import com.google.android.gms.location.places.Places;


public class ShowActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
    RatingBar ratingBar;
    Button qrButton;
    Museum museum;
    Button goToMaps;
    Intent intent;
    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        TextView textDescription;
        Intent i = getIntent();
        mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .enableAutoManage(this, this)
                .build();
        qrButton = (Button) findViewById(R.id.qrButton);
        goToMaps = (Button) findViewById(R.id.goToMaps);

        if (i != null) {
            museum = i.getParcelableExtra("museum");
            setTitle(museum.name);
            getPlace(museum.placeId);
            textDescription = (TextView) findViewById(R.id.textDescription);
            textDescription.setText(museum.description);


        }

        goToMaps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(ShowActivity.this, MapsActivity.class);
                startActivity(intent);
            }
        });

        qrButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(ShowActivity.this, QrShowActivity.class);
                intent.putExtra("flag", false);
                intent.putExtra("museumId", museum.key);
                startActivity(intent);
            }
        });
    }

    private void getPlace(final String placeId) {
        Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
                .setResultCallback(new ResultCallback<PlaceBuffer>() {
                    public static final String TAG = "TAG";

                    @Override
                    public void onResult(PlaceBuffer places) {
                        if (places.getStatus().isSuccess() && places.getCount() > 0) {
                            final Place myPlace = places.get(0);
                            final float rating = myPlace.getRating();
                            ratingBar = (RatingBar) findViewById(R.id.ratingBar);
                            ratingBar.setNumStars(5);
                            ratingBar.setRating(rating);
                            Log.i(TAG, "Place found: " + myPlace.getName());
                        } else {
                            Log.e(TAG, "Place not found");
                        }
                        places.release();
                    }
                });
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }
}

@@ -62,24 +62,48 @@
 ent.Intent;%0A
+import android.net.Uri;%0A
 import andro
@@ -116,16 +116,16 @@
 Bundle;%0A
-
 import a
@@ -906,16 +906,65 @@
 intent;%0A
+    String museumName;%0A    String museumAddress;%0A
     priv
@@ -2040,65 +2040,223 @@
     
-intent = new Intent(ShowActivity.this, MapsActivity.class
+Uri gmmIntentUri = Uri.parse(%22google.navigation:q= %22+museumName+museumAddress);%0A                intent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);%0A                intent.setPackage(%22com.google.android.apps.maps%22
 );%0A 
@@ -2579,65 +2579,9 @@
 e);%0A
-                intent.putExtra(%22museumId%22, museum.key);
 %0A
+
     
@@ -3207,24 +3207,172 @@
 etRating();%0A
+                            museumName = myPlace.getName().toString();%0A                            museumAddress = myPlace.getAddress().toString();%0A
             
@@ -3785,24 +3785,25 @@
 .release();%0A
+%0A
             
@@ -3834,24 +3834,430 @@
   %7D);%0A    %7D%0A
+    public String latlongStringBuilder(String orgStr)%7B%0A       String result = orgStr.replace(%22)%22,%22%22).replace(%22(%22,%22%22).replace(%22lat/lng:%22,%22%22);;%0A//        StringBuilder builder = new StringBuilder();%0A//        builder.append(orgStr);%0A//        builder.toString().replace(%22)%22,%22%22);%0A//        builder.toString().replace(%22(%22,%22%22);%0A//        builder.toString().replace(%22lat/lng:%22,%22%22);%0A%0A%0A        return result;%0A    %7D
 %0A    @Overri

create button to redirect the app to google maps native app tou show direction to go to selected museum