package com.renrenliao.im.im;

import android.util.Log;

import com.google.gson.Gson;
import com.renrenliao.im.model.repository.LocalDataSource;
import com.renrenliao.im.utils.CharsetUtils;
import com.renrenliao.im.utils.DataCacheUtils;
import com.renrenliao.im.utils.MDateUtils;

import net.openmob.mobileimsdk.server.s.PErrorResponse;


public class ImMsgFactory {

    private static String create(Object c) {
        return (new Gson()).toJson(c);
    }

    public static <T> T parse(byte[] fullProtocalJASOnBytes, int len, Class<T> clazz) {
        return parse(CharsetUtils.getString(fullProtocalJASOnBytes, len), clazz);
    }

    public static <T> T parse(String dataContentOfProtocal, Class<T> clazz) {
        return (new Gson()).fromJson(dataContentOfProtocal, clazz);
    }

    public static ImMsgBean parse(byte[] fullProtocalJASOnBytes, int len) {
        return (ImMsgBean) parse(fullProtocalJASOnBytes, len, ImMsgBean.class);
    }

    public static ImMsgBean createRecivedBack(String from_user_id, String to_user_id, String recievedMessageFingerPrint) {
        ImMsgBean p = new ImMsgBean();
//        p.setFid(from_user_id);
        p.setTid(to_user_id);
//        p.setBot(false);
        p.setAck(recievedMessageFingerPrint);
        p.setRoute(RouteType.ACK.getValue() + "");

        return p;
    }

//    public static ImMsgBean createCommonData(String dataContent, String from_user_id, String to_user_id, boolean QoS, String fingerPrint, int typeu) {
//        ImMsgBean p = new ImMsgBean();
//        p.setFid(from_user_id);
//        p.setTid(to_user_id);
//        p.setFp(fingerPrint);
//        p.setMsgType(typeu);
//        p.setTxt(dataContent);
//        return p;
//    }

    public static ImMsgBean createLoginMessage() {
        ImMsgBean message = new ImMsgBean();
        message.setFid("" + DataCacheUtils.INSTANCE.getUid());
        message.setDevice("android");
        message.setToken(DataCacheUtils.INSTANCE.getToken());
        message.setTid("0");
        message.setRoute(RouteType.SIGNIN.getValue() + "");
        return message;
    }

    public static ImMsgBean createPLogoutInfo(String user_id) {
        ImMsgBean p = new ImMsgBean();
        p.setFid(user_id);
        p.setTid("0");
        p.setTime(MDateUtils.currentTimMillis());
        //退出登录type
        p.setMsgType(MsgType.LOGOUT.getValue());
        p.setRoute(RouteType.LOGOUT.getValue()+"");
        return p;

    }

    public static ImMsgBean createPKeepAlive(String from_user_id) {
        ImMsgBean p = new ImMsgBean();
        p.setRoute(RouteType.KEEPALIVE.getValue() + "");
        p.setTid("0");
        Log.i("sendKeepAlive", " " + p.toJSONString());
        return p;
    }

    public static PErrorResponse parsePErrorResponse(String dataContentOfProtocal) {
        return (PErrorResponse) parse(dataContentOfProtocal, PErrorResponse.class);
    }

    public static ImMsgBean createTextMessage(String userName, String avatar, String tid, String msg, String fp, long time) {
        ImMsgBean p = new ImMsgBean();
        p.setFid(DataCacheUtils.INSTANCE.getUid());
        p.setName(userName);
        p.setBot(false);
        p.setAvatar(avatar);
        p.setTid(tid);
        p.setRoute(RouteType.NORMAL.getValue() + "");
        p.setTxt(msg);
        p.setFp(fp);
        p.setTime(time);
        p.setMsgType(MsgType.TEXT.getValue());

        return p;
    }

}
