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 com.ccwangluo.im.util.ToastUtil;

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<MessageContent> privateMsgList = new MutableLiveData<>();
    public MutableLiveData<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 server, String ip, int port) {
        Long id = Long.parseLong(userId);
        String extData = "extData";
        Long ts = System.currentTimeMillis();
        String data = appKey + "&" + id + "&" + username + "&" + server  + "&" + 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,server, new OnConnectListener() {
            @Override
            public void onSocketConnection() {
                System.out.println("onSocketConnection");
                loginState.postValue(true);
            }

            @Override
            public void onSocketDisconnection(int code) {
                ToastUtil.show(context,"onSocketDisconnection + " + code);
                //重连
//                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) {
        privateMsgList.postValue(res);
    }

    public void addPrivateMsg(List<MessageContent> res) {
        for (MessageContent re : res) {
            privateMsgList.postValue(re);
        }
    }

    public void addChannelMsg(MessageContent res) {
        channelMsgList.postValue(res);
    }

    public void addChannelMsg(List<MessageContent> res) {
        for (MessageContent re : res) {
            channelMsgList.postValue(re);
        }
    }
}