from typing import OrderedDict


def load_mhd_header(filename):
    """Return an OrderedDict containing metadata loaded from an mhd file."""

    metadata = OrderedDict()

    with open(filename) as header_file:
        for line in header_file:
            (key, val) = [x.strip() for x in line.split("=")]
            if key in ['ElementSpacing', 'Offset', 'CenterOfRotation',
                       'TransformMatrix', 'ElementSize']:
                new_val = [float(s) for s in val.split()]
            elif key in ['NDims', 'ElementNumberOfChannels']:
                new_val = int(val)
            elif key in ['DimSize']:
                new_val = [int(s) for s in val.split()]
            elif key in ['BinaryData', 'BinaryDataByteOrderMSB',
                         'CompressedData']:
                # pylint: disable=simplifiable-if-statement
                if val.lower() == "true":
                    new_val = True
                else:
                    new_val = False
            else:
                new_val = val

            metadata[key] = new_val

    return metadata