package net.openmob.mobileimsdk.android.core;


import android.util.Log;

import com.renrenliao.im.im.ImMsgBean;
import com.renrenliao.im.im.ImMsgFactory;

import net.openmob.mobileimsdk.android.ClientCoreSDK;
import net.openmob.mobileimsdk.android.conf.IMConfigEntity;
import net.openmob.mobileimsdk.android.utils.MBAsyncTask;
import net.openmob.mobileimsdk.android.utils.UDPUtils;

import java.net.DatagramSocket;
import java.net.InetAddress;


public class LocalDataSender {
    private static final String TAG = LocalDataSender.class.getSimpleName();

    private static final class InstanceHolder {
        static final LocalDataSender instance = new LocalDataSender();
    }
    public static LocalDataSender getInstance() {
        return InstanceHolder.instance;
    }

    public int sendLogin() {
        Log.i("sendLogin","sendLogin");

        byte[] b = ImMsgFactory.createLoginMessage().toBytes();
        int code = send(b, b.length);
        Log.i(TAG,"sendLogin:"+code);
        if (code == 0){
            Log.i(TAG,"Im登陆成功:"+code);
        }

        return code;
    }

    public int sendLogout() {
        int code = -1;
        if (ClientCoreSDK.getInstance().isLoginHasInit()) {
            byte[] b = ImMsgFactory.createPLogoutInfo(ClientCoreSDK.getInstance().getCurrentLoginUserId()).toBytes();
            code = send(b, b.length);
            Log.i(TAG,"Im退出登陆:"+code);
        }
       // ClientCoreSDK.getInstance().release();
        return code;
    }

    int sendKeepAlive() {
        byte[] b = ImMsgFactory.createPKeepAlive(ClientCoreSDK.getInstance().getCurrentLoginUserId()).toBytes();
        return send(b, b.length);
    }


//    public int sendCommonData(String dataContentWidthStr, String to_user_id, String fingerPrint, int typeu) {
//        return sendCommonData(dataContentWidthStr, to_user_id, true, fingerPrint, typeu);
//    }
//
//    public int sendCommonData(String dataContentWidthStr, String to_user_id, boolean QoS, String fingerPrint, int typeu) {
//        return sendCommonData(ImMsgFactory.createCommonData(dataContentWidthStr,
//                ClientCoreSDK.getInstance().getCurrentLoginUserId(), to_user_id, QoS, fingerPrint, typeu));
//    }

    public int sendCommonData(ImMsgBean p) {
        if (p != null) {
            byte[] b = p.toBytes();
            int code = this.send(b, b.length);
//            if (code == 0 && p.isQoS() && !QoS4SendDaemon.getInstance(this.context).exist(p.getFp())) {
            if (!QoS4SendDaemon.getInstance().exist(p.getFp())) {
                QoS4SendDaemon.getInstance().put(p);
            }

            return code;
        } else {
            return 4;
        }
    }

    private int send(byte[] fullProtocalBytes, int dataLen) {
        if (!ClientCoreSDK.getInstance().isInitialed()) {
            return 203;
        } else if (!ClientCoreSDK.getInstance().isLocalDeviceNetworkOk()) {
            Log.i(TAG, "【IMCORE】本地网络不能工作，send数据没有继续!");
            return 204;
        }  else {
            DatagramSocket ds = LocalSocketProvider.getInstance().getLocalSocket();
            if (ds != null && !ds.isConnected()) {
                try {
                    if (IMConfigEntity.serverIP == null) {
                        Log.i(TAG, "【IMCORE】send数据没有继续，原因是ConfigEntity.server_ip==null!");
                        return 205;
                    }
                    ds.connect(InetAddress.getByName(IMConfigEntity.serverIP), IMConfigEntity.serverPort);
                    Log.i("onCheckedChanged", IMConfigEntity.serverIP + "");
                } catch (Exception var5) {
                    Log.i(TAG, "【IMCORE】send时出错，原因是：" + var5.getMessage(), var5);
                    return 202;
                }
            }
            return UDPUtils.send(ds, fullProtocalBytes, dataLen) ? 0 : 3;
        }
    }

    public static abstract class SendCommonDataAsync extends MBAsyncTask {

        protected ImMsgBean p;

        public SendCommonDataAsync( ImMsgBean p) {

            this.p = null;
            if (p == null) {
                Log.i(TAG, "【IMCORE】无效的参数p==null!");
            } else {
                this.p = p;
            }
        }

        protected Integer doInBackground(Object... params) {
            if (this.p != null)
                return LocalDataSender.getInstance().sendCommonData(p);
            return -1;
        }

        protected abstract void onPostExecute(Integer param1Integer);
    }

}
