from dataclasses import dataclass

from abra_python.resources.base import BaseResource


class Account(BaseResource):
    def list(self, query: dict = None) -> list:
        if query is None:
            query = {'take': 10}

        response = self.requestor.request(
            method='GET',
            endpoint_url='/accounts',
            query=query
        )

        return [
            self.Data(item)
            for item in response
        ]

    def get(self, account_id: str):
        response = self.requestor.request(
            method='GET',
            endpoint_url=f'/accounts/{account_id}'
        )

        return self.Data(response)

    def create(self, payload: dict):
        response = self.requestor.request(
            method='POST',
            endpoint_url='/accounts',
            payload=payload
        )

        return self.Data(response)

    def update(self, account_id: str, payload: dict):
        response = self.requestor.request(
            method='PUT',
            endpoint_url=f'/accounts/{account_id}',
            payload=payload
        )

        return self.Data(response)

    @dataclass
    class Data:
        id: str
        objversion: int
        hidden: bool
        code: str
        name: str
        accounttype: str
        taxable: bool
        short: bool
        isincomplete: bool
        transferable: bool
        iscostforprojectcontrol: bool
        isrevenueforprojectcontrol: bool
        typeofactivity: int
        parent_id: str = None
        parent2_id: str = None
        balanceaccount: bool = False
        vatindex_id: str = None
        accounttypeindex: int = 0
        printtoacumulatedstatement: bool = False

        def __init__(self, data):
            self.id = data.get("id")
            self.objversion = data.get("objVersion")
            self.hidden = data.get("hidden")
            self.code = data.get("code")
            self.name = data.get("name")
            self.accounttype = data.get("accounttype")
            self.taxable = data.get("taxable")
            self.short = data.get("short")
            self.isincomplete = data.get("isincomplete")
            self.transferable = data.get("transferable")
            self.iscostforprojectcontrol = data.get("iscostforprojectcontrol")
            self.isrevenueforprojectcontrol = data.get("isrevenueforprojectcontrol")
            self.typeofactivity = data.get("typefactivity")
            self.parent_id = data.get("parent_id")
            self.parent2_id = data.get("parent2_id")
            self.balanceaccount = data.get("balanceaccount")
            self.vatindex_id = data.get("vatindex_id")
            self.accounttypeindex = data.get("accounttypeindex")
            self.printtoacumulatedstatement = data.get("printtoacumulatedstatement")
