def list_groups_request(intent):
    smgr = SplitwiseAccountmanager(userId=intent['userId'])
    groups = smgr.get_groups()
    group_name = ""
    for group in groups:
        group_name = group_name + group.getName() + ' \n '

    return "You have following groups in your account:\n {}".format(group_name)
def create_group_request(intent):
    group_name = try_ex(lambda: intent['currentIntent']['slots']['GroupName'])
    if group_name is not None:
        smgr = SplitwiseAccountmanager(userId=intent['userId'])
        smgr.create_group(group_name)
        return "Successfully created group name : {} ! Now you can invite friends and add them here".format(
            group_name)
    return "Sorry! Group {} can not be created".format(group_name)
def add_friend_request(intent):
    smgr = SplitwiseAccountmanager(userId=intent['userId'])
    first_name = try_ex(lambda: intent['currentIntent']['slots']['FirstName'])
    last_name = try_ex(lambda: intent['currentIntent']['slots']['LastName'])
    email = try_ex(lambda: intent['currentIntent']['slots']['Email'])
    if first_name and email:
        friend = dict(user_first_name=first_name, user_email=email)
        if last_name is not None:
            friend.update(dict(user_last_name=last_name))
        add_friend = smgr.add_friend(friend)
        logger.info("Add Friend Response %s" % add_friend)
        return "Successfully invited friend {} {}".format(
            first_name, last_name)
def get_friends(intent):
    smgr = SplitwiseAccountmanager(userId=intent['userId'])
    friends = smgr.get_friends()
    friend_list = ''
    for friend in friends:
        friend_list = friend_list + friend.getFirstName()
        if friend.getLastName() is not None:
            friend_list = friend_list + ' ' + friend.getLastName()
        friend_list = friend_list + '\n'

    if friend_list:
        return 'Your friends: \n{}'.format(friend_list)
    else:
        return 'Looks like you have not invited any friends yet, please invite friend'
def add_user_request(intent):
    smgr = SplitwiseAccountmanager(userId=intent['userId'])
    first_name = try_ex(lambda: intent['currentIntent']['slots']['FirstName'])
    group_name = try_ex(lambda: intent['currentIntent']['slots']['GroupName'])
    if first_name and group_name:
        friend_id = get_friend_id(smgr, first_name)
        logger.info('Friend Id {}'.format(friend_id))
        if friend_id is None:
            return ask_to_add_friend(first_name)
        group_id = get_group_id(smgr, group_name)
        logger.info('Group Id {}'.format(group_id))
        if group_id is None:
            return ask_to_create_group(group_name)
        smgr.add_user_to_group(friend_id, group_id)
        return "Successfully Added {} to group {}".format(
            first_name, group_name)
def create_expense_group(intent):
    logger.info('Create Expense :%s' % intent)
    group_name = try_ex(lambda: intent['currentIntent']['slots']['GroupName'])
    expense_dec = try_ex(
        lambda: intent['currentIntent']['slots']['ExpenseDescription'])
    expense_cost = try_ex(
        lambda: intent['currentIntent']['slots']['ExpenseCost'])
    if group_name and expense_cost and expense_cost:
        cost = float(expense_cost)
        smgr = SplitwiseAccountmanager(userId=intent['userId'])
        groups = smgr.get_groups()
        expense = smgr.get_expense_obj()
        current_user = smgr.get_current_user()
        my_id = current_user.getId()
        expense.setCost(cost)
        expense.setDescription(expense_dec)
        expense.setPayment(False)

        group_exist = False

        for group in groups:
            if group.getName().lower() == group_name.lower():
                group_exist = True
                group_id = group.getId()
                expense.setGroupId(group_id)
                members = group.getMembers()
                total_members = float(len(members))
                if total_members == 1:
                    return "There are no friends in this group, you should invite friends in group {}".format(
                        group_name)
                owe_share = float("{0:.2f}".format(cost / total_members))
                users = []
                # There is always atleast one group member
                for member in members:
                    user = smgr.get_expense_user()
                    user.setId(member.getId())
                    if my_id == member.getId():
                        my_owe_share = cost - owe_share * (total_members - 1)
                        user.setOwedShare(str(my_owe_share))
                        user.setPaidShare(str(cost))
                    else:
                        user.setOwedShare(str(owe_share))
                        user.setPaidShare(str(0))

                    users.append(user)

                expense.setUsers(users)

        if group_exist:
            expense = smgr.create_expense(expense)
            # parse the expense object to see if any error exist
            return 'Added your expense amount {} in group {}.'.format(
                expense_cost, group_name)
        else:
            return 'Oops! Group {} does not exist in your account. Why dont you create a new group'.format(
                group_name)
def get_users_in_group(intent):
    group_name = try_ex(lambda: intent['currentIntent']['slots']['GroupName'])
    smgr = SplitwiseAccountmanager(userId=intent['userId'])
    groups = smgr.get_groups()
    user = ''
    group_exist = False
    for group in groups:
        if group.getName().lower() == group_name.lower():
            group_exist = True
            members = group.getMembers()
            for m in members:
                user = user + m.getFirstName()
                if m.getLastName() is not None:
                    user = user + ' ' + m.getLastName()
                user = user + '\n'

    if user and group_exist:
        return 'Friends in this group are : \n{}'.format(user)
    elif not group_exist:
        return 'Sorry ! No group exist with name {}, Please create group'.format(
            group_name)
    else:
        return 'Sorry ! There are no friends added to this group, please add friends to this group'