import wfc from "@wfc/client/wfc";
import helper from "utils/helper";

// --------------------------------  群  --------------------------------
/**
 * 选择整个群时，合并
 * @param {*} selectGroupList
 * @param {*} choosedPeople
 * @param {*} cancelSelectGroup
 * @param {*} cancelSelectGroupId
 */
export function onMergeGroupAndchoosedPeople(
    selectGroupList,
    choosedPeople,
    cancelSelectGroup,
    cancelSelectGroupId
) {
    let groupLen = selectGroupList.length
    let choosedLen = choosedPeople.length
    let tempChoosedPeople = [].concat(choosedPeople)
    for (let i = 0; i < groupLen; i++) {
        let flag = false
        for (let j = 0; j < choosedLen; j++) {
            if (selectGroupList[i].id === choosedPeople[j].id) {
                flag = true
            }
        }
        if (!flag) {
            tempChoosedPeople.push(selectGroupList[i])
        }
    }
    // 点击取消群
    if (cancelSelectGroup) {
        let allGroupMerbers = []
        const groupMenbers = wfc.getGroupMembers(cancelSelectGroupId)
        groupMenbers.forEach(item => {
            let userInfo = helper.changeAllUserToIm(item.memberId);
            if (userInfo) {
                allGroupMerbers.push(userInfo)
            }
        })
        allGroupMerbers.map(item => [
            tempChoosedPeople.splice(
                tempChoosedPeople.findIndex(peopleItem => peopleItem.id === item.id), 1
            )
        ])

    }
    return tempChoosedPeople
}

/**
 * 筛选重复的群成员
 * @param {*} groupList
 * @param {*} choosedPeople
 * @param {*} cancelSelectGroup
 * @param {*} cancelSelectGroupId
 */
export function onFilterGroupMenbers(
    groupList,
    choosedPeople,
    cancelSelectGroup,
    cancelSelectGroupId
) {
    let allGroupMerbers = []
    groupList.map(item => {
        // 存放当前群成员
        // 从im中获取当前群的成员
        const groupMenbers = wfc.getGroupMembers(item.imGroupId)
        // 添加群成员属性
        groupMenbers.forEach(item => {
            let userInfo = helper.changeAllUserToIm(item.memberId);
            if (userInfo) {
                userInfo.checked = true
                userInfo.itemType = 'single'
                allGroupMerbers.push(userInfo)
            }
        })
    })
    let temp = [].concat(allGroupMerbers)
    let len = allGroupMerbers.length
    // 去重
    for (let i = 0; i < len - 1; i++) {
        let flag = false
        for (let j = i + 1; j < len; j++) {
            if (allGroupMerbers[i].id === allGroupMerbers[j].id) {
                temp.forEach((item, index) => {
                    if (!flag) {
                        if (item.id === allGroupMerbers[i].id) {
                            flag = true
                            temp.splice(index, 1)
                        }
                    }
                })
            }
        }
    }
    return onMergeGroupAndchoosedPeople(
        temp,
        choosedPeople,
        cancelSelectGroup,
        cancelSelectGroupId
    )
}

/**
 * 取消选择某个群
 * @param {} groupList
 * @param {*} selectGroup
 */
export function isCancelGroup(groupList, selectGroup) {
    let groupLen = groupList.length
    let selectLen = selectGroup.length

    // 判断是否是取消选择群的操作
    let count = 0
    for (let i = 0; i < groupLen; i++) {
        if (groupList[i].checked) {
            count++
        }
    }
    let groupId = ''
    let isCancel = false

    // 取消群选择
    if (count > selectLen) {
        // 找出被取消的群id
        for (let i = 0; i < groupLen; i++) {
            if (groupList[i].checked) {
                let flag = false
                for (let j = 0; j < selectLen; j++) {
                    if (groupList[i].imGroupId == selectGroup[j].imGroupId) {
                        flag = true
                    }
                }
                if (!flag) {
                    groupId = groupList[i].imGroupId
                    isCancel = true
                }
            }
        }
    }
    return { groupId, isCancel }
}

/**
 * 选择群
 * @param {*} groupList 所有的群
 * @param {*} selectGroup 选择的群
 * @param {*} choosedPeople 已选择的人员
 * @returns
 */
