"""
K6GTE, CAT interface abstraction
Email: michael.bridak@gmail.com
GPL V3
"""

import logging

if __name__ == "__main__":
    print("I'm not the program you are looking for.")

logger = logging.getLogger("cat_interface")


class CAT:
    """CAT control base class"""

    def __init__(self, host: str, port: int) -> None:
        """
        Computer Aided Transceiver abstraction class.
        Offers a normalized interface; this is the base class.

        Takes 2 inputs to setup the class.

        A string defining the host, example: 'localhost' or '127.0.0.1'

        An integer defining the network port used.
        Commonly 12345 for flrig, or 4532 for rigctld.

        Exposed methods are:

        reinit()
        get_vfo() set_vfo()
        get_mode() set_mode()
        get_power() set_power()
        get_ptt() ptt_on() ptt_off()
        sendcw() stopcw() set_cw_speed() set_cw_send()

        A variable 'online' is set to True if no error was encountered,
        otherwise False.
        """
        self.interface = ""
        self.host = host
        self.port = port
        self.online = False

    def reinit(self):
        """reinitialise rigctl"""

    def sendvoicememory(self, memoryspot=1):
        """..."""

    def sendcw(self, texttosend):
        """..."""

    def stopcw(self):
        """..."""

    def set_cw_speed(self, speed: int) -> None:
        """Set the CW speed in wpm"""

    def set_cw_send(self, send: bool) -> None:
        """Enable or disable the radio's CW keyer send flag."""

    def get_vfo(self) -> str:
        """Poll the radio for current vfo using the interface"""

    def get_mode(self) -> str:
        """Returns the current mode filter width of the radio"""

    def get_bw(self):
        """Get current vfo bandwidth"""

    def get_power(self):
        """Get power level from rig"""

    def get_ptt(self):
        """Get PTT state"""

    def get_mode_list(self):
        "Get a list of modes supported by the radio"

    def set_vfo(self, freq: str):
        """Sets the radios VFO. Defaults to VFOA."""

    def set_sync_vfos(self, sync_vfos: bool) -> bool:
        """Turn Sync VFOs on/off"""

    def set_mode(self, mode: str) -> bool:
        """Sets the radios mode"""

    def set_power(self, power):
        """Sets the radios power"""

    def ptt_on(self):
        """turn ptt on/off"""

    def ptt_off(self):
        """turn ptt on/off"""

    def send_cat_string(self, cmdstr=""):
        """send a raw cat string to radio"""

    @staticmethod
    def _parse_cat_command(cmdstr: str) -> tuple[str, bool, bool]:
        """Normalize an `RI:` macro payload into the form each CAT backend expects.

        Returns (cmd, ishex, ok):
        - cmd == "" and ok is True  -> whitespace input, nothing to send
        - ok is False                   -> malformed; caller drops and logs
        - ok is True, ishex is True     -> cmd holds hex bytes, space-delimited,
                                           upper-case, every 3rd char is a space
        - ok is True, ishex is False    -> cmd is ASCII text, returned as-is
        """
        cmdstr = cmdstr.strip()
        if not cmdstr:
            return "", False, True

        ishex = any(tok in cmdstr for tok in (" ", " x", " X", "\\"))
        if not ishex:
            return cmdstr, False, True

        cmd = cmdstr.replace("x", "").replace("X", "").replace("\\", "").upper()
        if any(c not in "0123456789ABCDEF " for c in cmd):
            logger.error("Bad char in RI: command string: [%s]", cmdstr)
            return "", True, False
        for i in range(2, len(cmd), 3):
            if cmd[i] != " ":
                logger.error("Bad delimiters in RI: cmd string: [%s]", cmdstr)
                return "", True, False
        return cmd, True, True
