import os
import bpy
import product_gen.generate_lamp_base
import product_gen.generate_shades as gs

def generate_models(shape_parameters):
    
    # Shade
    print(shape_parameters["shape"])
    if shape_parameters["shape"] == "mesh":
        shade = gs.generate_mesh_shade(shape_parameters)
    elif shape_parameters["shape"] == "led" or shape_parameters["shape"] == "bio":
        shade = gs.generate_led_shade(shape_parameters)      
    else: # default pendant
        print("doing pendant")
        shade = gs.generate_pendant_shade(shape_parameters)   
        
    # Base
    if "bio" not in shape_parameters["shape"]:
        radius1 = shape_parameters["fixture_radius"]
        radius2 = 0.07
        depth = shape_parameters["fixture_length"]
        location = (0.0, 0.0, depth/2.0)
        base = product_gen.generate_lamp_base.generate_cone_base(radius1, 
                                                                 radius2, 
                                                                 depth,
                                                                 location)
        # Chord
        radius = 0.05
        height = 6.0
        location = (0.0, 0.0, height/2.0)
        chord = product_gen.generate_lamp_base.generate_chord(height, 
                                                              radius, 
                                                              location)
        models = {shade.name: shade,
                  chord.name: chord,
                  base.name: base}
    else:
        models = {shade.name: shade}        
    return models    

def optimize_for_render(models):
    
    # Smooth shapes
    for eachModel in models.values():
        bpy.context.scene.objects.active = eachModel
        for poly in bpy.context.object.data.polygons:
            poly.use_smooth = True
            
def apply_textures(colormap, models):
    
    for eachName in colormap.keys():
        
        if "shade" in eachName:
            mat = bpy.data.materials.get("shade-material")
            if mat is None:
                mat = bpy.data.materials.new("shade-material")
            mat.use_nodes = True
            mat.node_tree.nodes.new('ShaderNodeBsdfGlossy')
            inp = mat.node_tree.nodes["Material Output"].inputs["Surface"]
            outp = mat.node_tree.nodes["Glossy BSDF"].outputs["BSDF"]
            mat.node_tree.nodes['Glossy BSDF'].inputs[0].default_value = (colormap[eachName][0],
                                                                         colormap[eachName][1],
                                                                         colormap[eachName][2]
                                                                         ,1)
            mat.node_tree.links.new(inp, outp)
            
            bpy.context.scene.objects.active = bpy.data.objects[eachName]
            if bpy.context.active_object.data.materials:
                # assign to 1st material slot
                bpy.context.active_object.data.materials[0] = mat
            else:
                # no slots
                bpy.context.active_object.data.materials.append(mat)             
            
        else:
            mat = bpy.data.materials.get(eachName + "-material")
            if mat is None:
                mat = bpy.data.materials.new(name=eachName + "-material")
            mat.diffuse_color = colormap[eachName]
            mat.specular_color = colormap[eachName]
            mat.diffuse_intensity = 1.0
            mat.specular_intensity = 1.0
                
            bpy.context.scene.objects.active = models[eachName]
            if bpy.context.active_object.data.materials:
                # assign to 1st material slot
                bpy.context.active_object.data.materials[0] = mat
            else:
                # no slots
                bpy.context.active_object.data.materials.append(mat)  