export function onSelectGroup(groupList, selectGroup, choosedPeople) {
    groupList.map(item => {
        item.isAllCheck = false
        item.checked = false
    })
    for (let i = 0; i < groupList.length; i++) {
        for (let j = 0; j < selectGroup.length; j++) {
            if (groupList[i].imGroupId === selectGroup[j].imGroupId) {
                groupList[i].isAllCheck = true
                groupList[i].checked = true
                break
            }
        }
    }
    groupList = peopleOfGroupStaus(groupList, choosedPeople)

    return groupList
}

/**
 * 改变群的选中选择状态
 * @param {*} groupLists 群列表
 * @param {*} choosedPeople 当前被选中的人员
 */
export function peopleOfGroupStaus(groupLists, choosedPeople) {
    let groupList = [].concat(groupLists)
    // choosedPeople 为空的情况
    if (choosedPeople.length == 0) {
        groupList.map(item => {
            item.isAllCheck = false
            item.isSomeCheck = false
            item.checked = false
        })
    } else {
        groupList.map(item => {
            let count = 0
            let groupMenbers = []
            const Menbers = wfc.getGroupMembers(item.imGroupId)
            // 添加群成员属性
            Menbers.forEach(item => {
                let userInfo = helper.changeAllUserToIm(item.memberId);
                if (userInfo) {
                    groupMenbers.push(userInfo)
                }
            })
            let menbersLen = groupMenbers.length
            let peopleLen = choosedPeople.length
            for (let i = 0; i < menbersLen; i++) {
                for (let j = 0; j < peopleLen; j++) {
                    if (groupMenbers[i].id == choosedPeople[j].id) {
                        count++;
                    }
                }
            }
            if (count === groupMenbers.length) {
                // 当前群成员全部选中
                item.isAllCheck = true
                item.isSomeCheck = false
                item.checked = true
            } else if (count < groupMenbers.length && count > 0) {
                // 部分选中
                item.isAllCheck = false
                item.isSomeCheck = true
                item.checked = false
            } else if (count === 0) {
                // 全部未选中
                item.isAllCheck = false
                item.isSomeCheck = false
                item.checked = false
            }
            return item
        })
    }
    return groupLists
}

/**
 * 显示群成员的选中状态
 * @param {*} groupMenbers
 * @param {*} choosedPeople
 */
export function showGroupMenbersStatus(groupMenbers, choosedPeople) {
    choosedPeople.forEach(item => {
        for (let i = 0; i < groupMenbers.length; i++) {
            if (groupMenbers[i].id === item.id) {
                groupMenbers[i].checked = true
            }
        }
    })
    return groupMenbers
}

// --------------------------------  团队  --------------------------------
/**
 * 获取团队成员
 * @param {*} imGroupId
 * @returns 团队成员列表
 */
export function getTeamMenbers(imGroupId) {
    let userIds = wfc.getGroupMemberIds(imGroupId)
    // 团队成员
    let list = []
    userIds.forEach((userId) => {
        let userInfo = helper.changeAllUserToIm(userId);
        if (userInfo) {
            userInfo.itemType = "single";
            list.push(userInfo);
        }
    });
    return list
}

/**
 * 取消选择某个团队
 * @param {} teamList
 * @param {*} selectTeam
 */
 export function isCancelTeam(teamList, selectTeam) {
    let teamLen = teamList.length
    let selectLen = selectTeam.length

    // 判断是否是取消选择群的操作
    let count = 0
    for (let i = 0; i < teamLen; i++) {
        if (teamList[i].checked) {
            count++
        }
    }
    let teamId = ''
    let isCancel = false

    // 取消群选择
    if (count > selectLen) {
        // 找出被取消的群id
        for (let i = 0; i < teamLen; i++) {
            if (teamList[i].checked) {
                let flag = false
                for (let j = 0; j < selectLen; j++) {
                    if (teamList[i].imGroupId == selectTeam[j].imGroupId) {
                        flag = true
                    }
                }
                if (!flag) {
                    teamId = teamList[i].imGroupId
                    isCancel = true
                }
            }
        }
    }
    return { teamId, isCancel }
}

/**
 * 选择整个团队时，合并
 * @param {*} selectTeamList
 * @param {*} choosedPeople
 * @param {*} cancelSelectTeam
 * @param {*} cancelSelectTeamId
 */
