package com.dbs.mplus.knuh.consent;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.dbs.mplus.knuh.BuildConfig;
import com.dbs.mplus.knuh.R;
import com.dbs.mplus.knuh.activity.ConsentActivity;
import com.dbs.mplus.knuh.activity.activityEvent.ImageActivity;
import com.dbs.mplus.knuh.httpTask.CallBack;
import com.dbs.mplus.knuh.httpTask.HttpSoapConnection;
import com.dbs.mplus.knuh.httpTask.SoapParser;
import com.dbs.mplus.knuh.model.ConsentFormListVO;
import com.dbs.mplus.knuh.model.ConsentImageVO;
import com.dbs.mplus.knuh.model.PatientInfoVO;
import com.dbs.mplus.knuh.util.ConsentConfig;
import com.dbs.mplus.knuh.util.Util;

import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

public class ConsentEvent {

  private static final String TAG = "ConsentEvent";
  private Context mContext;
  private ConsentActivity instance;
  private ConsentFormListVO consentFormListVO;

  public ConsentEvent(Context mContext, ConsentActivity instance, ConsentFormListVO consentFormListVO) {
    this.mContext = mContext;
    this.instance = instance;
    this.consentFormListVO = consentFormListVO;
  }

  public void consentOpenProc(final PatientInfoVO patientInfoVO, final String apiType) {
    final String formCd          = consentFormListVO.getFormCd();
    final String userDrFlag      = consentFormListVO.getUserDrFlag();
    final String consentState    = consentFormListVO.getConsentState();
    final String consentStateEng = consentFormListVO.getConsentStateEng();
    final String formPrntNm      = consentFormListVO.getFormPrntNm();
    final String certPass        = consentFormListVO.getCertUseYn();
    final String consentMstRid   = consentFormListVO.getConsentMstRid();
    final String formRid         = consentFormListVO.getFormRid();
    final String guid            = consentFormListVO.getFormGuid();
    String linkFormCd      = consentFormListVO.getLinkFormCd();
    final String ocrTag          = consentFormListVO.getOcrTag();
    String cretNo          = consentFormListVO.getCretNo();
    String jinjeongCd      = consentFormListVO.getJinjeongCd();
    final String linkCd          = jinjeongCd.equals("-") ? linkFormCd : jinjeongCd;

//    Log.e(TAG, "formCd       -- > " + formCd      );
//    Log.e(TAG, "userDrFlag   -- > " + userDrFlag  );
//    Log.e(TAG, "consentState -- > " + consentState);
//    Log.e(TAG, "formPrntNm   -- > " + formPrntNm  );
//    Log.e(TAG, "certPass     -- > " + certPass    );
//    Log.e(TAG, "rid          -- > " + consentMstRid);
//    Log.e(TAG, "guid         -- > " + guid        );
//    Log.e(TAG, "linkFormCd   -- > " + linkFormCd  );
//    Log.e(TAG, "jinjeongCd   -- > " + jinjeongCd  );
//    Log.e(TAG, "linkCd       -- > " + linkCd  );
//    Log.e(TAG, "ocrTag       -- > " + ocrTag      );
//    Log.e(TAG, "formRid      -- > " + formRid     );
//    Log.e(TAG, "cretNo       -- > " + cretNo      );

    if (consentStateEng.equals(ConsentConfig.CERTIFY_CMP) || consentStateEng.equals(ConsentConfig.PAPER_OUT)) {
      if (consentStateEng.equals(ConsentConfig.PAPER_OUT)) {
        instance.showSingButtonDialog(mContext.getString(R.string.printOnlyConsent));
      } else {
        CallBack callBack = new CallBack() {
          @Override
          public void result(final SoapObject result) {
            Log.e(TAG, "result -- > " + result);
            instance.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                ArrayList<ConsentImageVO> arrayList = SoapParser.getConsentImage(result);

                if (arrayList.size() == 0) {
                  stop();
                } else {
                  Intent intent = new Intent(mContext, ImageActivity.class);
                  intent.putExtra("imageArray", arrayList);
                  instance.startActivity(intent);
                }

                instance.dismissProgress();
              }
            });

          }

          @Override
          public void stop() {
            instance.showSingButtonDialog(mContext.getString(R.string.imageLoadFailed));
          }

          @Override
          public void error() {

          }
        };

        instance.showProgress();
        HashMap<String, String> mData = new HashMap<String, String>();
        mData.put("consentMstRid", consentMstRid);

        if (Util.checkNetwork(mContext) == true) {
          Util.callHttp(mContext, ConsentConfig.HOST_CONSENT, ConsentConfig.GET_CONSENT_IMAGE, mData, callBack);
        } else {
          instance.dismissProgress();
          Util.networkErrorAlert(instance);
        }

      }
    } else {

      CallBack callBack = new CallBack() {
        @Override
        public void result(final SoapObject result) {
          Log.e(TAG, "result -- > " + result);
          instance.runOnUiThread(new Runnable() {
            @Override
            public void run() {
              instance.dismissProgress();
              int state = 0;
              if (result == null) {
                state = -1;
              } else {
                state = Integer.parseInt(result.getProperty("responseData").toString().trim());
              }

              if (state == -1) { // 상태조회 실패(-1)
                instance.showSingButtonDialog(mContext.getString(R.string.printOnlyConsent));
              } else if (state == 1) { // 이미작성된 동의서(1)
                instance.showSingButtonDialog(mContext.getString(R.string.writeConsnet));
              } else {
                ConsentCreateParam consentCreateParam = new ConsentCreateParam(mContext, userDrFlag, consentState, consentStateEng);
                HashMap<String, String> globalParam = consentCreateParam.setGlobalParam(patientInfoVO, consentMstRid, consentFormListVO);
                ArrayList<HashMap<String, String>> mapArrayList = consentCreateParam.setFormParam(consentMstRid, formRid, guid, formPrntNm, formCd, linkCd, ocrTag);

                HashMap map = new HashMap();
                map.put("globalParam", globalParam);
                map.put("formListParam", mapArrayList);
                ConsentFacade consentFacade = new ConsentFacade(mContext);
                consentFacade.startConsent(map, certPass, apiType);
              }

            }
          });
        }

        @Override
        public void stop() {

        }

        @Override
        public void error() {

        }
      };

      HashMap<String, String> mData = new HashMap<String, String>();
      mData.put("consentmstrid", consentMstRid);
      mData.put("consent_state", consentStateEng);

      Log.e(TAG, "mData -- > " + mData);

      if (Util.checkNetwork(mContext) == true) {
        instance.showProgress();
        Util.callHttp(mContext, ConsentConfig.HOST_CONSENT, ConsentConfig.CHECK_CONSENT_STATE, mData, callBack);
      } else {
        instance.dismissProgress();
        Util.networkErrorAlert(instance);
      }
    }
  }

}
