package net.openmob.mobileimsdk.android;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.renrenliao.im.bean.LoginExtra;
import com.renrenliao.im.utils.DataCacheUtils;
import com.renrenliao.im.utils.GsonUtils;

import net.openmob.mobileimsdk.android.core.AutoReLoginDaemon;
import net.openmob.mobileimsdk.android.core.KeepAliveDaemon;
import net.openmob.mobileimsdk.android.core.LocalDataReciever;
import net.openmob.mobileimsdk.android.core.LocalSocketProvider;
import net.openmob.mobileimsdk.android.core.QoS4ReciveDaemon;
import net.openmob.mobileimsdk.android.core.QoS4SendDaemon;
import net.openmob.mobileimsdk.android.event.IMEvent;
//import net.openmob.mobileimsdk.android.event.ChatBaseEvent;
//import net.openmob.mobileimsdk.android.event.ChatTransDataEvent;
//import net.openmob.mobileimsdk.android.event.ChatQoSEvent;


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

    public static boolean DEBUG = true;

    public static boolean autoReLogin = true;

    private static ClientCoreSDK instance = null;

    private boolean _init = false;

    private boolean connectedToServer = true;

    private boolean loginHasInit = false;

    private boolean localDeviceNetworkOk = true;

    private String currentLoginUserId = null;
    private String currentLoginToken = null;
    private String currentLoginExtra = null;

    private IMEvent imEvent;
//    private ChatTransDataEvent chatTransDataEvent = null;
//    private ChatQoSEvent chatQoSEvent = null;
    public static final int IM_STATE_OFFLINE = 0;//0离线 1在线 2 登录中
    public static final int IM_STATE_ONLINE = 1;
    public static final int IM_STATE_CONNECTING = 2;

    private Context context = null;

    private final BroadcastReceiver networkConnectionStatusBroadcastReceiver;

    public static ClientCoreSDK getInstance() {
        if (instance == null)
            synchronized (ClientCoreSDK.class) {
                if (instance == null) {
                    instance = new ClientCoreSDK();
                }
            }
        return instance;
    }

    public void init(Context _context) {
        if (!this._init) {
            if (_context == null)
                throw new IllegalArgumentException("context can't be null!");
            if (_context instanceof Application) {
                this.context = _context;
            } else {
                this.context = _context.getApplicationContext();
            }
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            this.context.registerReceiver(this.networkConnectionStatusBroadcastReceiver, intentFilter);
            AutoReLoginDaemon.getInstance();
            KeepAliveDaemon.getInstance();
            LocalDataReciever.getInstance();
            QoS4ReciveDaemon.getInstance();
            QoS4SendDaemon.getInstance();
            this._init = true;
        }
    }

    public void release() {
        LocalSocketProvider.getInstance().closeLocalSocket();
        AutoReLoginDaemon.getInstance().stop();
        QoS4SendDaemon.getInstance().stop();
        KeepAliveDaemon.getInstance().stop();
        LocalDataReciever.getInstance().stop();
        QoS4ReciveDaemon.getInstance().stop();
        QoS4SendDaemon.getInstance().clear();
        QoS4ReciveDaemon.getInstance().clear();
        try {
            this.context.unregisterReceiver(this.networkConnectionStatusBroadcastReceiver);
        } catch (Exception e) {
            Log.i(TAG, "还未注册android网络事件广播的监听器，本次取消注册已被正常忽略哦.");
        }
        this._init = false;
        setLoginHasInit(false);
        setConnectedToServer(false);
    }

    public String getCurrentLoginUserId() {
         currentLoginUserId = DataCacheUtils.INSTANCE.getUid();
         return currentLoginUserId;
    }

    public String getCurrentLoginToken() {
        currentLoginToken = DataCacheUtils.INSTANCE.getToken();
        return currentLoginToken;
    }

    public String getCurrentLoginExtra() {
        LoginExtra loginExtra = new LoginExtra(DataCacheUtils.INSTANCE.getToken());
        currentLoginExtra = GsonUtils.object2JsonStr(loginExtra);
        return currentLoginExtra;
    }

    public boolean isLoginHasInit() {
        return this.loginHasInit;
    }

    public void setLoginHasInit(boolean loginHasInit) {
        this.loginHasInit = loginHasInit;
    }

    public boolean isConnectedToServer() {
        return this.connectedToServer;
    }

    public void setConnectedToServer(boolean connectedToServer) {
        this.connectedToServer = connectedToServer;
    }

    public boolean isInitialed() {
        return this._init;
    }

    public boolean isLocalDeviceNetworkOk() {
        return this.localDeviceNetworkOk;
    }

    public void setImEvent(IMEvent imEvent){
        this.imEvent = imEvent;
    }
    public IMEvent getImEvent(){
        return this.imEvent;
    }

//    public void setChatBaseEvent(ChatBaseEvent chatBaseEvent) {
//        this.chatBaseEvent = chatBaseEvent;
//    }
//
//    public ChatBaseEvent getChatBaseEvent() {
//        return this.chatBaseEvent;
//    }
//
//    public void setChatTransDataEvent(ChatTransDataEvent chatTransDataEvent) {
//        this.chatTransDataEvent = chatTransDataEvent;
//    }
//
//    public ChatTransDataEvent getChatTransDataEvent() {
//        return this.chatTransDataEvent;
//    }
//
//
//    public void setMessageQoSEvent(ChatQoSEvent messageQoSEvent) {
//        this.chatQoSEvent = messageQoSEvent;
//    }
//
//    public ChatQoSEvent getMessageQoSEvent() {
//        return this.chatQoSEvent;
//    }

    private ClientCoreSDK() {
        this.networkConnectionStatusBroadcastReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                NetworkInfo ethernetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
                if ((mobNetInfo == null || !mobNetInfo.isConnected()) && (wifiNetInfo == null ||
                        !wifiNetInfo.isConnected()) && (ethernetInfo == null ||
                        !ethernetInfo.isConnected())) {
                    Log.e(ClientCoreSDK.TAG, "【IMCORE】【本地网络通知】检测本地网络连接断开了!");
                    ClientCoreSDK.this.localDeviceNetworkOk = false;
                } else {
                    Log.e(ClientCoreSDK.TAG, "【IMCORE】【本地网络通知】检测本地网络已连接上了!");
                    ClientCoreSDK.this.localDeviceNetworkOk = true;
                }
                LocalSocketProvider.getInstance().closeLocalSocket();
            }
        };
    }
}
