# -*- coding: utf-8 -*-
"""
DESCRIPTION.
Subassembly models setup from SDFormat file.
Script selected subassembly models.
Support Blender compiled as a Python Module only!

DEPRECATED

"""
__version__ = "0.1"

import logging
import sys
import xml.etree.ElementTree as ET
import os
sys.path.append('../')
from mathutils import Matrix
import bpy

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def sdf_mesh_selector(sdf_path):
    """ Setup scene by SDFormat file description """
    mytree = ET.parse(sdf_path)
    myroot = mytree.getroot()

    models_transforms = {}
    for models in myroot.iter('model'):
        for models_childs in models:
            if models_childs.find('pose') is not None:
                pose = models_childs.find('pose').text
                logger.debug(models.attrib['name'], pose)
                pose_vectors = [float(x) * 0.001 for x in pose.split()]
                models_transforms[models.attrib['name']] = pose_vectors

    # TODO More cleanup and optimise
    # TODO add models <==> objects compare validation

    for obj in bpy.context.scene.objects:
        if obj.name in models_transforms:
            obj.matrix_world = Matrix()
        else:
            bpy.data.objects.remove(bpy.data.objects[obj.name], do_unlink=True)
            logger.info(obj.name, '- is not in assemly objects. Deleted!')
