package com.yidianling.im.session.extension;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachmentParser;

/**
 * Created by zhoujianghua on 2015/4/9.
 */
public class CustomAttachParser implements MsgAttachmentParser {

    private static final String KEY_TYPE = "type";
    private static final String KEY_DATA = "data";

    @Override
    public MsgAttachment parse(String json) {
        CustomAttachment attachment = null;
        try {
            JSONObject object = JSON.parseObject(json);
            int type = object.getInteger(KEY_TYPE);
            JSONObject data = object.getJSONObject(KEY_DATA);
            attachment = new CustomAttachmentFilter();

            switch (type) {
                //接诊通知
                case CustomAttachmentType.TYPE_INQUIRY_RECEIVE:
                    attachment = new CustomAttachmentInquiryReceive();
                    break;
                case CustomAttachmentType.TYPE_INQUIRY_START:
                    attachment = new CustomAttachmentInquiryStart();
                    break;
                case CustomAttachmentType.TYPE_INQUIRY_END:
                    attachment = new CustomAttachmentInquiryEnd();
                    break;
                case CustomAttachmentType.TYPE_RECIPE:
                    attachment = new CustomAttachmentRecipel();
                    break;
                case CustomAttachmentType.TYPE_PATIENT_FULL_INFO:
                    attachment = new CustomAttachmentPatientFullInfo();
                    break;
                default:
//                    attachment = new CustomAttachmentFilter();
                    break;
            }

            if (attachment != null) {
                attachment.fromJson(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return attachment;
    }

    public static String packData(int type, JSONObject data) {
        JSONObject object = new JSONObject();
        object.put(KEY_TYPE, type);
        if (data != null) {
            object.put(KEY_DATA, data);
        }

        return object.toJSONString();
    }
}
