package io.openim.android.ouicontact.vm;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

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

import io.openim.android.ouicontact.R;
import io.openim.android.ouicontact.ui.search.GroupInfoExpand;
import io.openim.android.ouicore.base.BaseApp;
import io.openim.android.ouicore.base.BaseViewModel;
import io.openim.android.sdk.models.GroupInfo;

public class SearchGroup extends BaseViewModel {
    //我加入的群
    public MutableLiveData<List<GroupInfo>> groups = new MutableLiveData<>(new ArrayList<>());
    public MutableLiveData<List<GroupInfoExpand>> searchGroups = new MutableLiveData<>(new ArrayList<>());
    public MutableLiveData<String> searchKey = new MutableLiveData<>();

    public void search(String key) {
        searchGroups.getValue().clear();
        if (!TextUtils.isEmpty(key)) {
            for (GroupInfo groupInfo : groups.getValue()) {
                if (groupInfo.getGroupName().toUpperCase().contains(key.toUpperCase())) {
                    GroupInfoExpand expand = new GroupInfoExpand();
                    if (groupInfo.getCreatorUserID().equals(BaseApp.inst().loginCertificate.userID)) {
                        //我创建的
                        expand.groupType = getContext().getString(io.openim.android.ouicore.R.string.the_group_I_created);
                    }else {
                        //我加入的
                        expand.groupType = getContext().getString(io.openim.android.ouicore.R.string.the_group_I_joined);
                    }
                    expand.setGroupID(groupInfo.getGroupID());
                    expand.setGroupName(groupInfo.getGroupName());
                    expand.setFaceURL(groupInfo.getFaceURL());
                    expand.setGroupType(groupInfo.getGroupType());
                    expand.setEx(groupInfo.getEx());
                    expand.setApplyMemberFriend(groupInfo.getApplyMemberFriend());
                    expand.setCreateTime(groupInfo.getCreateTime());
                    expand.setCreatorUserID(groupInfo.getCreatorUserID());
                    expand.setApplyMemberFriend(groupInfo.getApplyMemberFriend());
                    expand.setIntroduction(groupInfo.getIntroduction());
                    expand.setMemberCount(groupInfo.getMemberCount());
                    searchGroups.getValue().add(expand);
                }
            }
        }
        searchGroups.setValue(searchGroups.getValue());
    }
}
