import {FriendShipType} from '../db/FriendShip';

export function getRoleIds(roleIds: string[]) {
    roleIds.sort();
    return {
        roleIdA: roleIds[0],
        roleIdB: roleIds[1],
        roleIds: roleIds.join('|')
    }
}

export function outputCnt(myRoleId: string, friendShip: FriendShipType) {
    let roleIds = friendShip.roleIds.split('|');
    let { roleIdA, roleIdB } = getRoleIds(roleIds);
    if (roleIdA == myRoleId) {
        return {
            sendHeart: friendShip.sendHeartCntA, // 我送给他
            sendHeartTime: friendShip.sendHeartCntATime, // 我送他时间
            beSentHeart: friendShip.sendHeartCntB, // 他送我
            beSentHeartTime: friendShip.sendHeartCntBTime, // 他送我时间
            receiveHeart: friendShip.receiveHeartCntA, // 我收他
            receiveHeartTime: friendShip.receiveHeartCntATime, // 我收他时间
            beReceivedHeart: friendShip.receiveHeartCntB, // 他收我
            beReceivedHeartTime: friendShip.receiveHeartCntBTime // 他收我时间
        }
    } else if (roleIdB == myRoleId) {
        return {
            sendHeart: friendShip.sendHeartCntB,
            sendHeartTime: friendShip.sendHeartCntBTime,
            beSentHeart: friendShip.sendHeartCntA,
            beSentHeartTime: friendShip.sendHeartCntATime,
            receiveHeart: friendShip.receiveHeartCntB,
            receiveHeartTime: friendShip.receiveHeartCntBTime,
            beReceivedHeart: friendShip.receiveHeartCntA,
            beReceivedHeartTime: friendShip.receiveHeartCntATime
        }
    } else {
        return false
    }
}

interface InputParam {
    inc: {
        sendHeart?: number; // 赠送
        receiveHeart?: number; // 收取
    },
    set: {
        sendHeartTime?: number; // 赠送时间
        receiveHeartTime?: number; // 收取时间
    },
    lt: {
        sendHeart?: number; // 赠送
        receiveHeart?: number; // 收取
    }
}

export function inputSendCnt(myRoleId: string, hisRoleId: string, param: InputParam) {
    let { roleIdA, roleIdB } = getRoleIds([myRoleId, hisRoleId]);
    if(roleIdA == myRoleId) {
        return {
            inc: {
                sendHeartCntA: param.inc.sendHeart,
            },
            set: {
                sendHeartCntATime: param.set.sendHeartTime,
            },
            lt: {
                sendHeartCntA: {
                    $lt: param.inc.sendHeart
                }
            }
        }
    } else if (roleIdB == myRoleId) {
        return {
            inc: {
                sendHeartCntB: param.inc.sendHeart
            },
            set: {
                sendHeartCntBTime: param.set.sendHeartTime
            },
            lt: {
                sendHeartCntB: {
                    $lt: param.inc.sendHeart
                }
            }
        }
    } else {
        return false;
    }
}

export function inputReceiveCnt(myRoleId: string, hisRoleId: string, param: InputParam) {
    let { roleIdA, roleIdB } = getRoleIds([myRoleId, hisRoleId]);
    if(roleIdA == myRoleId) {
        return {
            inc: {
                receiveHeartCntA: param.inc.receiveHeart,
            },
            set: {
                receiveHeartCntATime: param.set.receiveHeartTime
            },
            lt: {
                receiveHeartCntA: {
                    $lt: param.inc.receiveHeart
                }
            }
        }
    } else if (roleIdB == myRoleId) {
        return {
            inc: {
                receiveHeartCntB: param.inc.receiveHeart,
            },
            set: {
                receiveHeartCntBTime: param.set.receiveHeartTime
            },
            lt: {
                receiveHeartCntB: {
                    $lt: param.inc.receiveHeart
                }
            }
        }
    } else {
        return false;
    }
}


export function setCanReceiveAndCanSend(this: any, roleId: string, friendship: FriendShipType) {

    let frd = outputCnt(roleId, friendship);
    if(frd) {
        let { sendHeart, receiveHeart, beSentHeart } = frd;
        if(sendHeart >= 1) {
            this.canSend = false;
        }
        if(beSentHeart >= 1 && receiveHeart < beSentHeart) {
            this.canReceive = true;
        }
    }
}