package dev.blunch.blunch.activity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
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.io.IOException;
import java.util.Date;
import java.util.List;

import dev.blunch.blunch.R;
import dev.blunch.blunch.domain.Menu;
import dev.blunch.blunch.services.MenuService;
import dev.blunch.blunch.services.ServiceFactory;

public class MenusLocationActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    Location localizacion;
    MenuService menuService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menus_location);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        localizacion = null;
        menuService = ServiceFactory.getMenuService(getApplicationContext());
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        } else {
            // Show rationale and request permission.
        }

//        if (gpsEnabled() && localizacion != null){
//            LatLng posicion = new LatLng(localizacion.getLatitude(),localizacion.getLongitude());
//            mMap.moveCamera(CameraUpdateFactory.newLatLng(posicion));
//        }
//        else {

            //we search for the fib
            LatLng FIB = getLocationFromAddress("C/Jordi Girona Salgado, 1-3, 08034 Barcelona");
            if(FIB != null) {
                mMap.addMarker(new MarkerOptions().position(FIB).title("DA FIB"));
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(FIB)      // Sets the center to DA FIB
                        .zoom(17)         // we set the zoom
                        .build();
                mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),5000,null);
            }
            else {
                Toast.makeText(this, "DA FIB DOESN'T EXIST ON EARTH!",
                        Toast.LENGTH_LONG).show();
            }
            situaMenus();
        }
//    }

    public void situaMenus(){
        List<Menu> menus = menuService.getMenus();
        for(Menu m : menus){
            if(m.getDateEnd().after(new Date())) {
                LatLng posicion = getLocationFromAddress(m.getLocalization());
                if (posicion != null) {
                    mMap.addMarker(new MarkerOptions().position(posicion).title(m.getName()));
                }
            }
        }
    }

    public LatLng getLocationFromAddress(String strAddress) {

        Geocoder coder = new Geocoder(this);
        List<Address> address = null;
        LatLng coordenades = null;
        try {
            try {
                address = coder.getFromLocationName(strAddress, 5);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (address == null) {
                return null;
            }
            Address location = address.get(0);
            coordenades = new LatLng(location.getLatitude(), location.getLongitude());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return coordenades;
    }
}

@@ -339,16 +339,63 @@
 ttings;%0A
+import android.support.design.widget.Snackbar;%0A
 import a
@@ -1216,16 +1216,59 @@
 Callback
+, GoogleMap.OnMyLocationButtonClickListener
  %7B%0A%0A    
@@ -1995,16 +1995,71 @@
 ogleMap;
+%0A        mMap.setOnMyLocationButtonClickListener(this);
 %0A%0A      
@@ -2333,263 +2333,8 @@
  %7D%0A%0A
-//        if (gpsEnabled() && localizacion != null)%7B%0A//            LatLng posicion = new LatLng(localizacion.getLatitude(),localizacion.getLongitude());%0A//            mMap.moveCamera(CameraUpdateFactory.newLatLng(posicion));%0A//        %7D%0A//        else %7B%0A%0A
     
@@ -2384,19 +2384,19 @@
  LatLng 
-FIB
+BCN
  = getLo
@@ -2418,43 +2418,8 @@
 ss(%22
-C/Jordi Girona Salgado, 1-3, 08034 
 Barc
@@ -2442,19 +2442,19 @@
      if(
-FIB
+BCN
  != null
@@ -2461,91 +2461,8 @@
 ) %7B%0A
-                mMap.addMarker(new MarkerOptions().position(FIB).title(%22DA FIB%22));%0A
     
@@ -2566,19 +2566,19 @@
 .target(
-FIB
+BCN
 )      /
@@ -2640,9 +2640,9 @@
 om(1
-7
+0
 )   
@@ -2819,163 +2819,8 @@
   %7D%0A
-            else %7B%0A                Toast.makeText(this, %22DA FIB DOESN'T EXIST ON EARTH!%22,%0A                        Toast.LENGTH_LONG).show();%0A            %7D%0A
     
@@ -3974,11 +3974,549 @@
 s;%0A    %7D
+%0A%0A    private Boolean gpsEnabled() %7B%0A        ContentResolver contentResolver = getBaseContext()%0A                .getContentResolver();%0A        boolean gpsStatus = Settings.Secure%0A                .isLocationProviderEnabled(contentResolver,%0A                LocationManager.GPS_PROVIDER);%0A         return gpsStatus;%0A    %7D%0A%0A    @Override%0A    public boolean onMyLocationButtonClick() %7B%0A        if(gpsEnabled()) return false;%0A        Toast.makeText(this, %22GPS not enabled%22,%0A                Toast.LENGTH_LONG).show();%0A        return false;%0A    %7D
 %0A%7D%0A

work on map