package dev.paytrack.paytrack.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import dev.paytrack.paytrack.adapter.PaymentAdapter;
import dev.paytrack.paytrack.domain.Transaction;
import dev.paytrack.paytrack.model.PaymentItem;
import dev.paytrack.paytrack.R;
import dev.paytrack.paytrack.service.TransactionService;
import dev.paytrack.paytrack.utils.Utils;

public class TripActivity extends AppCompatActivity {

    private GoogleMap mMap;
    private MapView mMapView;

    private TransactionService transactionService;

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

        mMapView = (MapView) findViewById(R.id.mapView);

        ArrayList<PaymentItem> paymentItems = new ArrayList<>();
        paymentItems.add(
                new PaymentItem(
                        "Starbucks Pl. Universitat",
                        4.70
                )
        );
        paymentItems.add(
                new PaymentItem(
                        "Restaurante el Puma",
                        12.30
                )
        );
        paymentItems.add(
                new PaymentItem(
                        "Tiger",
                        1.00
                )
        );

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        assert recyclerView != null;
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(new PaymentAdapter(paymentItems));

        initialitzeMap();
    }

    private void initialitzeMap() {
        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;

                //we search for the fib
                LatLng BCN = getLocationFromAddress("Barcelona");
                if (BCN != null) {
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(BCN)      // Sets the center to BCN
                            .zoom(10)         // we set the zoom
                            .build();
                    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 5000, null);
                }
                locateTransactions();
            }
        });
    }

    private void locateTransactions() {
        List<Transaction> transactions = transactionService.getTransactionsByOriginIbanBetweenDates("iban", new Date(), new Date());
        for (Transaction t : transactions) {
            LatLng position = getLocationFromAddress(t.getCounterPartyName());
            if (position != null) {
                MarkerOptions marker = new MarkerOptions()
                        .position(position)
                        .title(String.valueOf(t.getAmount()));
                mMap.addMarker(marker);
            }
        }
    }

    public LatLng getLocationFromAddress(String strAddress) {
        return Utils.getLocationFromAddress(strAddress, getApplicationContext());
    }
}

@@ -127,16 +127,62 @@
 Bundle;%0A
+import android.support.v4.app.ActivityCompat;%0A
 import a
@@ -214,32 +214,32 @@
 .ContextCompat;%0A
-
 import android.s
@@ -373,16 +373,143 @@
 rView;%0A%0A
+import com.google.android.gms.common.GooglePlayServicesNotAvailableException;%0Aimport com.google.android.gms.maps.CameraUpdate;%0A
 import c
@@ -646,16 +646,68 @@
 apView;%0A
+import com.google.android.gms.maps.MapsInitializer;%0A
 import c
@@ -1787,24 +1787,959 @@
 .mapView);%0A%0A
+        mMapView.onCreate(savedInstanceState);%0A        mMapView.onResume(); //without this, map showed but was empty%0A%0A        // Gets to GoogleMap from the MapView and does initialization stuff%0A        mMap.getUiSettings().setMyLocationButtonEnabled(false);%0A        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) %7B%0A            return;%0A        %7D%0A        mMap.setMyLocationEnabled(true);%0A%0A        // Needs to call MapsInitializer before doing any CameraUpdateFactory calls%0A        MapsInitializer.initialize(getApplicationContext());%0A%0A        // Updates the location and zoom of the MapView%0A        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(44.14, 14.2), 10);%0A        mMap.animateCamera(cameraUpdate);%0A%0A
         Arra
@@ -3612,34 +3612,8 @@
 );%0A%0A
-        initialitzeMap();%0A
     

add new version of initialize mapview