import typing
from dataclasses import dataclass
from solders.pubkey import Pubkey
from solana.rpc.async_api import AsyncClient
from solana.rpc.commitment import Commitment
import borsh_construct as borsh
from anchorpy.coder.accounts import ACCOUNT_DISCRIMINATOR_SIZE
from anchorpy.error import AccountInvalidDiscriminator
from anchorpy.utils.rpc import get_multiple_accounts
from anchorpy.borsh_extension import BorshPubkey
from ..program_id import PROGRAM_ID


class OrderJSON(typing.TypedDict):
    order_id: int
    user_pubkey: str
    user_token_addr: str
    source_token_mint: str
    match_pubkey: typing.Optional[str]
    dst_token_mint: list[int]
    source_sell_amount: int
    dst_buy_amount: int
    min_sell_amount: int
    settled: int
    eid: int
    bump: int
    is_valiable: bool
    target_address: list[int]
    bond_asset_mint: str
    bond_amount: int
    bond_fee: int


@dataclass
class Order:
    discriminator: typing.ClassVar = b"\x86\xad\xdf\xb9MV\x1c3"
    layout: typing.ClassVar = borsh.CStruct(
        "order_id" / borsh.U64,
        "user_pubkey" / BorshPubkey,
        "user_token_addr" / BorshPubkey,
        "source_token_mint" / BorshPubkey,
        "match_pubkey" / borsh.Option(BorshPubkey),
        "dst_token_mint" / borsh.U8[32],
        "source_sell_amount" / borsh.U64,
        "dst_buy_amount" / borsh.U64,
        "min_sell_amount" / borsh.U64,
        "settled" / borsh.U64,
        "eid" / borsh.U32,
        "bump" / borsh.U8,
        "is_valiable" / borsh.Bool,
        "target_address" / borsh.U8[32],
        "bond_asset_mint" / BorshPubkey,
        "bond_amount" / borsh.U64,
        "bond_fee" / borsh.U16,
    )
    order_id: int
    user_pubkey: Pubkey
    user_token_addr: Pubkey
    source_token_mint: Pubkey
    match_pubkey: typing.Optional[Pubkey]
    dst_token_mint: list[int]
    source_sell_amount: int
    dst_buy_amount: int
    min_sell_amount: int
    settled: int
    eid: int
    bump: int
    is_valiable: bool
    target_address: list[int]
    bond_asset_mint: Pubkey
    bond_amount: int
    bond_fee: int

    @classmethod
    async def fetch(
        cls,
        conn: AsyncClient,
        address: Pubkey,
        commitment: typing.Optional[Commitment] = None,
        program_id: Pubkey = PROGRAM_ID,
    ) -> typing.Optional["Order"]:
        resp = await conn.get_account_info(address, commitment=commitment)
        info = resp.value
        if info is None:
            return None
        if info.owner != program_id:
            raise ValueError("Account does not belong to this program")
        bytes_data = info.data
        return cls.decode(bytes_data)

    @classmethod
    async def fetch_multiple(
        cls,
        conn: AsyncClient,
        addresses: list[Pubkey],
        commitment: typing.Optional[Commitment] = None,
        program_id: Pubkey = PROGRAM_ID,
    ) -> typing.List[typing.Optional["Order"]]:
        infos = await get_multiple_accounts(conn, addresses, commitment=commitment)
        res: typing.List[typing.Optional["Order"]] = []
        for info in infos:
            if info is None:
                res.append(None)
                continue
            if info.account.owner != program_id:
                raise ValueError("Account does not belong to this program")
            res.append(cls.decode(info.account.data))
        return res

    @classmethod
    def decode(cls, data: bytes) -> "Order":
        if data[:ACCOUNT_DISCRIMINATOR_SIZE] != cls.discriminator:
            raise AccountInvalidDiscriminator(
                "The discriminator for this account is invalid"
            )
        dec = Order.layout.parse(data[ACCOUNT_DISCRIMINATOR_SIZE:])
        return cls(
            order_id=dec.order_id,
            user_pubkey=dec.user_pubkey,
            user_token_addr=dec.user_token_addr,
            source_token_mint=dec.source_token_mint,
            match_pubkey=dec.match_pubkey,
            dst_token_mint=dec.dst_token_mint,
            source_sell_amount=dec.source_sell_amount,
            dst_buy_amount=dec.dst_buy_amount,
            min_sell_amount=dec.min_sell_amount,
            settled=dec.settled,
            eid=dec.eid,
            bump=dec.bump,
            is_valiable=dec.is_valiable,
            target_address=dec.target_address,
            bond_asset_mint=dec.bond_asset_mint,
            bond_amount=dec.bond_amount,
            bond_fee=dec.bond_fee,
        )

    def to_json(self) -> OrderJSON:
        return {
            "order_id": self.order_id,
            "user_pubkey": str(self.user_pubkey),
            "user_token_addr": str(self.user_token_addr),
            "source_token_mint": str(self.source_token_mint),
            "match_pubkey": (
                None if self.match_pubkey is None else str(self.match_pubkey)
            ),
            "dst_token_mint": self.dst_token_mint,
            "source_sell_amount": self.source_sell_amount,
            "dst_buy_amount": self.dst_buy_amount,
            "min_sell_amount": self.min_sell_amount,
            "settled": self.settled,
            "eid": self.eid,
            "bump": self.bump,
            "is_valiable": self.is_valiable,
            "target_address": self.target_address,
            "bond_asset_mint": str(self.bond_asset_mint),
            "bond_amount": self.bond_amount,
            "bond_fee": self.bond_fee,
        }

    @classmethod
    def from_json(cls, obj: OrderJSON) -> "Order":
        return cls(
            order_id=obj["order_id"],
            user_pubkey=Pubkey.from_string(obj["user_pubkey"]),
            user_token_addr=Pubkey.from_string(obj["user_token_addr"]),
            source_token_mint=Pubkey.from_string(obj["source_token_mint"]),
            match_pubkey=(
                None
                if obj["match_pubkey"] is None
                else Pubkey.from_string(obj["match_pubkey"])
            ),
            dst_token_mint=obj["dst_token_mint"],
            source_sell_amount=obj["source_sell_amount"],
            dst_buy_amount=obj["dst_buy_amount"],
            min_sell_amount=obj["min_sell_amount"],
            settled=obj["settled"],
            eid=obj["eid"],
            bump=obj["bump"],
            is_valiable=obj["is_valiable"],
            target_address=obj["target_address"],
            bond_asset_mint=Pubkey.from_string(obj["bond_asset_mint"]),
            bond_amount=obj["bond_amount"],
            bond_fee=obj["bond_fee"],
        )
