/*************************************************************
 * By: Coralys Cubero Rivera
 * Date: 2019
 *************************************************************/

package uprrp.tania;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Debug;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.Executor;

import javax.net.ssl.HttpsURLConnection;

public class FetchAssessment extends AsyncTask <URL, Object, JSONObject> {

    private StringBuilder data =  new StringBuilder();
    private String TAG = "FETCH ASSESSMENT";

    private URLEventListener myCallBack;
    private Context myContext;
    private Exception myException;
    private String device_token;

    private String resp;
    ProgressDialog progressDialog;

    FetchAssessment(Context context, URLEventListener callback) {
        myCallBack = callback;
        myContext = context;
    }

//    @Override
//    protected void onPreExecute() {
//        RelativeLayout layout = findViewById(this);
//
//    }
@Override
protected void onPreExecute() {
    progressDialog = ProgressDialog.show(myContext,
            "ProgressDialog",
            "Looking for questionnaire");
}


    @Override
    protected JSONObject doInBackground(URL... urls) {

        try
        {
 //we received the url in urls and pass it to  httpURLConnection
             URL url=urls[0];
            Log.d(TAG, "url:" + url.toString());

            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
            InputStream inputStream = httpsURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";

            while (line != null) {
                line = bufferedReader.readLine();
                data.append(line);
            }

            Log.d(TAG, data.toString().substring(0, 10));
            if (data.toString().substring(0, 10).equals("Error:NoHa")) return new JSONObject("{Error:NoHay}");
            else return new JSONObject(data.toString());


        } catch (IOException | JSONException e) {
            e.printStackTrace();
//            Log.d(TAG, "token5:" + device_token);

        }

        return null;
    }

    @Override
    protected void onPostExecute(JSONObject o) {

        if(myCallBack != null){
            if (myException == null){
                if (o != null) {
                    try {
//                        String se = o.getString("Error");
//                        Log.d(TAG, "error:" + o.getString("Error"));
//                        if (o.getString("Error").equals("NoHay")){
                          if (o.has("Error")){
//                            Log.d(TAG, "error:" + o.toString());
                            Toast.makeText(myContext, "There are no surveys at the moment.",Toast.LENGTH_LONG).show();
                        }
                        else {
                            myCallBack.onSuccess(o.toString());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            else{
                myCallBack.onFailure(myException);
            }
        }
        progressDialog.dismiss();

    }

}


