package com.anand.geofence.sample;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.anand.firebaseapp.R;
import com.anand.geofence.MapsActivity;
import com.anand.geofence.model.response.geofence.Geofence;
import com.anand.geofence.model.response.geofence.GetGeoLocationResponse;
import com.anand.geofence.reference.AppSharedpreferences;
import com.anand.geofence.retrofit.ApiClient;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RetrofitCall extends AppCompatActivity {
    private ApiClient apiClient;
    private AppSharedpreferences appSharedpreferences;
    TextView location;
    EditText pub_id;
    Button btn;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);
        location = findViewById(R.id.latlong);
        pub_id = findViewById(R.id.pub_id);
        btn = findViewById(R.id.btn);
        apiClient = ApiClient.getInstance();
        appSharedpreferences = AppSharedpreferences.getInstance(RetrofitCall.this);
        String geoLocationJson = appSharedpreferences.getString("geoLocation");

// Parse the JSON string into a list of Geofence objects
        List<Geofence> geofenceList = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(geoLocationJson);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Geofence geofence = new Geofence();
                geofence.setIdentifier(jsonObject.getString("identifier"));
                geofence.setLat(jsonObject.getString("lat"));
                geofence.setLog(jsonObject.getString("log"));
                geofence.setRadius(jsonObject.getString("radius"));
                geofenceList.add(geofence);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.i("LocationList", "LocationList : " + geofenceList);

// Iterate through the list of Geofence objects and extract the latitude and longitude values
        List<String> latitudes = new ArrayList<>();
        List<String> longitudes = new ArrayList<>();
        List<String> latLong = new ArrayList<>();
        for (Geofence geofence : geofenceList) {
            latitudes.add(geofence.getLat());
            longitudes.add(geofence.getLog());
            latLong.add(geofence.getLat() + "," + geofence.getLog());
        }
        Log.i("Latitudes Count", String.valueOf(latitudes.size()));
        Log.i("LatLong Count", String.valueOf(latLong.size()));
        Gson gson2 = new Gson();
        String json2 = gson2.toJson(latLong);
        Log.i("LatLong", json2);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String zoneid = pub_id.getText().toString();
                appSharedpreferences.saveString("zoneid", zoneid);
                getAllGeoLocations(zoneid);
            }
        });

    }

    private void getAllGeoLocations(String zoneid) {

        apiClient.getApiInterface().getAllGeoLocations(zoneid).enqueue(new Callback<GetGeoLocationResponse>() {
            @Override
            public void onResponse(Call<GetGeoLocationResponse> call, Response<GetGeoLocationResponse> response) {
                Log.d("retrocall","retrocall : success : "+response.body());
               // List<GetGeoLocationResponse> getGeoLocationResponseList = response.body();
                List<Geofence> geofenceList = response.body().getGeofence();
                Gson gson = new Gson();
                String json = gson.toJson(geofenceList);
                Log.i("jsonLocation",json);
                appSharedpreferences.saveString("geoLocation",json);
                Intent in = new Intent(RetrofitCall.this, MapsActivity.class);
                startActivity(in);

            }

            @Override
            public void onFailure(Call<GetGeoLocationResponse> call, Throwable t) {

            }
        });

    }

}
