package com.ss.android.ugc.aweme.profile.model;

import X.46i;
import X.4AU;
import X.9oY;
import X.Ar5;
import X.BbB;
import X.C1816Dfo;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.feed.model.LogPbBean;
import com.ss.android.ugc.aweme.friends.model.RecommendList;
import com.ss.android.ugc.aweme.social.api.IRecommendUserApi;
import com.ss.android.ugc.aweme.social.api.RecommendUserApiService;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class RecommendCommonUserModel extends BbB<RecommendList> {
    public boolean isNewFindFriendsPage = false;
    public final IRecommendUserApi mRecommendApi = RecommendUserApiService.LIZ;
    public HashSet<String> mShownUserIds = new HashSet<>();
    public int newUserCount;
    public List<String> userIdList;

    static {
        Covode.recordClassIndex(98829);
    }

    public void resetShownUserIds() {
        this.mShownUserIds.clear();
        this.newUserCount = 0;
    }

    public void removeFollowedUser() {
        if (this.mData != null && !((RecommendList) this.mData).getUserList().isEmpty()) {
            List<User> userList = ((RecommendList) this.mData).getUserList();
            int size = userList.size();
            int i = 0;
            while (i < size) {
                if (userList.get(i).getFollowStatus() != 0) {
                    userList.remove(i);
                    size--;
                    i--;
                }
                i++;
            }
            ((RecommendList) this.mData).setUserList(userList);
        }
    }

    public void setRecommendList(RecommendList recommendList) {
        this.mQueryType = 1;
        handleData(recommendList);
    }

    public int getUserImprOrder(String str) {
        List<String> list = this.userIdList;
        if (list != null) {
            return list.indexOf(str);
        }
        return 0;
    }

    public void setData(List<User> list) {
        if (this.mData != null) {
            ((RecommendList) this.mData).setUserList(list);
        }
    }

    private int getScenarioFromRecommendType(int i) {
        if (i != 4) {
            if (i == 13) {
                return 2;
            }
            if (i != 21) {
                return 0;
            }
            return 3;
        } else if (C1816Dfo.LIZ.LIZ() > 0) {
            return 16;
        } else {
            return 1;
        }
    }

    public void recommendUserDialogLoadMore(int i) {
        int cursor;
        if (!this.mIsLoading) {
            this.mIsLoading = true;
            this.mQueryType = 4;
            IRecommendUserApi iRecommendUserApi = this.mRecommendApi;
            Integer valueOf = Integer.valueOf(i);
            if (this.mData == null) {
                cursor = 0;
            } else {
                cursor = ((RecommendList) this.mData).getCursor();
            }
            iRecommendUserApi.fetchRecommendUserDialoList(valueOf, Integer.valueOf(cursor), Ar5.LIZ.LIZ()).LIZ(new 4AU(this.mHandler));
        }
    }

    public void removeData(User user) {
        if (this.mData != null && !((RecommendList) this.mData).getUserList().isEmpty()) {
            List<User> userList = ((RecommendList) this.mData).getUserList();
            userList.remove(user);
            ((RecommendList) this.mData).setUserList(userList);
        }
    }

    public void handleData(RecommendList recommendList) {
        if (recommendList != null) {
            LogPbBean logPb = recommendList.getLogPb();
            if (!(recommendList.getUserList() == null || logPb == null)) {
                for (User requestId : recommendList.getUserList()) {
                    requestId.setRequestId(logPb.getImprId());
                }
            }
            if (this.mQueryType == 1) {
                this.newUserCount = 0;
                RecommendCommonUserModel.super.handleData(recommendList);
                Iterator<User> it = recommendList.getUserList().iterator();
                while (it.hasNext()) {
                    User next = it.next();
                    if (this.mShownUserIds.contains(next.getUid())) {
                        it.remove();
                    } else {
                        this.mShownUserIds.add(next.getUid());
                    }
                }
            } else if (this.mQueryType == 4) {
                if (this.mData != null) {
                    List<User> userList = ((RecommendList) this.mData).getUserList();
                    List<User> inviterList = ((RecommendList) this.mData).getInviterList();
                    int size = userList.size();
                    for (User next2 : recommendList.getUserList()) {
                        if (!this.mShownUserIds.contains(next2.getUid())) {
                            userList.add(next2);
                            this.mShownUserIds.add(next2.getUid());
                        }
                    }
                    inviterList.addAll(recommendList.getInviterList());
                    this.newUserCount = userList.size() - size;
                    this.mData = recommendList;
                    ((RecommendList) this.mData).setUserList(userList);
                    ((RecommendList) this.mData).setInviterList(inviterList);
                } else {
                    this.mData = recommendList;
                }
            }
            List<String> list = this.userIdList;
            if (list == null) {
                this.userIdList = new ArrayList();
            } else {
                list.clear();
            }
            if (recommendList.getUserList() != null) {
                for (User uid : recommendList.getUserList()) {
                    this.userIdList.add(uid.getUid());
                }
            }
            9oY.LIZ.LIZ(recommendList.getRid(), recommendList.getLogPb());
        }
    }

    public void loadMore(int i, String str, int i2, int i3, int i4, int i5, String str2, String str3) {
        int cursor;
        if (!this.mIsLoading) {
            this.mIsLoading = true;
            this.mQueryType = 4;
            if (this.mData == null) {
                cursor = 0;
            } else {
                cursor = ((RecommendList) this.mData).getCursor();
            }
            int i6 = i;
            String str4 = str;
            int i7 = i2;
            int i8 = i3;
            fetchData(i6, str4, cursor, i7, i8, i4, (String) null, i5, str2, str3, false);
        }
    }

    public void fetchData(int i, String str, int i2, int i3, int i4, int i5, String str2, int i6) {
        int i7;
        if (!this.mIsLoading) {
            if (46i.LJFF().isUidContactPermisioned()) {
                i7 = 1;
            } else {
                i7 = 2;
            }
            this.mIsLoading = true;
            this.mQueryType = 1;
            this.mRecommendApi.recommendList(Integer.valueOf(i), (Integer) 0, str, Integer.valueOf(i2), Integer.valueOf(i3), Integer.valueOf(i7), Ar5.LIZ.LIZ(), str2, i6).LIZ(new 4AU(this.mHandler));
        }
    }

    public void refreshRecommendUser(int i, String str, int i2, int i3, int i4, String str2, int i5, String str3, String str4, String str5, boolean z) {
        if (!this.mIsLoading) {
            this.mIsLoading = true;
            this.mQueryType = 1;
            int i6 = i3;
            int i7 = i2;
            String str6 = str;
            int i8 = i;
            int i9 = i5;
            fetchData(i8, str6, 0, i7, i6, i4, str2, i9, str3, str4, z);
        }
    }

    private void fetchData(int i, String str, int i2, int i3, int i4, int i5, String str2, int i6, String str3, String str4, boolean z) {
        int i7;
        RecommendCommonUserModel recommendCommonUserModel = this;
        if (46i.LJFF().isUidContactPermisioned()) {
            i7 = 1;
        } else {
            i7 = 2;
        }
        String str5 = str2;
        String str6 = str;
        String str7 = str3;
        int i8 = i3;
        if (!TextUtils.isEmpty(str5)) {
            recommendCommonUserModel.mRecommendApi.recommendList(Integer.valueOf(i), Integer.valueOf(i2), str6, Integer.valueOf(i8), 1, Integer.valueOf(i7), Ar5.LIZ.LIZ(), str5, str7, str4).LIZ(new 4AU(recommendCommonUserModel.mHandler));
        } else if (i8 == 1) {
            recommendCommonUserModel.mRecommendApi.recommendListMT(Integer.valueOf(i), Integer.valueOf(i2), Ar5.LIZ.LIZ(), str6, Integer.valueOf(recommendCommonUserModel.getScenarioFromRecommendType(i8)), false).LIZ(new 4AU(recommendCommonUserModel.mHandler));
        } else if (i8 == 4 || i8 == 13 || i8 == 21) {
            recommendCommonUserModel.mRecommendApi.recommendListMT(Integer.valueOf(i), Integer.valueOf(i2), Ar5.LIZ.LIZ(), str7, Integer.valueOf(recommendCommonUserModel.getScenarioFromRecommendType(i8)), z).LIZ(new 4AU(recommendCommonUserModel.mHandler));
        } else if (3 == i8) {
            recommendCommonUserModel.mRecommendApi.recommendList4NewFindFriends(Integer.valueOf(i), Integer.valueOf(i2), Ar5.LIZ.LIZ()).LIZ(new 4AU(recommendCommonUserModel.mHandler));
        }
    }
}
