from kopeechka.async_methods import AsyncMethods
from kopeechka.config import sft_id
from kopeechka.kopeechka_types import UserBalance, GetEmail, GetMessage, Status, GetTaskId, Domains, MailboxZones, Zone, Popular
from kopeechka.error import catch_error


class AsyncMailActivations(AsyncMethods):
    def __init__(self, api_token: str, timeout: int = 5):
        super().__init__(api_token, timeout)

    async def user_balance(self,
                           **kwargs
                           ) -> UserBalance:
        json_data = await super().user_balance(**kwargs)
        catch_error(json_data)
        return UserBalance(**json_data)

    async def mailbox_get_email(self,
                                site: str,
                                mail_type: str = "",
                                regex: str = "",
                                soft_id: int = sft_id,
                                investor: int = "",
                                subject: str = "",
                                password: int = 0,
                                **kwargs
                                ) -> GetEmail:
        json_data = await super().mailbox_get_email(site,
                                                    mail_type,
                                                    regex,
                                                    soft_id,
                                                    investor,
                                                    subject,
                                                    password,
                                                    **kwargs
                                                    )
        catch_error(json_data)
        return GetEmail(**json_data)

    async def mailbox_get_message(self,
                                  task_id: int,
                                  full: int = "",
                                  **kwargs
                                  ) -> GetMessage:
        json_data = await super().mailbox_get_message(task_id, full, **kwargs)
        catch_error(json_data)
        return GetMessage(**json_data)

    async def mailbox_cancel(self,
                             task_id: int,
                             **kwargs
                             ) -> Status:
        json_data = await super().mailbox_cancel(task_id, **kwargs)
        catch_error(json_data)
        return Status(**json_data)

    async def mailbox_reorder(self,
                              site: str,
                              email: str,
                              regex: str = "",
                              subject: str = "",
                              password: int = "",
                              **kwargs
                              ) -> GetEmail:
        json_data = await super().mailbox_reorder(site, email, regex, subject, password, **kwargs)
        catch_error(json_data)
        return GetEmail(**json_data)

    async def mailbox_get_fresh_id(self,
                                   site: str,
                                   email: str,
                                   **kwargs
                                   ) -> GetTaskId:
        json_data = await super().mailbox_get_fresh_id(site, email, **kwargs)
        catch_error(json_data)
        return GetTaskId(**json_data)

    async def mailbox_get_domains(self,
                                  **kwargs
                                  ) -> Domains:
        json_data = await super().mailbox_get_domains(**kwargs)
        catch_error(json_data)
        return Domains(**json_data)

    async def mailbox_zones(self,
                            popular: int = "",
                            zones: int = "",
                            site: str = "",
                            **kwargs
                            ) -> MailboxZones:
        json_data = await super().mailbox_zones(popular, zones, site, **kwargs)
        catch_error(json_data)
        return MailboxZones(json_data.get("status"), zones=[Zone(**x) for x in json_data.get("zones")] if json_data.get("zones") else None, popular=[Popular(**x) for x in json_data.get("popular")] if json_data.get("popular") else None)
