import os
from typing import Optional

import dolomite_base as dl
import h5py
from biocutils import combine_sequences
from compressed_lists import CompressedList


@dl.save_object.register
@dl.validate_saves
def save_atomic_vector_list(x: CompressedList, path: str, **kwargs):
    """Method for saving :py:class:`~CompressedList`
    objects to their corresponding file representations, see
    :py:meth:`~dolomite_base.save_object.save_object` for details.

    Args:
        x:
            Object to be staged.

        path:
            Path to a directory in which to save ``x``.

        data_frame_args:
            Further arguments to pass to the ``save_object`` method for
            ``mcols``.

        kwargs:
            Further arguments to be passed to individual methods.

    Returns:
        `x` is saved to `path`.
    """
    return _save_compressed_list(x, path=path, name="atomic_vector_list", **kwargs)


def _save_compressed_list(x, path, name, **kwargs):
    os.mkdir(path)

    _all_ranges = x.get_unlist_data()
    if isinstance(_all_ranges, list) and len(_all_ranges) > 1:
        _all_ranges = combine_sequences(*x.get_unlist_data())

    dl.alt_save_object(_all_ranges, path=os.path.join(path, "concatenated"), **kwargs)

    _elem_annotation = x.get_element_metadata()
    if _elem_annotation is not None:
        dl.alt_save_object(_elem_annotation, path=os.path.join(path, "element_annotations"), **kwargs)

    _meta = x.get_metadata()
    if _meta is not None and len(_meta) > 0:
        dl.alt_save_object(_meta, path=os.path.join(path, "other_annotations"), **kwargs)

    with h5py.File(os.path.join(path, "partitions.h5"), "w") as handle:
        ghandle = handle.create_group(name)

        dl.write_integer_vector_to_hdf5(ghandle, name="lengths", h5type="u4", x=x.get_element_lengths())

        if x.get_names() is not None:
            dl.write_string_vector_to_hdf5(ghandle, name="names", x=x.get_names())

    _info = {}
    _info[name] = {"version": "1.0"}
    dl.save_object_file(path, name, _info)

    return
