package com.kfb.kfbv1.model;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.SyncHttpClient;

import cz.msebera.android.httpclient.Header;

public class KfbKttpRequest2 extends AsyncTask<KfbGate,Void,String> {
    Handler handler = KfbModel.getData().handler;
    KfbParser p = new KfbParser();
    SyncHttpClient client = new SyncHttpClient();

    @Override
    protected String doInBackground(final KfbGate... g) {
        client.get(g[0].getmAdres(), new AsyncHttpResponseHandler() {

            @Override
            public void onStart() {
                // called before request is started
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                g[0].setmError("");
                int i = g[0].getId();
                String outS = new String(response);
                KfbModel.getData().tekser =outS;
                p.dataParser(KfbModel.getData().tekser, KfbModel.getData().getGates().getmGate().get(i));
                KfbModel.getData().getGates().getmGate().get(i).getmMeasure().CreateBitMap();
                Message msg = handler.obtainMessage();
                msg.what = 2;
                msg.obj = "Pomiar";
                msg.arg1 =  KfbModel.getData().getGates().getmGate().get(i).getId();
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
                // called when response HTTP status is "4XX" (eg. 401, 403, 404)
            }

            @Override
            public void onRetry(int retryNo) {
                // called when request is retried
            }
        });
        return "";
    }
}
