from typing import Optional

from clarabot_nano.core.state.update.base import AbstractStateUpdateHandler


class SetGatewayAccess(AbstractStateUpdateHandler):
    HANDLER_KEY = "set_gateway_access"

    def get_new_state_kvs(self, data: dict) -> Optional[dict]:
        if not isinstance(port := data["port"], int) or port < 1 or port >= 65536:
            raise Exception("Invalid gateway port")
        if not isinstance(key := data["key"], bytes) or len(key) < 8:
            raise Exception("Invalid gateway key")
        return {"gateway_port": port, "gateway_key": key}
