# matrix-appservice-kakaotalk - A Matrix-KakaoTalk puppeting bridge.
# Copyright (C) 2022 Tulir Asokan, Andrew Ferrazzutti
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations

from typing import TYPE_CHECKING
import asyncio

from mautrix.bridge.commands import HelpSection, command_handler

from ..kt.types.api.struct import ApiUserType

from .. import puppet as pu, user as u
from .typehint import CommandEvent

from ..kt.client.errors import CommandException

SECTION_FRIENDS = HelpSection("Friends management", 40, "")

if TYPE_CHECKING:
    from ..kt.types.api.struct import FriendStruct


async def _get_search_result_puppet(source: u.User, friend_struct: FriendStruct) -> pu.Puppet:
    puppet = await pu.Puppet.get_by_ktid(friend_struct.userId)
    if not puppet.name_set:
        await puppet.update_info_from_friend(source, friend_struct)
    return puppet


@command_handler(
    needs_auth=True,
    management_only=False,
    help_section=SECTION_FRIENDS,
    help_text="List all KakaoTalk friends",
)
async def list_friends(evt: CommandEvent) -> None:
    try:
        resp = await evt.sender.client.list_friends()
        await evt.mark_read()
    except CommandException as e:
        await evt.reply(f"Error while listing friends: {e!s}")
        return
    puppets = await asyncio.gather(
        *[
            _get_search_result_puppet(evt.sender, friend_struct)
            for friend_struct in resp.friends if friend_struct.userType == ApiUserType.NORMAL
            # NOTE Using NORMAL to avoid listing KakaoTalk bots, which are apparently PLUS users
        ]
    )
    results = "".join(
        f"* [{puppet.name}](https://matrix.to/#/{puppet.default_mxid})\n" for puppet in puppets
    )
    if results:
        await evt.reply(f"{results}")
    else:
        await evt.reply("No friends found.")