# Copyright (c) 2015 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher.

from UM.View.View import View
from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
from UM.Resources import Resources
from UM.Application import Application
from UM.Preferences import Preferences
from UM.View.Renderer import Renderer

from UM.View.GL.OpenGL import OpenGL

from cura.ExtrudersModel import ExtrudersModel

import math

## Standard view for mesh models.
class SolidView(View):
    def __init__(self):
        super().__init__()

        Preferences.getInstance().addPreference("view/show_overhang", True)

        self._enabled_shader = None
        self._disabled_shader = None

        self._extruders_model = ExtrudersModel()

    def beginRendering(self):
        scene = self.getController().getScene()
        renderer = self.getRenderer()

        if not self._enabled_shader:
            self._enabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))

        if not self._disabled_shader:
            self._disabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
            self._disabled_shader.setUniformValue("u_diffuseColor", [0.68, 0.68, 0.68, 1.0])
            self._disabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0)))

        if Application.getInstance().getGlobalContainerStack():
            if Preferences.getInstance().getValue("view/show_overhang"):
                angle = Application.getInstance().getGlobalContainerStack().getProperty("support_angle", "value")
                if angle is not None:
                    self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(90 - angle)))
                else:
                    self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0))) #Overhang angle of 0 causes no area at all to be marked as overhang.
            else:
                self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0)))

        for node in DepthFirstIterator(scene.getRoot()):
            if not node.render(renderer):
                if node.getMeshData() and node.isVisible():
                    # TODO: Find a better way to handle this
                    #if node.getBoundingBoxMesh():
                    #    renderer.queueNode(scene.getRoot(), mesh = node.getBoundingBoxMesh(),mode = Renderer.RenderLines)

                    uniforms = {}
                    if self._extruders_model.rowCount() > 0:
                        # Get color to render this mesh in from ExtrudersModel
                        extruder_index = 0
                        extruder_id = node.callDecoration("getActiveExtruder")
                        if extruder_id:
                            extruder_index = self._extruders_model.find("id", extruder_id)
                        if extruder_index:
                            extruder_color = self._extruders_model.getItem(extruder_index)["color"]

                            try:
                                # Colors are passed as rgb hex strings (eg "#ffffff"), and the shader needs
                                # an rgba list of floats (eg [1.0, 1.0, 1.0, 1.0])
                                uniforms["diffuse_color"] = [
                                    int(extruder_color[1:3], 16) / 255,
                                    int(extruder_color[3:5], 16) / 255,
                                    int(extruder_color[5:7], 16) / 255,
                                    1.0
                                ]
                            except:
                                pass

                    if hasattr(node, "_outside_buildarea"):
                        if node._outside_buildarea:
                            renderer.queueNode(node, shader = self._disabled_shader)
                        else:
                            renderer.queueNode(node, shader = self._enabled_shader, uniforms = uniforms)
                    else:
                        renderer.queueNode(node, material = self._enabled_shader, uniforms = uniforms)
                if node.callDecoration("isGroup"):
                    renderer.queueNode(scene.getRoot(), mesh = node.getBoundingBoxMesh(), mode = Renderer.RenderLines)

    def endRendering(self):
        pass

    #def _onPreferenceChanged(self, preference):
        #if preference == "view/show_overhang": ## Todo: This a printer only setting. Should be removed from Uranium.
            #self._enabled_material = None

@@ -2887,16 +2887,23 @@
 _index =
+ max(0,
  self._e
@@ -2944,51 +2944,10 @@
 _id)
+)
 %0A
-                        if extruder_index:
 %0A   
@@ -2959,36 +2959,32 @@
                 
-    
 extruder_color =
@@ -3039,21 +3039,16 @@
 color%22%5D%0A
-%0A    
         
@@ -3068,20 +3068,16 @@
    try:%0A
-    
         
@@ -3200,20 +3200,16 @@
         
-    
 # an rgb
@@ -3271,36 +3271,32 @@
                 
-    
 uniforms%5B%22diffus
@@ -3309,20 +3309,16 @@
 r%22%5D = %5B%0A
-    
         
@@ -3401,36 +3401,32 @@
                 
-    
 int(extruder_col
@@ -3437,36 +3437,32 @@
 :5%5D, 16) / 255,%0A
-    
                 
@@ -3505,36 +3505,32 @@
 :7%5D, 16) / 255,%0A
-    
                 
@@ -3581,18 +3581,10 @@
     
-    %5D%0A    
+%5D%0A
     
@@ -3611,20 +3611,16 @@
 except:%0A
-    
         

Fix extruder color when no extruder_nr has been added to an object