package com.hadinour.hnweather;

import android.*;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Geocoder;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.SearchView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.identity.intents.Address;
import com.google.android.gms.location.LocationServices;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hadinour.hnweather.Interfaces.WeatherService;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import okhttp3.Interceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    private static GoogleApiClient googleApiClient;
    private static Location lastLocation;

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

        Retrofit retrofit = setupRetrofitWithBaseURL("https://api.flickr.com/services/rest/");

        WeatherService wService = retrofit.create(WeatherService.class);
        Call<String> weather = wService.getBackgroundImage();
        weather.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.d("Network", response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.d("onFailure", t.toString());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // perform query here
                query = query.trim();
                searchFor(query);
                searchView.setQuery(query, false);
                // workaround to avoid issues with some emulators and keyboard devices firing twice if a keyboard enter is used
                // see https://code.google.com/p/android/issues/detail?id=24599
                searchView.clearFocus();

                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }


    private void searchFor(String q) {
        Log.d("DEBUG", "search Entered: " + q);
    }

    private void createGAC() {
        if (googleApiClient == null) {
            googleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }

    }


    protected void onStart() {
        googleApiClient.connect();
        super.onStart();
    }

    protected void onStop() {
        googleApiClient.disconnect();
        super.onStop();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
                    15);
            return;
        }
        lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        if (lastLocation != null) {

            String lat = String.valueOf(lastLocation.getLatitude());
            String lng =String.valueOf(lastLocation.getLongitude());
            Log.d("Debug Location", "lat: " + lat + ", long: " + lng);
            Geocoder gcd = new Geocoder(this, Locale.getDefault());
            List<android.location.Address> addresses = null;
            try {
                addresses = gcd.getFromLocation(lastLocation.getLatitude(), lastLocation.getLongitude(), 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (addresses.size() > 0)
                Log.d("Debug Location", addresses.get(0).getLocality() + ", " + addresses.get(0).getCountryName());

        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d("suspended: " , String.valueOf(i));
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.d("WTF: ", connectionResult.toString());
    }

    private Retrofit setupRetrofitWithBaseURL(String url) {
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        return new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }
}
@@ -1601,626 +1601,8 @@
 C();
-%0A%0A        Retrofit retrofit = setupRetrofitWithBaseURL(%22https://api.flickr.com/services/rest/%22);%0A%0A        WeatherService wService = retrofit.create(WeatherService.class);%0A        Call%3CString%3E weather = wService.getBackgroundImage();%0A        weather.enqueue(new Callback%3CString%3E() %7B%0A            @Override%0A            public void onResponse(Call%3CString%3E call, Response%3CString%3E response) %7B%0A                Log.d(%22Network%22, response.body());%0A            %7D%0A%0A            @Override%0A            public void onFailure(Call%3CString%3E call, Throwable t) %7B%0A                Log.d(%22onFailure%22, t.toString());%0A            %7D%0A        %7D);
 %0A   
@@ -5035,334 +5035,9 @@
   %7D%0A
-%0A
     
-private Retrofit setupRetrofitWithBaseURL(String url) %7B%0A        Gson gson = new GsonBuilder()%0A                .setLenient()%0A                .create();%0A%0A        return new Retrofit.Builder()%0A                .baseUrl(url)%0A                .addConverterFactory(GsonConverterFactory.create(gson))%0A                .build();%0A    %7D%0A
 %7D

remove retrofit implementations to wrap it to class