export function onMergeTeamAndchoosedPeople(
    selectTeamList,
    choosedPeople,
    cancelSelectTeam,
    cancelSelectTeamId
) {
    let groupLen = selectTeamList.length
    let choosedLen = choosedPeople.length
    let tempChoosedPeople = [].concat(choosedPeople)
    for (let i = 0; i < groupLen; i++) {
        let flag = false
        for (let j = 0; j < choosedLen; j++) {
            if (selectTeamList[i].id === choosedPeople[j].id) {
                flag = true
            }
        }
        if (!flag) {
            tempChoosedPeople.push(selectTeamList[i])
        }
    }
    // 点击取消群
    if (cancelSelectTeam) {
        let allTeamMerbers = getTeamMenbers(cancelSelectTeamId)
        allTeamMerbers.map(item => [
            tempChoosedPeople.splice(
                tempChoosedPeople.findIndex(peopleItem => peopleItem.id === item.id), 1
            )
        ])

    }
    return tempChoosedPeople
}

/**
 * 筛选重复的团队成员
 * @param {*} selectTeam
 * @param {*} choosedPeople
 * @param {*} cancelSelectTeam
 * @param {*} cancelSelectTeamId
 */
export function onFilterTeamMenbers(
    selectTeam,
    choosedPeople,
    cancelSelectTeam,
    cancelSelectTeamId
) {
    let allTeamMerbers = []
    selectTeam.map(item => {
        // 存放当前群成员
        // 从im中获取当前群的成员
        let teamMenbers = getTeamMenbers(item.imGroupId)

        // 添加群成员属性
        teamMenbers.forEach(item => {
            item.checked = true
            item.itemType = 'single'
        })
        allTeamMerbers = allTeamMerbers.concat(teamMenbers)
    })
    let temp = [].concat(allTeamMerbers)
    let len = allTeamMerbers.length
    // 去重
    for (let i = 0; i < len - 1; i++) {
        let flag = false
        for (let j = i + 1; j < len; j++) {
            if (allTeamMerbers[i].id === allTeamMerbers[j].id) {
                temp.forEach((item, index) => {
                    if (!flag) {
                        if (item.id === allTeamMerbers[i].id) {
                            flag = true
                            temp.splice(index, 1)
                        }
                    }
                })
            }
        }
    }
    return onMergeTeamAndchoosedPeople(
        temp,
        choosedPeople,
        cancelSelectTeam,
        cancelSelectTeamId
    )
}

/**
 * 显示团队成员的选中状态
 * @param {*} teamMenbers
 * @param {*} choosedPeople
 */
export function showTeamMenbersStatus(teamMenbers, choosedPeople) {
    choosedPeople.forEach(item => {
        for (let i = 0; i < teamMenbers.length; i++) {
            if (teamMenbers[i].id === item.id) {
                teamMenbers[i].checked = true
            }
        }
    })
    return teamMenbers
}

/**
 * 改变团队的选中选择状态
 * @param {*} teamLists 团队列表
 * @param {*} choosedPeople 当前被选中的人员
 */
export function peopleOfTeamStaus(teamLists, choosedPeople) {
    let teamList = [].concat(teamLists)
    // choosedPeople 为空的情况
    if (choosedPeople.length == 0) {
        teamList.map(item => {
            item.isAllCheck = false
            item.isSomeCheck = false
            item.checked = false
        })
    } else {
        teamList.map(item => {
            let count = 0
            let teamMenbers = []
            const Menbers = wfc.getGroupMembers(item.imGroupId)
            // 添加群成员属性
            Menbers.forEach(item => {
                let userInfo = helper.changeAllUserToIm(item.memberId);
                if (userInfo) {
                    teamMenbers.push(userInfo)
                }
            })
            let menbersLen = teamMenbers.length
            let peopleLen = choosedPeople.length
            for (let i = 0; i < menbersLen; i++) {
                for (let j = 0; j < peopleLen; j++) {
                    if (teamMenbers[i].id == choosedPeople[j].id) {
                        count++;
                    }
                }
            }
            if (count === teamMenbers.length) {
                // 当前群成员全部选中
                item.isAllCheck = true
                item.isSomeCheck = false
                item.checked = true
            } else if (count < teamMenbers.length && count > 0) {
                // 部分选中
                item.isAllCheck = false
                item.isSomeCheck = true
                item.checked = false
            } else if (count === 0) {
                // 全部未选中
                item.isAllCheck = false
                item.isSomeCheck = false
                item.checked = false
            }
            return item
        })
    }
    return teamLists
}

