package librerias;
import android.util.Log;
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.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;

/**
 * Created by LuisMitre on 30/04/2018.
 */

public class ConexionWeb{
    static JSONObject jObj = null;
    static String json = "";
    BufferedReader reader;
    public ConexionWeb() {    }

    public String buscarDato(String url,String parametros){
        OutputStream os = null;
        InputStream is = null;
        HttpURLConnection conn = null;
        try {
            URL urlEnviar = new URL(url + parametros);
            Log.d("URL:", url + parametros);
            conn = (HttpURLConnection) urlEnviar.openConnection();
            conn.setReadTimeout(15000 /*milliseconds*/);
            conn.setConnectTimeout(30000 /* milliseconds */);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
            conn.connect();
            os = conn.getOutputStream();
            os.flush();
            //do somehting with response
            is = conn.getInputStream();
            // fin del archvio
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            reader.close();
            json = sb.toString();
                jObj = new JSONObject(json);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }
    public void grabarDatos(String url,String parametros){
        OutputStream os = null;
        InputStream is = null;
        HttpURLConnection conn = null;
        try {
            URL urlEnviar = new URL(url + parametros);
            Log.d("URL:", url + parametros);
            conn = (HttpURLConnection) urlEnviar.openConnection();
            conn.setReadTimeout(15000 /*milliseconds*/);
            conn.setConnectTimeout(30000 /* milliseconds */);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
            conn.connect();
            os = conn.getOutputStream();
            os.flush();
            //do somehting with response
            is = conn.getInputStream();
            // fin del archvio
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
           //
            reader.close();
            json = sb.toString();
            Log.d("REspuesta de la WEB",json);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String armarGET(ArrayList<String> keys, ArrayList<String> valor){
        String resultado="?";
        for(int i=0;i<valor.size();i++){
            if(i!=valor.size()-1) {
                resultado = resultado + keys.get(i) + "=" + valor.get(i)+"&";

            }else{
                resultado = resultado + keys.get(i) + "=" + valor.get(i);
            }
        }
        String respuesta=resultado.replace(" ","%20");

        return respuesta;
    }

}