package com.dbs.mplus.knuh.httpTask;

import android.util.Log;

import com.dbs.mplus.knuh.model.CategoryVO;
import com.dbs.mplus.knuh.model.DeptListVO;
import com.dbs.mplus.knuh.model.DoctorListVO;
import com.dbs.mplus.knuh.model.PatientListVO;
import com.dbs.mplus.knuh.model.UserLoginDeptList;
import com.dbs.mplus.knuh.util.ConsentConfig;

import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;

public class SoapParser {
  private static final String TAG = "SoapParser";

  public static ArrayList<UserLoginDeptList> userLoginDeptList(SoapObject object) {
    ArrayList<UserLoginDeptList> arrayList = new ArrayList<>();
    int total = 0;
    if (object != null) {
      total = object.getPropertyCount();
      if (total == 1) {
        if (object.getProperty(0) == null) {
          total = 0;
        }
      }
    }

    if (total == 0) {
    } else {
      for (int i = 0; i < total; i++) {
        SoapObject so = (SoapObject) object.getProperty(i);
        String dutplceInstCd = so.getProperty("dutplceInstCd").toString().trim();
        String dutplceInstNm = so.getProperty("dutplceInstNm").toString().trim();
        String dutplceCd     = so.getProperty("dutplceCd").toString().trim();
        String deptHngNm     = so.getProperty("deptHngNm").toString().trim();
        String fromDd        = so.getProperty("fromDd").toString().trim();
        String sortNo        = so.getProperty("sortNo").toString().trim();
        String deptCd        = so.getProperty("deptCd").toString().trim();
        String jobKindCd     = so.getProperty("jobKindCd").toString().trim();
        String jobKindNm     = so.getProperty("jobKindNm").toString().trim();
        String indexPage     = so.getProperty("indexPage").toString().trim();
        String userId        = so.getProperty("userId").toString().trim();
        String userName      = so.getProperty("userName").toString().trim();
        String drKind        = so.getProperty("drKind").toString().trim();

        dutplceInstCd = dutplceInstCd.equals(ConsentConfig.RETURN_NULL) ? "" : dutplceInstCd;
        dutplceInstNm = dutplceInstNm.equals(ConsentConfig.RETURN_NULL) ? "" : dutplceInstNm;
        dutplceCd     = dutplceCd.equals(ConsentConfig.RETURN_NULL) ? "" : dutplceCd;
        deptHngNm     = deptHngNm.equals(ConsentConfig.RETURN_NULL) ? "" : deptHngNm;
        fromDd        = fromDd.equals(ConsentConfig.RETURN_NULL) ? "" : fromDd;
        sortNo        = sortNo.equals(ConsentConfig.RETURN_NULL) ? "" : sortNo;
        deptCd        = deptCd.equals(ConsentConfig.RETURN_NULL) ? "" : deptCd;
        jobKindCd     = jobKindCd.equals(ConsentConfig.RETURN_NULL) ? "" : jobKindCd;
        jobKindNm     = jobKindNm.equals(ConsentConfig.RETURN_NULL) ? "" : jobKindNm;
        indexPage     = indexPage.equals(ConsentConfig.RETURN_NULL) ? "" : indexPage;
        userId        = userId.equals(ConsentConfig.RETURN_NULL) ? "" : userId;
        userName      = userName.equals(ConsentConfig.RETURN_NULL) ? "" : userName;
        drKind        = drKind.equals(ConsentConfig.RETURN_NULL) ? "" : drKind;

        arrayList.add(new UserLoginDeptList(dutplceInstCd, dutplceInstNm, dutplceCd, deptHngNm, fromDd, sortNo, deptCd, jobKindCd, jobKindNm, indexPage, userId, userName, drKind));
      }
    }

    return arrayList;
  }

