
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
import blf
from pathlib import Path
from .constants import PICKERS
from .core.picker import Picker, PickerGroup, load_picker_data
import json


def draw_rect_2d(position, width, height, color):
    """
    Draw a 2d rectangele.

    :arg position: Position of the lower left corner.
    :type position: 2D Vector
    :arg width: Width of the rect.
    :type width: float
    :arg height: Height of the rect.
    :type height: float
    """
    
    coords = ((0, 0), (1, 0), (1, 1), (0, 1))

    shader = gpu.shader.from_builtin('UNIFORM_COLOR')
    batch = batch_for_shader(
        shader, 'TRI_FAN',
        {"pos": coords},
    )

    with gpu.matrix.push_pop():
        gpu.matrix.translate(position)
        gpu.matrix.scale((width, height))
        shader.uniform_float("color", color)

        batch.draw(shader)


def draw_callback_view():
    space_data = bpy.context.space_data

    if not space_data or not space_data.tree_type == 'RigPickerTree':
        return

    # Use the pin to know if this is the first time this picker window in created to hide the n panel
    if not space_data.pin:
        space_data.pin = True
        space_data.show_region_ui = False

    ob = bpy.context.object
    if not ob or ob.type !='ARMATURE' or not ob.data.rig_picker.sources:
        return

    if ob not in PICKERS:
        load_picker_data(ob)

    picker_group = PICKERS[ob]   
    picker_group.draw()


def draw_callback_px():
    sp = bpy.context.space_data
    if not sp.tree_type == 'RigPickerTree':
        return

    ob = bpy.context.object

    picker_group = PICKERS.get(ob)
    if not picker_group or not picker_group.tooltip:
        return

    region = bpy.context.region
    text = picker_group.tooltip
    ui_scale = bpy.context.preferences.system.ui_scale

    #print('Draw text', text)
    font_id = 0
    blf.size(font_id, int(13 * ui_scale))

    margins = [12, 5]
    text_size = blf.dimensions(font_id, text)
    text_pos = (region.width - text_size[0]-margins[0], margins[1])

    bg_pos = (text_pos[0] - margins[0], text_pos[1] - margins[1]-1)
    bg_size = (text_size[0] + 2*margins[0], text_size[1] + 2*margins[1])

    gpu.state.blend_set('ALPHA')
    draw_rect_2d(bg_pos, *bg_size, (0.1, 0.1, 0.1, 0.66))
    gpu.state.blend_set('NONE')


    #blf.dimensions(font_id, text)
    blf.enable(font_id, blf.SHADOW)


    # BLF drawing routine
    blf.position(font_id, round(text_pos[0]), round(text_pos[1]), 0)
    blf.color(font_id, 0.85, 0.85, 0.85, 1)

    blf.shadow(font_id , 5, 0.0, 0.0, 0.0, 1)
    blf.shadow_offset(font_id, 2, -2)

    blf.draw(font_id, text)

    blf.disable(font_id, blf.SHADOW)

handle_view = None
handle_pixel = None


def register():
    global handle_view
    global handle_pixel

    handle_view = bpy.types.SpaceNodeEditor.draw_handler_add(draw_callback_view, (), 'WINDOW', 'POST_VIEW')
    handle_pixel = bpy.types.SpaceNodeEditor.draw_handler_add(draw_callback_px, (), 'WINDOW', 'POST_PIXEL')

def unregister():
    global handle_view
    global handle_pixel

    bpy.types.SpaceNodeEditor.draw_handler_remove(handle_view, 'WINDOW')
    bpy.types.SpaceNodeEditor.draw_handler_remove(handle_pixel, 'WINDOW')

    handle_view = None
    handle_pixel = None