# coding: utf-8
# Copyright (C) 2023 Ilia Kurochkin <brothermechanic@yandex.com>
#
# This program 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.
#
'''
DESCRIPTION.
Import from json exported FreeCAD's asm4 coordinates as Blender's empty object.

DEPRECATED

'''
__version__ = '0.2'

import logging
import bpy
import json

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


def lcs_json_importer(path_json):
    ''' Import json LCS as Bledner's Empty object. '''
    with open(path_json) as f:
        data = json.load(f)

    lcs_name = data['label']
    lcs_parent_name = data['parent_label']
    lcs_pose = data['placement']
    loc = tuple(lcs_pose['position'].values())
    fori = tuple(lcs_pose['orientation'].values())
    bori = (fori[3],)+fori[:3]

    if not bpy.data.collections.get('Import LCS'):
        lcs_collection = bpy.data.collections.new('Import LCS')
        bpy.context.scene.collection.children.link(lcs_collection)
        bpy.context.view_layer.active_layer_collection = \
            bpy.context.view_layer.layer_collection.children['Import LCS']
    else:
        lcs_collection = bpy.data.collections['Import LCS']

    bpy.ops.object.empty_add(
        type='ARROWS', radius=0.1, align='WORLD',
        location=(0, 0, 0), rotation=(0, 0, 0))
    lcs_obj = bpy.context.active_object  # or bpy.context.object
    lcs_obj.name = lcs_name
    lcs_obj.rotation_mode = 'QUATERNION'
    lcs_obj.location = loc
    lcs_obj.rotation_quaternion = bori
    lcs_obj.rotation_mode = 'XYZ'
    lcs_obj.show_in_front = True

    if lcs_parent_name:
        lcs_obj.parent = bpy.data.objects[lcs_parent_name]

    f.close()
    logger.info('Point %s imported without errors', lcs_name)
    return lcs_obj
