#  ***** BEGIN GPL LICENSE BLOCK *****
#
#   Copyright (C) 2024 Ilia Kurochkin <brothermechanic@yandex.com>
#
#   Created by Ilia Kurochkin (brothermechanic)
#   contact:    brothermechanic@yandex.com
#
#   This is free software;
#   you can redistribute it and/or modify
#   it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version 3
#   of the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, see <https://www.gnu.org/licenses/>.
#
#  ***** END GPL LICENSE BLOCK *****
#
# coding: utf-8
'''
DESCRIPTION.
Various collection tools.
'''

__version__ = '0.1'

import bpy
from collections import defaultdict


def copy_objects(from_col, to_col, linked, double_lut):
    '''Function copying objects from collection to collection.'''
    for obj in from_col.objects:
        double = obj.copy()
        if not linked and obj.data:
            double.data = double.data.copy()
        to_col.objects.link(double)
        double_lut[obj] = double
    return True


def copy_collections_recursive(collection, suffix='copy', linked=False):
    '''Function recursive copying collection.'''
    double_lut = defaultdict(lambda: None)
    parent = [p for p in (bpy.data.collections[:] + [bpy.context.scene.collection])
              if collection.name in p.children.keys()][0]

    def _copy(parent, collection, suffix, linked=False):
        '''Function copying collection.'''
        clone_collection = bpy.data.collections.new(
            '_'.join((collection.name, suffix))
        )
        copy_objects(collection, clone_collection, linked, double_lut)

        for _collection in collection.children:
            _copy(clone_collection, _collection, suffix, linked)

        parent.children.link(clone_collection)

    _copy(parent, collection, suffix, linked)
    for obj, double in tuple(double_lut.items()):
        parent = double_lut[obj.parent]
        if parent:
            double.parent = parent
    return '_'.join((collection.name, suffix))


def unlink_from_collections(obj):
    ''' Unlinking object from all collections. '''
    for col in bpy.data.collections:
        if obj.name in col.objects:
            col.objects.unlink(obj)
    return obj


def remove_collections_with_objects(collection=None):
    '''Removes all collection (or given) with objects from scene '''
    if collection:
        for obj in collection.objects:
            bpy.data.objects.remove(obj, do_unlink=True)
        bpy.data.collections.remove(collection)
    else:
        for col in bpy.data.collections:
            for obj in col.objects:
                bpy.data.objects.remove(obj, do_unlink=True)
            bpy.data.collections.remove(col)
    return True
