package com.rayantec.reservation.reservationdemo.Model;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.DefaultRetryPolicy;
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.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.rayantec.reservation.reservationdemo.Adapter.AttractionAdapter;
import com.rayantec.reservation.reservationdemo.G;
import com.rayantec.reservation.reservationdemo.Lists.AttractionList;
import com.rayantec.reservation.reservationdemo.Lists.CityList;
import com.rayantec.reservation.reservationdemo.R;
import com.toptoche.searchablespinnerlibrary.SearchableSpinner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class Attractions extends Fragment {
RecyclerView recyclerView;
ArrayList<AttractionList> AttractionData = new ArrayList<>();
public SearchableSpinner spinnerCitiest;
ArrayList<CityList> cityLists = new ArrayList<>();
ProgressDialog progressDialog;
public void showCity(View view) {
spinnerCitiest = (SearchableSpinner) view.findViewById(R.id.SpinnerCitiestId);
spinnerCitiest.setTitle("Please Select City");
spinnerCitiest.setPositiveButton("Ok");
//spinnerCitiest.setSelection(2);
JsonArrayRequest JsonObjectRequest = new JsonArrayRequest(Request.Method.GET, G.serverURL + "/android/jsyncs/getdata/city", null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//progressDialog.dismiss();
try {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonOBJ = response.getJSONObject(i);
String id = jsonOBJ.getString("id");
String title = jsonOBJ.getString("title");
cityLists.add(new CityList(id, title));
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//progressDialog.dismiss();
Toast.makeText(G.context, "Server Connection error", Toast.LENGTH_SHORT).show();
}
});
JsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(7000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(G.context);
requestQueue.add(JsonObjectRequest);
ArrayAdapter<CityList> adapterCities = new ArrayAdapter<CityList>(G.context, android.R.layout.simple_spinner_item, cityLists);
adapterCities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCitiest.setAdapter(adapterCities);
spinnerCitiest.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
CityList cityList = (CityList) adapterView.getSelectedItem();
Toast.makeText(G.context, "City ID: " + cityList.getId() + ", City Name : " + cityList.getTitle(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void showRandomAttractions(View view) {
recyclerView = (RecyclerView) view.findViewById(R.id.RecyclerAttracionId);
JsonArrayRequest JsonObjectRequests = new JsonArrayRequest(Request.Method.GET, G.serverURL + "/android/jsyncs/getdata/randattraction", null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
progressDialog.dismiss();
//AttractionList sampledata = new AttractionList();
try {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonOBJs = response.getJSONObject(i);
String id = jsonOBJs.getString("id");
String title = jsonOBJs.getString("title");
String imageUrl = jsonOBJs.getString("imageUrl");
String location = jsonOBJs.getString("location");
String type = jsonOBJs.getString("type");
String body = jsonOBJs.getString("body");
AttractionData.add(new AttractionList(id, title, body, location, imageUrl, type));
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(G.context, "Server Connection error", Toast.LENGTH_SHORT).show();
}
});
JsonObjectRequests.setRetryPolicy(new DefaultRetryPolicy(7000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(G.context);
requestQueue.add(JsonObjectRequests);
recyclerView.setLayoutManager(new LinearLayoutManager(G.context, LinearLayoutManager.VERTICAL, false));
AttractionAdapter attractionAdapter = new AttractionAdapter(AttractionData);
recyclerView.setAdapter(attractionAdapter);
}
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.attraction, container, false);
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Please wait ...");
progressDialog.setCancelable(false);
progressDialog.show();
showCity(view);
showRandomAttractions(view);
return view;
}
}