package com.renitha.Util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.renitha.Volley.VolleySingleton;

import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

/**
 * Created by TEJ on 01-Sep-16.
 */
public class Json {
    private static Dialog mDialog;
    public static ProgressDialog mProgressDialog;

    public static void getResponse(Context context, String url, final Handler handler){

       /* Log.d("test", "Url=====>" + url);
        mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setTitle("Loading..");
        mProgressDialog.show();
*/
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url,null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                Log.d("test","Response:"+response.toString());
                Message message = new Message();
                message.obj = response;
                handler.sendMessage(message);
                if(mProgressDialog != null){
                    mProgressDialog.dismiss();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("test","error:"+error);
                if(mProgressDialog != null){
                    mProgressDialog.dismiss();
                }
            }
        });

        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
                (int) TimeUnit.SECONDS.toMillis(35),
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        VolleySingleton.getInstance(context).getRequestQueue().add(jsonObjectRequest);
    }

    public final static void showDialogMessage(Context context, String msg){

        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(msg)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // FIRE ZE MISSILES!
                        dialog.dismiss();
                    }
                });

        AlertDialog dialog = builder.create();
        dialog.show();


    }
}
