# 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.
Mesh processing for original tesselated assets for render.
'''
__version__ = '0.3'

import logging
import bpy
import math

logger = logging.getLogger(__name__)


def setup_meshes(obj_names, cleanup=False, sharpness=False, shading=False):
    ''' Setup raw meshes list after importing '''
    logger.info('Render assets setup launched...')
    fixed_obj_names = []
    for obj_name in obj_names:
        if not bpy.data.objects.get(obj_name):
            continue
        obj = bpy.data.objects[obj_name]
        bpy.ops.object.select_all(action='DESELECT')
        obj.select_set(state=True)
        bpy.context.view_layer.objects.active = obj

        if cleanup:
            # remove doubles
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.mesh.remove_doubles(threshold=0.00001)
            bpy.ops.mesh.select_all(action='DESELECT')
            bpy.ops.mesh.select_mode(type='FACE')
            bpy.ops.mesh.select_interior_faces()
            bpy.ops.mesh.delete(type='FACE')
            bpy.ops.object.mode_set(mode='OBJECT')

        if sharpness:
            # set shaps and unwrap
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.mesh.select_all(action='DESELECT')
            bpy.ops.mesh.select_mode(type='EDGE')
            bpy.ops.mesh.edges_select_sharp(sharpness=math.radians(12))
            bpy.ops.mesh.mark_sharp()
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.uv.smart_project()
            bpy.ops.object.mode_set(mode='OBJECT')

        if shading:
            # fix shading TODO
            bpy.ops.object.shade_smooth()
            bpy.context.view_layer.objects.active.data.use_auto_smooth = 1
            bpy.context.view_layer.objects.active.modifiers.new(type='DECIMATE', name='decimate')
            bpy.context.view_layer.objects.active.modifiers['decimate'].decimate_type = 'DISSOLVE'
            bpy.context.view_layer.objects.active.modifiers['decimate'].angle_limit = 0.00872665
            bpy.context.object.modifiers['decimate'].show_expanded = 0
            bpy.context.view_layer.objects.active.modifiers.new(type='TRIANGULATE', name='triangulate')
            bpy.context.object.modifiers['triangulate'].keep_custom_normals = 1
            bpy.context.object.modifiers['triangulate'].show_expanded = 0

        fixed_obj_names.append(obj_name)

    return logger.info('Setup of %s meshes is finished!', len(fixed_obj_names))
