# Path: core/ezdxf/tools/c23.py
# PY3 = sys.version_info.major > 2
# 
# Path: core/ezdxf/lldxf/tags.py
# class CompressedTags(object):
#     """Store multiple tags, compressed by zlib, as one DXFTag(code, value). value is a CompressedString() object.
#     """
#     def __init__(self, code, tags):
#         self.code = code
#         self.value = CompressedString("".join(strtag2(tag) for tag in tags))
# 
#     def __getitem__(self, item):
#         if item == 0:
#             return self.code
#         elif item == 1:
#             return self.value
#         else:
#             raise IndexError
# 
#     def decompress(self):
#         return Tags.from_text(self.value.decompress())
# 
#     def write(self, stream):
#         stream.write(self.value.decompress())


from .c23 import PY3
from ..lldxf.tags import CompressedTags
from array import array


def _collect_compress_tasks(tags):
    compress_tasks = []
    tag_collector = []
    collect_code = None
    for index, tag in enumerate(tags):
        if collect_code is not None:
            if tag.code == collect_code:
                tag_collector.append(tag)
                tags[index] = None
            else:
                compress_tasks.append((collect_code, tag_collector))
                collect_code = None
                tag_collector = []  # new list is essential, because old list is stored in compress_tasks
        if collect_code is None and tag.code in BINARY_DATA_CODES:
            collect_code = tag.code
            tag_collector.append(tag)
            tags[index] = "COMPRESSED_{}".format(len(compress_tasks))  # mark insert position
    if len(tag_collector):
        compress_tasks.append((collect_code, tag_collector))
    return compress_tasks


def _execute_compress_tasks(tags, tasks):
    if not len(tasks):
        return
    for num, task in enumerate(tasks):
        code, binary_tags = task
        if len(binary_tags):

# Path: core/ezdxf/tools/c23.py
# PY3 = sys.version_info.major > 2
# 
# Path: core/ezdxf/lldxf/tags.py
# class CompressedTags(object):
#     """Store multiple tags, compressed by zlib, as one DXFTag(code, value). value is a CompressedString() object.
#     """
#     def __init__(self, code, tags):
#         self.code = code
#         self.value = CompressedString("".join(strtag2(tag) for tag in tags))
# 
#     def __getitem__(self, item):
#         if item == 0:
#             return self.code
#         elif item == 1:
#             return self.value
#         else:
#             raise IndexError
# 
#     def decompress(self):
#         return Tags.from_text(self.value.decompress())
# 
#     def write(self, stream):
#         stream.write(self.value.decompress())


# Path: core/ezdxf/tools/binarydata.py
from .c23 import PY3
from ..lldxf.tags import CompressedTags
from array import array



def _collect_compress_tasks(tags):
    compress_tasks = []
    tag_collector = []
    collect_code = None
    for index, tag in enumerate(tags):
        if collect_code is not None:
            if tag.code == collect_code:
                tag_collector.append(tag)
                tags[index] = None
            else:
                compress_tasks.append((collect_code, tag_collector))
                collect_code = None
                tag_collector = []  # new list is essential, because old list is stored in compress_tasks
        if collect_code is None and tag.code in BINARY_DATA_CODES:
            collect_code = tag.code
            tag_collector.append(tag)
            tags[index] = "COMPRESSED_{}".format(len(compress_tasks))  # mark insert position
    if len(tag_collector):
        compress_tasks.append((collect_code, tag_collector))
    return compress_tasks


def _execute_compress_tasks(tags, tasks):
    if not len(tasks):
        return
    for num, task in enumerate(tasks):
        code, binary_tags = task
        if len(binary_tags):

            compressed_tag = CompressedTags(code, binary_tags)