package com.project.ilsaenterprise.monitoring;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.project.ilsaenterprise.R;
import com.project.ilsaenterprise.adapter.BeaconAdapter;
import com.project.ilsaenterprise.models.Beacon;
import com.project.ilsaenterprise.models.Channel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ShowData extends AppCompatActivity {
    private TextView mTextView;
    ListView listView;
    List<Beacon> listData;
    private static final String JSON_URL = "https://api.thingspeak.com/channels/1603955/feeds.json?api_key=ND0HG1CVKUENHMZQ&results=8000";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getSupportActionBar() != null)
            getSupportActionBar().hide();
        setContentView(R.layout.activity_show_data);

        listView = findViewById(R.id.listView);
        listData = new ArrayList<>();

        loadData();
    }

    private void loadData(){

        StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL, new Response.Listener<String>(){
            @Override
            public void onResponse(String response){

                try {
                    JSONObject obj = new JSONObject(response);
                    JSONArray sensorArray = obj.getJSONArray("feeds");

                    for (int i = sensorArray.length() - 1; i >= 0; i--) {
                        JSONObject arrayObject = sensorArray.getJSONObject(i);

                        Beacon beaconData = new Beacon(arrayObject.getString("created_at"), arrayObject.getString("entry_id"), arrayObject.getString("field1"), arrayObject.getString("field2"), arrayObject.getString("field3"),
                                arrayObject.getString("field4"), arrayObject.getString("field5"), arrayObject.getString("field6"), arrayObject.getString("field7"), arrayObject.getString("field8"));

                        listData.add(beaconData);
                    }

                    BeaconAdapter adapter = new BeaconAdapter(listData, getApplicationContext());
                    listView.setAdapter(adapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //displaying the error in toast if occur
                        Toast.makeText(getBaseContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(getBaseContext());

        //adding the string request to request queue
        requestQueue.add(stringRequest);
    }
}