package com.dbs.consent.demo.httpTask;

import android.os.AsyncTask;
import android.util.Log;

import com.dbs.consent.demo.BuildConfig;
import com.dbs.consent.demo.util.ConsentConfig;
import com.dbs.consent.demo.util.Util;

import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HttpSoapConnection extends AsyncTask<Void, Void, SoapObject> {
  
  private static final String TAG = "HttpSoapConnection";
  private HashMap<String, String> mData;
  private String soapAction  = "";
  private String callMethod = "";
  
  public HttpSoapConnection(String soapAction, String callMethod, HashMap<String, String> mData) {
    this.soapAction  = soapAction;
    this.callMethod = callMethod;
    this.mData       = mData;
  }
  
  @Override
  protected void onPreExecute() {
  }
  
  @Override
  protected SoapObject doInBackground(Void... voids) {

    SoapObject soapObject  = soapRequestParam(callMethod, mData);
    SoapObject soapResponse = null;

    try {

      SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
      soapEnvelope.bodyOut = soapObject;
      soapEnvelope.setOutputSoapObject(soapObject);
      soapEnvelope.dotNet = true;
      soapEnvelope.implicitTypes = false;

      List<HeaderProperty> headers=new ArrayList<HeaderProperty>();
      HeaderProperty headerProperty=new HeaderProperty("Accept-Encoding", "none");
      headers.add(headerProperty);

      Log.e(TAG, "soapAction -- > " + soapAction);
      Log.e(TAG, "callMmethod -- > " + callMethod);

      HttpTransportSE hts = new HttpTransportSE(soapAction, ConsentConfig.CONNECT_TIMEOUT);
      Log.e(TAG, "http reuqest " + callMethod + " start -- > " + Util.logTime());
      hts.debug = true;
      hts.call(BuildConfig.NAME_SPACE + callMethod, soapEnvelope, headers);

      if (soapEnvelope.getResponse() instanceof SoapObject) {
        soapResponse = (SoapObject) soapEnvelope.getResponse();
      } else {
        soapResponse = new SoapObject();
        soapResponse.addProperty(callMethod, soapEnvelope.getResponse());
      }
      Log.e(TAG, "http reuqest " + callMethod + " end -- > " + Util.logTime());

      if (soapResponse != null) {
        return soapResponse;
      } else {
//        errorObject.addProperty("ERROR", "ERROR");
      }

    } catch (IOException e) {
      e.printStackTrace();
      Log.e(TAG, "서버에서 요청을 처리할 수 없습니다 -- > " + callMethod);
    } catch (XmlPullParserException e) {
      e.printStackTrace();
    }

    return null;
  }
  
  @Override
  protected void onPostExecute(SoapObject o) {
//    super.onPostExecute(o);
  }
  
  private SoapObject soapRequestParam(String callMmethod, HashMap<String, String> hashMap) {
    SoapObject soapObject = new SoapObject(BuildConfig.NAME_SPACE, callMmethod);

    for (String key : hashMap.keySet()) {
      soapObject.addProperty(key, hashMap.get(key));
    }
    
    return soapObject;
  }
}