/**
 * 选择团队
 * @param {*} teamList 所有的团队
 * @param {*} selectTeam 选择的团队
 * @param {*} choosedPeople 已选择的人员
 * @returns
 */
export function onSelectTeam(teamList, selectTeam, choosedPeople) {
    teamList.map(item => {
        item.isAllCheck = false
        item.checked = false
    })
    for (let i = 0; i < teamList.length; i++) {
        for (let j = 0; j < selectTeam.length; j++) {
            if (teamList[i].imGroupId === selectTeam[j].imGroupId) {
                teamList[i].isAllCheck = true
                teamList[i].checked = true
                break
            }
        }
    }
    // teamList = peopleOfGroupStaus(teamList, choosedPeople)

    return teamList
}

// --------------------------------  其他  --------------------------------

/**
 * 合并最近联系人
 * @param {*} checkedValue
 * @param {*} chatsList
 * @param {*} choosedPeople
 */
 export function mergeRecentContactAndChoosedPeople(checkedValue, chatsList, choosedPeople) {
    let checkedLen = checkedValue.length
    let chatsLen = chatsList.length
    let choosedLen = choosedPeople.length

    let tempChoosedPeople = [].concat(choosedPeople)
    let temp = [].concat(chatsList)
    for (let i = 0; i < chatsLen; i++) {
        let flag = false
        for (let j = 0; j < checkedLen; j++) {
            if (chatsList[i].id === checkedValue[j].id) {
                flag = true
            }
        }
        if (flag) {
            temp.splice(temp.findIndex(item => item.id === chatsList[i].id), 1)
        }
    }

    for (let i = 0; i < checkedLen; i++) {
        let flag = false
        for (let j = 0; j < choosedLen; j++) {
            if (choosedPeople[j].id === checkedValue[i].id) {
                flag = true
            }
        }
        if (!flag) {
            tempChoosedPeople.push(checkedValue[i])
        }
    }

    let tempLen = temp.length
    for (let i = 0; i < choosedLen; i++) {
        let flag = false
        for (let j = 0; j < tempLen; j++) {
            if (choosedPeople[i].id === temp[j].id) {
                flag = true
            }
        }
        if (flag) {
            tempChoosedPeople.splice(tempChoosedPeople.findIndex(item => item.id === choosedPeople[i].id), 1)
        }
    }
    return tempChoosedPeople
}

/**
 * 合并当前选择人员
 * @param {*} CheckedValue
 * @param {*} choosedPeople
 * @param {*} currentImGroupId
 */
 export function mergeCheckedValueAndchoosedPeople(
    checkedValue,
    choosedPeople,
    currentImGroupId
) {
    let tempChoosedPeople = [].concat(choosedPeople)
    let checkedLen = checkedValue.length
    let choosedLen = choosedPeople.length
    const Menbers = wfc.getGroupMembers(currentImGroupId)
    // 添加群成员属性
    let groupMenbers = []
    Menbers.forEach(item => {
        let userInfo = helper.changeAllUserToIm(item.memberId);
        if (userInfo) {
            groupMenbers.push(userInfo)
        }
    })
    // 零时保存未选中的群成员
    let temp = [].concat(groupMenbers)
    let groupMenbersLen = groupMenbers.length
    for (let i = 0; i < groupMenbersLen; i++) {
        let flag = false
        for (let j = 0; j < checkedLen; j++) {
            if (groupMenbers[i].id === checkedValue[j].id) {
                flag = true
            }
        }
        if (flag) {
            temp.splice(temp.findIndex(item => item.id === groupMenbers[i].id), 1)
        }
    }

    for (let i = 0; i < checkedLen; i++) {
        let flag = false
        for (let j = 0; j < choosedLen; j++) {
            if (choosedPeople[j].id === checkedValue[i].id) {
                flag = true
            }
        }
        if (!flag) {
            tempChoosedPeople.push(checkedValue[i])
        }
    }

    let tempLen = temp.length
    for (let i = 0; i < choosedLen; i++) {
        let flag = false
        for (let j = 0; j < tempLen; j++) {
            if (choosedPeople[i].id === temp[j].id) {
                flag = true
            }
        }
        if (flag) {
            tempChoosedPeople.splice(tempChoosedPeople.findIndex(item => item.id === choosedPeople[i].id), 1)
        }
    }
    return tempChoosedPeople
}
