import bpy
from bpy.props import EnumProperty, BoolProperty, FloatProperty, IntProperty, PointerProperty
from . import DUV_Panel
from . import DUV_UVTranslate 
from . import DUV_UVRotate 
from . import DUV_UVScale 
from . import DUV_UVExtend 
from . import DUV_UVStitch 
from . import DUV_UVTransfer 
from . import DUV_UVCycle
from . import DUV_UVCopy
from . import DUV_UVMirror 
from . import DUV_UVMoveToEdge
from . import DUV_Utils
from . import DUV_HotSpot
from . import DUV_UVProject
from . import DUV_UVUnwrap
from . import DUV_UVInset
from . import DUV_UVTrim
from . import DUV_ApplyMaterial
from . import DUV_UVBoxmap

import importlib
if 'bpy' in locals():
    importlib.reload(DUV_Panel)
    importlib.reload(DUV_UVTranslate)
    importlib.reload(DUV_UVRotate)
    importlib.reload(DUV_UVScale)
    importlib.reload(DUV_UVExtend) 
    importlib.reload(DUV_UVStitch)
    importlib.reload(DUV_UVTransfer) 
    importlib.reload(DUV_UVCycle) 
    importlib.reload(DUV_UVCopy)
    importlib.reload(DUV_UVMirror) 
    importlib.reload(DUV_UVMoveToEdge)
    importlib.reload(DUV_Utils)
    importlib.reload(DUV_HotSpot)
    importlib.reload(DUV_UVProject)
    importlib.reload(DUV_UVUnwrap)
    importlib.reload(DUV_UVInset)
    importlib.reload(DUV_UVTrim)
    importlib.reload(DUV_ApplyMaterial)
    importlib.reload(DUV_UVBoxmap)

class DUV_UVToolsPreferences(bpy.types.AddonPreferences):
    bl_idname = __name__

    pixel_snap : BoolProperty(
        name="UV Pixel Snap",
        description="Translate Pixel Snapping",
        default=False
    )

    move_snap : FloatProperty(
        name="UV Move Snap",
        description="Translate Scale Subdivision Snap Size",
        default=0.25
    )
    scale_snap : FloatProperty(
        name="UV Scale Snap",
        description="Scale Snap Size",
        default=2
    )
    rotate_snap : FloatProperty(
        name="UV Rotate Snap",
        description="Rotate Angle Snap Size",
        default=45
    )




classes = (
    DUV_UVToolsPreferences,
    DUV_Panel.DREAMUV_PT_uv,
    DUV_UVTranslate.DREAMUV_OT_uv_translate,
    DUV_UVTranslate.DREAMUV_OT_uv_translate_step,
    DUV_UVRotate.DREAMUV_OT_uv_rotate,
    DUV_UVRotate.DREAMUV_OT_uv_rotate_step,
    DUV_UVScale.DREAMUV_OT_uv_scale,
    DUV_UVScale.DREAMUV_OT_uv_scale_step,
    DUV_UVExtend.DREAMUV_OT_uv_extend,
    DUV_UVStitch.DREAMUV_OT_uv_stitch,
    DUV_UVTransfer.DREAMUV_OT_uv_transfer,
    DUV_UVTransfer.DREAMUV_OT_uv_transfer_grab,
    DUV_UVCycle.DREAMUV_OT_uv_cycle,
    DUV_UVCopy.DREAMUV_OT_uv_copy,
    DUV_UVMirror.DREAMUV_OT_uv_mirror,
    DUV_UVMoveToEdge.DREAMUV_OT_uv_move_to_edge,
    DUV_UVProject.DREAMUV_OT_uv_project,
    DUV_UVUnwrap.DREAMUV_OT_uv_unwrap_square,
    DUV_HotSpot.DREAMUV_OT_hotspotter,
    DUV_HotSpot.DREAMUV_OT_pushhotspot,
    DUV_UVInset.DREAMUV_OT_uv_inset,
    DUV_UVInset.DREAMUV_OT_uv_inset_step,
    DUV_UVTrim.DREAMUV_OT_uv_trim,
    DUV_UVTrim.DREAMUV_OT_uv_cap,
    DUV_UVTrim.DREAMUV_OT_uv_trimnext,
    DUV_UVTrim.DREAMUV_OT_uv_capnext,
    DUV_ApplyMaterial.DREAMUV_OT_apply_material,
    DUV_UVBoxmap.DREAMUV_OT_uv_boxmap,
)

def poll_material(self, material):
    return not material.is_grease_pencil

