package com.ccwangluo.im.data;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.ccwangluo.ccim.CCIMClient;
import com.ccwangluo.ccim.listener.OnConnectListener;
import com.ccwangluo.ccim.listener.ResultEventCallback;
import com.ccwangluo.ccim.modle.Contact;
import com.ccwangluo.ccim.modle.ContactGroup;
import com.ccwangluo.ccim.modle.Dialog;
import com.ccwangluo.ccim.modle.MessageContent;
import com.ccwangluo.ccim.modle.PullContactsRes;
import com.ccwangluo.ccim.util.HmacUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class LoginViewModel extends ViewModel {

    public MutableLiveData<Boolean> loginState = new MutableLiveData<>();
    public MutableLiveData<Collection<Contact>> contactList = new MutableLiveData<>();
    public MutableLiveData<Collection<ContactGroup>> contactGroupList = new MutableLiveData<>();
    public MutableLiveData<Collection<Dialog>> dialogList = new MutableLiveData<>();
    public MutableLiveData<List<MessageContent>> privateMsgList = new MutableLiveData<>();
    public MutableLiveData<List<MessageContent>> channelMsgList = new MutableLiveData<>();

    public MutableLiveData<List<Long>> channelList = new MutableLiveData<>();

    public void login(Context context, String appKey, String appsecret, String username, String userId, String ip, int port) {
        Long id = Long.parseLong(userId);
        String extData = "extData";
        Long ts = System.currentTimeMillis();
        String data = appKey + "&" + id + "&" + username + "&" + extData + "&" + ts +
                "&" + appsecret;
        //sign计算在服务器,本地不要暴露appsecrect
        String sign = HmacUtil.stringToSign(appsecret, data);
        CCIMClient.getInstance().initServer(ip, port);
        CCIMClient.getInstance().initSDK(context, appKey, username, extData, id, ts, sign, new OnConnectListener() {
            @Override
            public void onSocketConnection() {
                loginState.postValue(true);
            }

            @Override
            public void onSocketDisconnection() {
                //重连
                login(context, appKey, appsecret, username, userId, ip, port);
            }
        });
    }

    public void getContactList() {
        CCIMClient.getInstance().getContactList(false, new ResultEventCallback<PullContactsRes>() {
            @Override
            public void onSuccess(PullContactsRes pullContactsRes) {
                contactList.postValue(pullContactsRes.getContacts());
                contactGroupList.postValue(pullContactsRes.getContactGroups());
                dialogList.postValue(pullContactsRes.getDialogs());
            }

            @Override
            public void onFailed(int errorCode) {

            }
        });
    }

    public void addChannelId(long id) {
        List<Long> value = channelList.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        value.add(id);
        channelList.postValue(value);

    }

    public void removeChannelId(long id) {
        List<Long> value = channelList.getValue();
        if (value == null) {
            return;
        }
        value.remove(id);
        channelList.postValue(value);
    }

    public void addPrivateMsg(MessageContent res) {
        List<MessageContent> value = privateMsgList.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        value.add(res);
        privateMsgList.postValue(value);
    }

    public void addPrivateMsg(List<MessageContent> res) {
        List<MessageContent> value = privateMsgList.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        for (MessageContent re : res) {
            value.add(re);
        }
        privateMsgList.postValue(value);
    }

    public void addChannelMsg(MessageContent res) {
        List<MessageContent> value = channelMsgList.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        value.add(res);
        channelMsgList.postValue(value);
    }

    public void addChannelMsg(List<MessageContent> res) {
        List<MessageContent> value = channelMsgList.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        for (MessageContent re : res) {
            value.add(re);
        }
        channelMsgList.postValue(value);
    }
}