import re
from dataclasses import dataclass, field
from typing import Optional, List
from eq1core.utils import Numeric


@dataclass(frozen=True)
class ReceivedData:
    cmd: str
    data: List[str]

    @classmethod
    def from_bytes(cls, data: bytes):
        data = data.decode('utf-8')
        split_data = data.split('#')

        if len(split_data) == 1:
            return cls(cmd=split_data[0], data=[])

        return cls(cmd=split_data[0], data=split_data[1:])


@dataclass(frozen=True)
class SendData:
    cmd: str
    data: List[str] = field(default_factory=list)

    def to_bytes(self) -> bytes:
        result = self.cmd
        for datum in self.data:
            result += f"#{datum}"

        return result.encode('utf-8')


class PacketStructure:
    HEAD_PACKET = b'$'
    TAIL_PACKET = b'$'

    @classmethod
    def to_packet(cls, data: bytes) -> bytes:
        return cls.HEAD_PACKET + data + cls.TAIL_PACKET

    @classmethod
    def from_packet(cls, packet: bytes) -> bytes:
        if not cls.is_valid(packet):
            raise ValueError(f"Packet Structure Error : {packet}")

        return packet[1:-1]

    @classmethod
    def is_valid(cls, packet: bytes) -> bool:
        if (cls.TAIL_PACKET + cls.HEAD_PACKET) in packet:
            return False

        if packet[:1] != cls.HEAD_PACKET:
            return False

        if packet[-1:] != cls.TAIL_PACKET:
            return False

        return True

    @classmethod
    def split_packet(cls, packet: bytes) -> list[bytes]:
        results = []
        for _d in packet.split(cls.HEAD_PACKET):
            if len(_d) == 0:
                continue
            results.append(cls.HEAD_PACKET+_d+cls.TAIL_PACKET)
        return results


if __name__ == "__main__":
    message = b'$abc$$def$'
    print(PacketStructure.split_packet(message))