def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.Scene.subrect_atlas = bpy.props.PointerProperty (name="atlas",type=bpy.types.Object,description="atlas object")
    bpy.types.Scene.uv_box = bpy.props.PointerProperty (name="uvbox",type=bpy.types.Object,description="uv box")
    bpy.types.Scene.trim_atlas = bpy.props.PointerProperty (name="trim_atlas",type=bpy.types.Object,description="trim atlas")
    bpy.types.Scene.trim_index = bpy.props.IntProperty (name = "trim_index",default = 0,description = "trim index")
    bpy.types.Scene.cap_index = bpy.props.IntProperty (name = "cap_index",default = 0,description = "cap index")
    bpy.types.Scene.duv_trimuseinset = bpy.props.BoolProperty (name = "duv_trimuseinset",default = False,description = "Use inset when trimming")
    bpy.types.Scene.uvinsetpixels = bpy.props.FloatProperty (name = "uv inset pixel amount",default = 1.0,description = "")
    bpy.types.Scene.uvinsettexsize = bpy.props.FloatProperty (name = "uv inset texture size",default = 1024.0,description = "")
    bpy.types.Scene.uvtransferxmin = bpy.props.FloatProperty (name = "uvtransferxmin",default = 0.0,description = "uv left bottom corner X")
    bpy.types.Scene.uvtransferymin = bpy.props.FloatProperty (name = "uvtransferymin",default = 0.0,description = "uv left bottom corner Y")
    bpy.types.Scene.uvtransferxmax = bpy.props.FloatProperty (name = "uvtransferxmax",default = 1.0,description = "uv right top corner X")
    bpy.types.Scene.uvtransferymax = bpy.props.FloatProperty (name = "uvtransferymax",default = 1.0,description = "uv right top corner Y")
    bpy.types.Scene.duv_useorientation = bpy.props.BoolProperty (name = "duv_useorientation",default = False,description = "Align UVs with world orientation")
    bpy.types.Scene.duv_usemirrorx = bpy.props.BoolProperty (name = "duv_usemirrorx",default = True,description = "Randomly mirror faces on the x-axis")
    bpy.types.Scene.duv_usemirrory = bpy.props.BoolProperty (name = "duv_usemirrory",default = True,description = "Randomly mirror faces on the y-axis")
    bpy.types.Scene.duvhotspotscale = bpy.props.FloatProperty (name = "duvhotspotscale",default = 1.0,description = "Hotspotting scale multiplier")
    bpy.types.Scene.duv_hotspotmaterial = bpy.props.PointerProperty (name="duv_hotspotmaterial",type=bpy.types.Material,poll=poll_material,description="Hotspot material")
    bpy.types.Scene.duv_hotspotuseinset = bpy.props.BoolProperty (name = "duv_hotspotuseinset",default = False,description = "Use inset when hotspotting")
    bpy.types.Scene.hotspotinsetpixels = bpy.props.FloatProperty (name = "hotspot inset pixel amount",default = 1.0,description = "")
    bpy.types.Scene.hotspotinsettexsize = bpy.props.FloatProperty (name = "hotspot texture size",default = 1024.0,description = "")
    bpy.types.Scene.duv_experimentaltools = bpy.props.BoolProperty (name = "duv_experimentaltools",default = False,description = "Show experimental tools")
    bpy.types.Scene.duv_uv2copy = bpy.props.BoolProperty (name = "duv_uv2copy",default = False,description = "Copy uv2")
    bpy.types.Scene.duv_hotspot_uv1 = bpy.props.BoolProperty (name = "duv_hotspot_uv1",default = False,description = "Always hotspot UV 1")
    bpy.types.Scene.duv_hotspot_uv2 = bpy.props.BoolProperty (name = "duv_hotspot_uv2",default = False,description = "Always hotspot UV 2")
    bpy.types.Scene.duv_boxmap_uv1 = bpy.props.BoolProperty (name = "duv_boxmap_uv1",default = False,description = "Always box map UV 1")
    bpy.types.Scene.duv_boxmap_uv2 = bpy.props.BoolProperty (name = "duv_boxmap_uv2",default = False,description = "Always box map UV 2")
    bpy.types.Scene.duv_autoboxmap = bpy.props.BoolProperty (name = "duv_autoboxmap",default = False,description = "Auto apply boxmap after hotspot operation")
    bpy.types.Scene.duv_trimcap_uv1 = bpy.props.BoolProperty (name = "duv_trimcap_uv1",default = False,description = "Always apply trim/cap to UV 1")
    bpy.types.Scene.duv_trimcap_uv2 = bpy.props.BoolProperty (name = "duv_trimcap_uv2",default = False,description = "Always apply trim/cap to UV 2")
    bpy.types.Scene.duv_autoboxmaptrim = bpy.props.BoolProperty (name = "duv_autoboxmaptrim",default = False,description = "Auto apply boxmap after trim/cap operation")
    bpy.types.Scene.duv_uvtrim_randomshift = bpy.props.BoolProperty (name = "duv_uvtrim_randomshift",default = False,description = "Randomize trim position along tiling axis")
    bpy.types.Scene.duv_uvtrim_bounds = bpy.props.BoolProperty (name = "duv_uvtrim_bounds",default = False,description = "Scale trim to boundary region")
    bpy.types.Scene.duv_uvtrim_min = bpy.props.FloatProperty (name = "duv_uvtrim_min",default = 0.0,description = "Boundary start")
    bpy.types.Scene.duv_uvtrim_max = bpy.props.FloatProperty (name = "duv_uvtrim_max",default = 1.0,description = "Boundary end")
    
    bpy.types.Scene.subrect_atlas1 = bpy.props.PointerProperty (name="atlas1",type=bpy.types.Object,description="atlas1")
    bpy.types.Scene.subrect_atlas2 = bpy.props.PointerProperty (name="atlas2",type=bpy.types.Object,description="atlas2")
    bpy.types.Scene.subrect_atlas3 = bpy.props.PointerProperty (name="atlas3",type=bpy.types.Object,description="atlas3")
    bpy.types.Scene.subrect_atlas4 = bpy.props.PointerProperty (name="atlas4",type=bpy.types.Object,description="atlas4")
    bpy.types.Scene.subrect_atlas5 = bpy.props.PointerProperty (name="atlas5",type=bpy.types.Object,description="atlas5")
    bpy.types.Scene.subrect_atlas6 = bpy.props.PointerProperty (name="atlas6",type=bpy.types.Object,description="atlas6")
    bpy.types.Scene.subrect_atlas7 = bpy.props.PointerProperty (name="atlas7",type=bpy.types.Object,description="atlas7")
    bpy.types.Scene.subrect_atlas8 = bpy.props.PointerProperty (name="atlas8",type=bpy.types.Object,description="atlas8")

    bpy.types.Scene.duv_hotspotmaterial1 = bpy.props.PointerProperty (name="duv_hotspotmaterial1",type=bpy.types.Material,poll=poll_material,description="Hotspot material 1")
    bpy.types.Scene.duv_hotspotmaterial2 = bpy.props.PointerProperty (name="duv_hotspotmaterial2",type=bpy.types.Material,poll=poll_material,description="Hotspot material 2")
    bpy.types.Scene.duv_hotspotmaterial3 = bpy.props.PointerProperty (name="duv_hotspotmaterial3",type=bpy.types.Material,poll=poll_material,description="Hotspot material 3")
    bpy.types.Scene.duv_hotspotmaterial4 = bpy.props.PointerProperty (name="duv_hotspotmaterial4",type=bpy.types.Material,poll=poll_material,description="Hotspot material 4")
    bpy.types.Scene.duv_hotspotmaterial5 = bpy.props.PointerProperty (name="duv_hotspotmaterial5",type=bpy.types.Material,poll=poll_material,description="Hotspot material 5")
    bpy.types.Scene.duv_hotspotmaterial6 = bpy.props.PointerProperty (name="duv_hotspotmaterial6",type=bpy.types.Material,poll=poll_material,description="Hotspot material 6")
    bpy.types.Scene.duv_hotspotmaterial7 = bpy.props.PointerProperty (name="duv_hotspotmaterial7",type=bpy.types.Material,poll=poll_material,description="Hotspot material 7")
    bpy.types.Scene.duv_hotspotmaterial8 = bpy.props.PointerProperty (name="duv_hotspotmaterial8",type=bpy.types.Material,poll=poll_material,description="Hotspot material 8")
    
    bpy.types.Scene.duv_hotspot_atlas1 = bpy.props.BoolProperty (name = "duv_hotspot_atlas1",default = True,description = "duv_hotspot_atlas1")
    bpy.types.Scene.duv_hotspot_atlas2 = bpy.props.BoolProperty (name = "duv_hotspot_atlas2",default = False,description = "duv_hotspot_atlas2")
    bpy.types.Scene.duv_hotspot_atlas3 = bpy.props.BoolProperty (name = "duv_hotspot_atlas3",default = False,description = "duv_hotspot_atlas3")
    bpy.types.Scene.duv_hotspot_atlas4 = bpy.props.BoolProperty (name = "duv_hotspot_atlas4",default = False,description = "duv_hotspot_atlas4")
    bpy.types.Scene.duv_hotspot_atlas5 = bpy.props.BoolProperty (name = "duv_hotspot_atlas5",default = False,description = "duv_hotspot_atlas5")
    bpy.types.Scene.duv_hotspot_atlas6 = bpy.props.BoolProperty (name = "duv_hotspot_atlas6",default = False,description = "duv_hotspot_atlas6")
    bpy.types.Scene.duv_hotspot_atlas7 = bpy.props.BoolProperty (name = "duv_hotspot_atlas7",default = False,description = "duv_hotspot_atlas7")
    bpy.types.Scene.duv_hotspot_atlas8 = bpy.props.BoolProperty (name = "duv_hotspot_atlas8",default = False,description = "duv_hotspot_atlas8")


    MULTI_MAT_CONFIGS = [
        ("1", "One material", "The textureset has only one material"),
        ("4", "Four materials", "The textureset has four materials"),
    ]

    bpy.types.Scene.duv_hotspot_atlas_multi = bpy.props.EnumProperty (name = "duv_hotspot_atlas_multi",default = "1", items = MULTI_MAT_CONFIGS, description = "Select an option from the dropdown")

    bpy.types.Scene.duv_hotspot_atlas_multi1 = bpy.props.EnumProperty (name = "duv_hotspot_atlas_multi1",default = "1", items = MULTI_MAT_CONFIGS, description = "Select an option from the dropdown")
    bpy.types.Scene.duv_hotspot_atlas_multi2 = bpy.props.EnumProperty (name = "duv_hotspot_atlas_multi2",default = "1", items = MULTI_MAT_CONFIGS, description = "Select an option from the dropdown")
    bpy.types.Scene.duv_hotspot_atlas_multi3 = bpy.props.EnumProperty (name = "duv_hotspot_atlas_multi3",default = "1", items = MULTI_MAT_CONFIGS, description = "Select an option from the dropdown")
    bpy.types.Scene.duv_hotspot_atlas_multi4 = bpy.props.EnumProperty (name = "duv_hotspot_atlas_multi4",default = "1", items = MULTI_MAT_CONFIGS, description = "Select an option from the dropdown")
    bpy.types.Scene.duv_hotspot_atlas_multi5 = bpy.props.EnumProperty (name = "duv_hotspot_atlas_multi5",default = "1", items = MULTI_MAT_CONFIGS, description = "Select an option from the dropdown")
    bpy.types.Scene.duv_hotspot_atlas_multi6 = bpy.props.EnumProperty (name = "duv_hotspot_atlas_multi6",default = "1", items = MULTI_MAT_CONFIGS, description = "Select an option from the dropdown")
    bpy.types.Scene.duv_hotspot_atlas_multi7 = bpy.props.EnumProperty (name = "duv_hotspot_atlas_multi7",default = "1", items = MULTI_MAT_CONFIGS, description = "Select an option from the dropdown")
    bpy.types.Scene.duv_hotspot_atlas_multi8 = bpy.props.EnumProperty (name = "duv_hotspot_atlas_multi8",default = "1", items = MULTI_MAT_CONFIGS, description = "Select an option from the dropdown")

    bpy.types.Scene.duv_hotspot_atlas_index = bpy.props.IntProperty (name = "duv_hotspot_atlas_index",default = 1, min = 1, max = 4, description = "Select the textureset")

    bpy.types.Scene.duv_hotspot_atlas_index1 = bpy.props.IntProperty (name = "duv_hotspot_atlas_index1",default = 1, min = 1, max = 4, description = "Select the textureset by index, following X and Y directions")
    bpy.types.Scene.duv_hotspot_atlas_index2 = bpy.props.IntProperty (name = "duv_hotspot_atlas_index2",default = 1, min = 1, max = 4, description = "Select the textureset by index, following X and Y directions")
    bpy.types.Scene.duv_hotspot_atlas_index3 = bpy.props.IntProperty (name = "duv_hotspot_atlas_index3",default = 1, min = 1, max = 4, description = "Select the textureset by index, following X and Y directions")
    bpy.types.Scene.duv_hotspot_atlas_index4 = bpy.props.IntProperty (name = "duv_hotspot_atlas_index4",default = 1, min = 1, max = 4, description = "Select the textureset by index, following X and Y directions")
    bpy.types.Scene.duv_hotspot_atlas_index5 = bpy.props.IntProperty (name = "duv_hotspot_atlas_index5",default = 1, min = 1, max = 4, description = "Select the textureset by index, following X and Y directions")
    bpy.types.Scene.duv_hotspot_atlas_index6 = bpy.props.IntProperty (name = "duv_hotspot_atlas_index6",default = 1, min = 1, max = 4, description = "Select the textureset by index, following X and Y directions")
    bpy.types.Scene.duv_hotspot_atlas_index7 = bpy.props.IntProperty (name = "duv_hotspot_atlas_index7",default = 1, min = 1, max = 4, description = "Select the textureset by index, following X and Y directions")
    bpy.types.Scene.duv_hotspot_atlas_index8 = bpy.props.IntProperty (name = "duv_hotspot_atlas_index8",default = 1, min = 1, max = 4, description = "Select the textureset by index, following X and Y directions")

    bpy.types.Scene.atlas_list_size = bpy.props.IntProperty (name = "atlas_list_size", default = 1, min = 1, max = 8, description = "atlas_list_size",)
    

def unregister():
    for cls in reversed(classes):
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()
