package com.dbs.mplus.fatima.consent.model;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Field;

/**
 * Created by dbs on 2016-10-14.
 */

public class SavedConsent {

    private String dataXml;

    public String IO_roomNo, IO_ADdate, IO_Dx, IO_dm, IO_signtime, O_drsign, IO_hx, IO_heart,
            IO_respiration, IO_sex_age_y_m, O_Ptsign, IO_drug, IO_OPdr, O_JuminNo, IO_bp,
            IO_formname, IO_INPUTNM, O_Pt_rel, O_Ptname_write, O_Drname_write, IO_status_etc,
            IO_OPdept, IO_idio, IO_nacrotics,IO_kidney, IO_JuminNo, O_sign_cause, IO_hemorrhage,
            IO_smoking, IO_Pt_ID, IO_airway, IO_device, IO_Dept, IO_signdate, IO_Pt_name, IO_allergy,
            IO_printtime, O_device_ID, IO_ocr_cd, IO_maindr;

    public void parse(String xml) {
        this.dataXml = xml;
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();
            parser.setInput(new StringReader(xml));
            int eventType = parser.getEventType();
            while(eventType != XmlPullParser.END_DOCUMENT) {
                switch(eventType) {
                    case XmlPullParser.START_TAG:
                        // 태그명 가져오기
                        String startTag = parser.getName();
                        Class clz = this.getClass();
                        try {
                            // 태그명 필드 찾아서 값 설정
                            clz.getField(startTag).set(this, parser.nextText());
                        } catch (NoSuchFieldException e) {
                            // 없는필드 무시
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                            // 발생할일이 없음
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        String endTag = parser.getName();
                        break;
                }
                // 다음 이벤트 가져옴
                eventType = parser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "SavedConsent{" +
                "IO_roomNo='" + IO_roomNo + '\'' +
                ", IO_ADdate='" + IO_ADdate + '\'' +
                ", IO_Dx='" + IO_Dx + '\'' +
                ", IO_dm='" + IO_dm + '\'' +
                ", IO_signtime='" + IO_signtime + '\'' +
                ", O_drsign='" + O_drsign + '\'' +
                ", IO_hx='" + IO_hx + '\'' +
                ", IO_heart='" + IO_heart + '\'' +
                ", IO_respiration='" + IO_respiration + '\'' +
                ", IO_sex_age_y_m='" + IO_sex_age_y_m + '\'' +
                ", O_Ptsign='" + O_Ptsign + '\'' +
                ", IO_drug='" + IO_drug + '\'' +
                ", IO_OPdr='" + IO_OPdr + '\'' +
                ", O_JuminNo='" + O_JuminNo + '\'' +
                ", IO_bp='" + IO_bp + '\'' +
                ", IO_formname='" + IO_formname + '\'' +
                ", IO_INPUTNM='" + IO_INPUTNM + '\'' +
                ", O_Pt_rel='" + O_Pt_rel + '\'' +
                ", O_Ptname_write='" + O_Ptname_write + '\'' +
                ", O_Drname_write='" + O_Drname_write + '\'' +
                ", IO_status_etc='" + IO_status_etc + '\'' +
                ", IO_OPdept='" + IO_OPdept + '\'' +
                ", IO_idio='" + IO_idio + '\'' +
                ", IO_nacrotics='" + IO_nacrotics + '\'' +
                ", IO_kidney='" + IO_kidney + '\'' +
                ", IO_JuminNo='" + IO_JuminNo + '\'' +
                ", O_sign_cause='" + O_sign_cause + '\'' +
                ", IO_hemorrhage='" + IO_hemorrhage + '\'' +
                ", IO_smoking='" + IO_smoking + '\'' +
                ", IO_Pt_ID='" + IO_Pt_ID + '\'' +
                ", IO_airway='" + IO_airway + '\'' +
                ", IO_device='" + IO_device + '\'' +
                ", IO_Dept='" + IO_Dept + '\'' +
                ", IO_signdate='" + IO_signdate + '\'' +
                ", IO_Pt_name='" + IO_Pt_name + '\'' +
                ", IO_allergy='" + IO_allergy + '\'' +
                ", IO_printtime='" + IO_printtime + '\'' +
                ", O_device_ID='" + O_device_ID + '\'' +
                ", IO_ocr_cd='" + IO_ocr_cd + '\'' +
                ", IO_maindr='" + IO_maindr + '\'' +
                '}';
    }

    public String getDataXml() { return this.dataXml; }
}
