import bpy


def prefs():
    try:
        return bpy.context.preferences.addons[__package__].preferences
    except (AttributeError, KeyError):
        return None

class DREAMUV_PT_uv(bpy.types.Panel):
    """DreamUV Tools Panel"""
    bl_label = "DreamUV"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'DreamUV'

    def draw(self, context):
        addon_prefs = prefs()
        layout = self.layout
        box = layout.box()
        if bpy.context.object.mode != 'EDIT':
            box.enabled = False

        col = box.column(align=True)
        col.label(text="Viewport UV Tools")
        row = col.row(align = True)
        row.operator("view3d.dreamuv_uvtranslate", text="Move", icon="UV_SYNC_SELECT")
        row = row.row(align = True)
        row.prop(addon_prefs, 'move_snap', text="")

        row = col.row(align = True)
        op = row.operator("view3d.dreamuv_uvtranslatestep", text=" ", icon="TRIA_UP")
        op.direction = "up"
        op = row.operator("view3d.dreamuv_uvtranslatestep", text=" ", icon="TRIA_DOWN")
        op.direction = "down"
        op = row.operator("view3d.dreamuv_uvtranslatestep", text=" ", icon="TRIA_LEFT")
        op.direction = "left"
        op = row.operator("view3d.dreamuv_uvtranslatestep", text=" ", icon="TRIA_RIGHT")
        op.direction = "right"
        col.separator()

        row = col.row(align = True)
        row.operator("view3d.dreamuv_uvscale", text="Scale", icon="FULLSCREEN_ENTER")
        row = row.row(align = True)
        row.prop(addon_prefs, 'scale_snap', text="")
        row = col.row(align = True)
        op = row.operator("view3d.dreamuv_uvscalestep", text=" ", icon="ADD")
        op.direction="+XY"
        op = row.operator("view3d.dreamuv_uvscalestep", text=" ", icon="REMOVE")
        op.direction="-XY"
        op = row.operator("view3d.dreamuv_uvscalestep", text="+X")
        op.direction = "+X"
        op = row.operator("view3d.dreamuv_uvscalestep", text="-X")
        op.direction = "-X"
        op = row.operator("view3d.dreamuv_uvscalestep", text="+Y")
        op.direction = "+Y"
        op = row.operator("view3d.dreamuv_uvscalestep", text="-Y")
        op.direction = "-Y"
        col.separator()

        row = col.row(align = True)
        row.operator("view3d.dreamuv_uvrotate", text="Rotate", icon="FILE_REFRESH")
        row = row.row(align = True)
        row.prop(addon_prefs, 'rotate_snap', text="")
        row = col.row(align = True)
        op = row.operator("view3d.dreamuv_uvrotatestep", text=" ", icon="LOOP_FORWARDS")
        op.direction="forward"
        op = row.operator("view3d.dreamuv_uvrotatestep", text=" ", icon="LOOP_BACK")
        op.direction="reverse"
        col.separator()
        col.operator("view3d.dreamuv_uvextend", text="Extend", icon="MOD_TRIANGULATE")
        col.operator("view3d.dreamuv_uvstitch", text="Stitch", icon="UV_EDGESEL")
        col.operator("view3d.dreamuv_uvcycle", text="Cycle", icon="FILE_REFRESH")
        row = col.row(align = True)
        op = row.operator("view3d.dreamuv_uvmirror", text="Mirror X", icon="MOD_MIRROR")
        op.direction = "x"
        op = row.operator("view3d.dreamuv_uvmirror", text="Mirror Y")
        op.direction = "y"

        col.separator()
        row = col.row(align = True)
        op = row.operator("view3d.dreamuv_uvinsetstep", text="Inset", icon="FULLSCREEN_EXIT")
        op.direction = "in"
        op = row.operator("view3d.dreamuv_uvinsetstep", text="Expand", icon="FULLSCREEN_ENTER")
        op.direction = "out"
        row.prop(context.scene, "uvinsetpixels", text="")
        row.prop(context.scene, "uvinsettexsize", text="")

        col.label(text="Move to UV Edge")
        row = col.row(align = True)
        op = row.operator("view3d.dreamuv_uvmovetoedge", text=" ", icon="TRIA_UP_BAR")
        op.direction="up"
        op = row.operator("view3d.dreamuv_uvmovetoedge", text=" ", icon="TRIA_DOWN_BAR")
        op.direction="down"
        op = row.operator("view3d.dreamuv_uvmovetoedge", text=" ", icon="TRIA_LEFT_BAR")
        op.direction = "left"
        op = row.operator("view3d.dreamuv_uvmovetoedge", text=" ", icon="TRIA_RIGHT_BAR")
        op.direction = "right"

        box = layout.box()
        if bpy.context.object.mode != 'EDIT':
            box.enabled = False
        col = box.column(align=True)
        col.label(text="Unwrapping Tools:")
        col.operator("view3d.dreamuv_uvunwrapsquare", text="Square Fit Unwrap", icon="OUTLINER_OB_LATTICE")
        unwraptool=col.operator("uv.unwrap", text="Blender Unwrap", icon='UV')
        unwraptool.method='CONFORMAL'
        unwraptool.margin=0.001
        

        col.separator()
        box = layout.box()
        if bpy.context.object.mode != 'EDIT':
            box.enabled = False
        col = box.column(align=True)
        col.label(text="UV Transfer Tool")
        row = col.row(align = True)
        row.prop(context.scene, "uvtransferxmin", text="")
        row.prop(context.scene, "uvtransferymin", text="")
        row.prop(context.scene, "uvtransferxmax", text="")
        row.prop(context.scene, "uvtransferymax", text="")

        col.operator("view3d.dreamuv_uvtransfergrab", text="Grab UV from selection", icon="FILE_TICK")
        row = col.row(align = True)
        row.operator("view3d.dreamuv_uvtransfer", text="Transfer to selection", icon="MOD_UVPROJECT")

        # Hot spot section
        col.separator()
        box = layout.box()
        col = box.column(align=True)
        col.label(text="HotSpot Tool")
        col.separator()
        radiobutton = (
            context.scene.duv_hotspot_atlas1,
            context.scene.duv_hotspot_atlas1,
            context.scene.duv_hotspot_atlas2,
            context.scene.duv_hotspot_atlas3,
            context.scene.duv_hotspot_atlas4,
            context.scene.duv_hotspot_atlas5,
            context.scene.duv_hotspot_atlas6,
            context.scene.duv_hotspot_atlas7,
            context.scene.duv_hotspot_atlas8,
        )
        multi_material_type = (
            context.scene.duv_hotspot_atlas_multi1,
            context.scene.duv_hotspot_atlas_multi1,
            context.scene.duv_hotspot_atlas_multi2,
            context.scene.duv_hotspot_atlas_multi3,
            context.scene.duv_hotspot_atlas_multi4,
            context.scene.duv_hotspot_atlas_multi5,
            context.scene.duv_hotspot_atlas_multi6,
            context.scene.duv_hotspot_atlas_multi7,
            context.scene.duv_hotspot_atlas_multi8,
        )
        listsize = 1
        col.prop(context.scene, "atlas_list_size", text="Atlas count")
        
        
        while listsize <= context.scene.atlas_list_size:
            row = col.row(align = True)
            if radiobutton[listsize]: 
                op = row.operator("view3d.dreamuv_pushhotspot", text="", icon="RADIOBUT_ON")
            if not radiobutton[listsize]: 
                op = row.operator("view3d.dreamuv_pushhotspot", text="", icon="RADIOBUT_OFF")
            op.index = listsize
            row.prop_search(context.scene, "subrect_atlas"+str(listsize), context.scene, "objects", text="", icon="MOD_MULTIRES")
            row.prop_search(context.scene, "duv_hotspotmaterial"+str(listsize), bpy.data, "materials", text="")
            row.prop(context.scene, "duv_hotspot_atlas_multi"+str(listsize), text ="")
            if int(multi_material_type[listsize]) > 1:
                row.prop(context.scene, "duv_hotspot_atlas_index"+str(listsize), text ="")
            listsize += 1
                
        col.separator()
        row = col.row(align = True)
        row.label(text="Atlas Scale")
        row.prop(context.scene, "duvhotspotscale", text="")
        row = col.row(align = True)
        row = col.row(align = True)
        row.prop(context.scene, "duv_hotspotuseinset", icon="FULLSCREEN_EXIT", text="inset")

        row.separator()
        row.prop(context.scene, "hotspotinsetpixels", text="")
        row.prop(context.scene, "hotspotinsettexsize", text="")

        col.separator()
        row = col.row(align = True)
        row.operator("view3d.dreamuv_hotspotter", text="HotSpot", icon="SHADERFX")
        row.prop(context.scene, "duv_useorientation", icon="EVENT_W", text="")
        row.prop(context.scene, "duv_usemirrorx", icon="EVENT_X", text="")
        row.prop(context.scene, "duv_usemirrory", icon="EVENT_Y", text="")
        row.prop(context.scene, "duv_autoboxmap", icon="EVENT_B", text="")
        row.prop(context.scene, "duv_hotspot_uv1", icon="IPO_SINE", text="")
        row.prop(context.scene, "duv_hotspot_uv2", icon="IPO_QUAD", text="")

        # Trim tools section
        col.separator()
        box = layout.box()
        if bpy.context.object.mode != 'EDIT':
            box.enabled = False
        col = box.column(align=True)
        col.label(text="Trim Tool")
        row = col.row(align = True)
        
        row.label(text="Trim/Cap Atlas")
        row.prop_search(context.scene, "trim_atlas", context.scene, "objects", text="", icon="LINENUMBERS_ON")
        row = col.row(align = True)
        row.label(text="Trim index: "+str(context.scene.trim_index))
        op = row.operator("view3d.dreamuv_uvtrimnext", text=" ", icon="BACK")
        op.reverse = True
        op = row.operator("view3d.dreamuv_uvtrimnext", text=" ", icon="FORWARD")
        op.reverse = False
        
        row = col.row(align = True)
        row.label(text="Cap index: "+str(context.scene.cap_index))
        op = row.operator("view3d.dreamuv_uvcapnext", text=" ", icon="BACK")
        op.reverse = True
        op = row.operator("view3d.dreamuv_uvcapnext", text=" ", icon="FORWARD")
        op.reverse = False
        
        row = col.row(align = True)
        row.enabled = not context.scene.duv_uvtrim_randomshift
        row.prop(context.scene, "duv_uvtrim_bounds", icon="CENTER_ONLY", text="bounds")
        row.separator()
        row.prop(context.scene, "duv_uvtrim_min", text="")
        row.prop(context.scene, "duv_uvtrim_max", text="")
        
        col.separator()
        row = col.row(align = True) 
        row.operator("view3d.dreamuv_uvtrim", text="Trim", icon="SEQ_SEQUENCER")

        row.operator("view3d.dreamuv_uvcap", text="Cap", icon="MOD_BUILD")
        
        row.prop(context.scene, "duv_uvtrim_randomshift", icon="NLA_PUSHDOWN", text="")
        row.prop(context.scene, "duv_autoboxmaptrim", icon="EVENT_B", text="")
        row.prop(context.scene, "duv_trimcap_uv1", icon="IPO_SINE", text="")
        row.prop(context.scene, "duv_trimcap_uv2", icon="IPO_QUAD", text="")
        
        
        
        #boxmap
        col.separator()
        box = layout.box()
        col = box.column(align=True)
        col.label(text="Boxmapping Tool:")
        row = col.row(align = True)
        row.label(text="Box Reference:")
        row.prop_search(context.scene, "uv_box", context.scene, "objects", text="", icon="MOD_MULTIRES")
        row = col.row(align = True)
        row.operator("view3d.dreamuv_uvboxmap", text="Boxmap", icon="FILE_3D")
        row.prop(context.scene, "duv_boxmap_uv1", icon="IPO_SINE", text="")
        row.prop(context.scene, "duv_boxmap_uv2", icon="IPO_QUAD", text="")
        
               
        col.separator()
        box = layout.box()
        col = box.column(align=True)
        col.label(text="UV sets:")
        row = col.row(align = True)

        op = row.operator("view3d.dreamuv_uvcopy", text="copy uv1->2", icon="XRAY")
        op.reverse = False
        op = row.operator("view3d.dreamuv_uvcopy", text="copy uv2->1", icon="XRAY")
        op.reverse = True
        
        if bpy.context.object.type == 'MESH':
            me = bpy.context.object.data

            row = col.row()
            col = row.column()

            col.template_list("MESH_UL_uvmaps", "uvmaps", me, "uv_layers", me.uv_layers, "active_index", rows=2)
            col = row.column(align=True)
            col.operator("mesh.uv_texture_add", icon='ADD', text="")
            col.operator("mesh.uv_texture_remove", icon='REMOVE', text="")


        col.separator()

        col = self.layout.column(align = True)
        col2= self.layout.column(align = True)
        row  = col2.row(align = True)
        row.alignment = 'CENTER'
        row.label(text="Niwl Games version of the addon")
        row2 = col2.row(align = True) 
        row2.operator("wm.url_open", text="Niwl Games", icon='WORLD').url = "https://docs.niwl.games/dream_uv/"