"""
Copyright 2009-2010 Mozes, Inc.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""
import io
from typing import Any

from smpp.pdu import pdu_types
from smpp.pdu.error import PDUCorruptError


class IEncoder(object):

    def encode(self, value: Any) -> bytes:
        """Takes an object representing the type and returns a byte string"""
        raise NotImplementedError()

    def decode(self, file: io.BytesIO) -> Any:
        """Takes file stream in and returns an object representing the type"""
        raise NotImplementedError()

    def read(self, file: io.BytesIO, size: int):
        bytes_read = file.read(size)
        length = len(bytes_read)
        if length == 0:
            raise PDUCorruptError("Unexpected EOF",
                                  pdu_types.CommandStatus.ESME_RINVMSGLEN)
        if length != size:
            raise PDUCorruptError(
                "Length mismatch. Expecting %d bytes. Read %d" % (size, length),
                pdu_types.CommandStatus.ESME_RINVMSGLEN)
        return bytes_read
