import bpy
from .utils_bugreport import allowSendingBugReport, canReportABug
from .utils_icons import getPreviewIcon

class BugreporterPanel(bpy.types.Panel):
    bl_idname = "OBJECT_PT_NEEDLE_bugreport"
    bl_label = "Needle Bugreporter"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw_header(self, context):
        layout = self.layout
        icon = getPreviewIcon("logo_small_bw")
        layout.label(text="", icon_value=icon)

    def draw(self, context):
        drawBugReportUI(self, context)
      

def drawBugReportUI(self, context):
    layout = self.layout
    canReport = canReportABug()
    if not canReport:
        layout.label(text="Please create a Nodejs project (and install it) using the Needle Engine panel to report a bug")
        layout = layout.column()
    layout.enabled = canReport and allowSendingBugReport()
    layout.prop(context.scene, "NEEDLE_bugreport_contact", text="Contact")
    layout.prop(context.scene, "NEEDLE_bugreport_description", text="Description")
    sendLayout = layout.row()
    if canReport:
        if not context.scene.NEEDLE_bugreport_contact or not context.scene.NEEDLE_bugreport_description:
            layout.label(text="Please enter contact information and a description of the bug")
            sendLayout.enabled = False
        elif not bpy.data.filepath:
            layout.label(text="Please save your blend file before reporting a bug")
            sendLayout.enabled = False
        elif bpy.data.is_dirty:
            layout.label(text="Please save your blend file before reporting a bug (unsaved changes)")
            sendLayout.enabled = False
    sendLayout.operator("needle.report_bug", text="Generate & Send Bug Report")