  public static ArrayList<DeptListVO> getWardNDeptList(SoapObject object) {
    ArrayList<DeptListVO> arrayList = new ArrayList<DeptListVO>();
    int total = 0;
    if (object != null) {
      total = object.getPropertyCount();
      if (total == 1) {
        if (object.getProperty(0) == null) {
          total = 0;
        }
      }
    }

    if (total == 0) {
    } else {
      for (int i = 0; i < total; i++) {
        SoapObject so = (SoapObject) object.getProperty(i);
        String deptCd = so.getProperty("deptCd").toString().trim();
        String deptNm = so.getProperty("deptNm").toString().trim();

        deptCd = deptCd.equals(ConsentConfig.RETURN_NULL) ? "" : deptCd;
        deptNm = deptNm.equals(ConsentConfig.RETURN_NULL) ? "" : deptNm;

        arrayList.add(new DeptListVO(deptCd, deptNm));
      }
    }

    return arrayList;
  }

  public static ArrayList<DoctorListVO> getDoctorList(SoapObject object) {
    ArrayList<DoctorListVO> arrayList = new ArrayList<DoctorListVO>();
    int total = 0;
    if (object != null) {
      total = object.getPropertyCount();
      if (total == 1) {
        if (object.getProperty(0) == null) {
          total = 0;
        }
      }
    }

    if (total == 0) {
    } else {
      for (int i = 0; i < total; i++) {
        SoapObject so = (SoapObject) object.getProperty(i);
        String doctorId   = so.getProperty("doctorId").toString().trim();
        String doctorNm   = so.getProperty("doctorNm").toString().trim();
        String doctorKind = so.getProperty("doctorKind").toString().trim();
        String drKind     = so.getProperty("drKind").toString().trim();

        arrayList.add(new DoctorListVO(doctorId, doctorNm, doctorKind, drKind));
      }
    }

    return arrayList;
  }

  public static ArrayList<PatientListVO> getPatientList(SoapObject object) {
    ArrayList<PatientListVO> arrayList = new ArrayList<PatientListVO>();

    int total = 0;
    if (object != null) {
      total = object.getPropertyCount();
      if (total == 1) {
        if (object.getProperty(0) == null) {
          total = 0;
        }
      }
    }

    if (total == 0) {
    } else {
      for (int i = 0; i < total; i++) {
        SoapObject so = (SoapObject) object.getProperty(i);
        String pid       = so.getProperty("pid").toString().trim();
        String inDd      = so.getProperty("inDd").toString().trim();
        String cretNo    = so.getProperty("cretNo").toString().trim();
        String instCd    = so.getProperty("instCd").toString().trim();
        String hngNm     = so.getProperty("hngNm").toString().trim();
        String ordDeptCd = so.getProperty("ordDeptCd").toString().trim();
        String doctorId  = so.getProperty("doctorId").toString().trim();
        String sa        = so.getProperty("sa").toString().trim();
        String deptNm    = so.getProperty("deptNm").toString().trim();
        String doctorNm  = so.getProperty("doctorNm").toString().trim();
        String roomCd    = so.getProperty("roomCd").toString().trim();
        String opRoomNm  = so.getProperty("opRoomNm").toString().trim();
        String oprsRvNo  = so.getProperty("oprsRvNo").toString().trim();

        arrayList.add(new PatientListVO(pid, inDd, cretNo, instCd, hngNm, ordDeptCd, doctorId, sa, deptNm, doctorNm, roomCd, opRoomNm, oprsRvNo));
      }
    }

    return arrayList;
  }

  public static ArrayList<CategoryVO> getHospitalList(SoapObject object) {

    ArrayList<CategoryVO> arrayList = new ArrayList<CategoryVO>();

    int total = 0;
    if (object != null) {
      total = object.getPropertyCount();
      if(total == 1) {
        if(object.getProperty(0) == null) {
          total = 0;
        }
      }
    }

    if(total == 0) {
    } else {
      for(int i = 0; i < total; i++) {
        SoapObject so = (SoapObject) object.getProperty(i);

        String categoryType = so.getProperty("CategoryType").toString().trim();
        String categoryId   = so.getProperty("CategoryId").toString().trim();
        String categoryName = so.getProperty("CategoryName").toString().trim();

        arrayList.add(new CategoryVO(categoryType, categoryId, categoryName));

      }
    }

    return arrayList;
  }
}
