import os
from enum import IntEnum


class EdidFileType(IntEnum):
    BIN = 0
    HEX = 1


def save_file(data: bytearray, path: str, file_type: EdidFileType):
    if file_type == EdidFileType.BIN:
        file_dpd = open(path + '.bin', 'bw+')
        file_dpd.write(data)
        file_dpd.close()
    elif file_type == EdidFileType.HEX:
        file = open(path + '.hex', 'w')
        str_for_save = ''
        for i in data:
            if i < 16:
                str_for_save += str(hex(i)).replace("0x", "0")
            else:
                str_for_save += str(hex(i)).replace("0x", "")
        file.write(str_for_save)
        file.close()


def load_file(path: str) -> bytearray:
    filename, file_extension = os.path.splitext(path)
    if file_extension.find('.hex') != -1:
        file = open(path, 'r')
        data = file.read()
        file.close()
        i = 0
        size = len(data)
        byte_array = bytearray()
        while i < size:
            byte_array.append(int('0x' + data[i] + data[i + 1], 16))
            i += 2
        return byte_array
    elif file_extension.find('.bin') != -1:
        file = open(path, 'rb')
        data = file.read()
        file.close()
        return bytearray(data)
