# THIS FILE IS A PART OF VCStudio
# PYTHON 3


# This is collection of simple geometric math operations that I gonna use trough
# out the UI of the VCStudio. Stuff like collision detections, overlap detections
# and other verious little tiny, but overused functions. 

def line_overlap(line1, line2):
    
    # This function will check if 2 one dimenshional line overlap.
    
    overlap = False
    
    # Let's sort them just incase.
    
    line1.sort()
    line2.sort()
    
    
    # Well. I guess this is the least ambiguos way of doing so. In my opinion
    
    if line1[0] > line2[0] and line1[0] < line2[1]:
        overlap = True
    elif line1[1] > line2[0] and line1[1] < line2[1]:
        overlap = True
    elif line2[0] > line1[0] and line2[0] < line1[1]:
        overlap = True
    elif line2[1] > line1[0] and line2[1] < line1[1]:
        overlap = True
    
    
    return overlap

def rectangle_overlap(rectangle1, rectangle2):
    
    # This function will see if 2 rectangle overlap. It returns either True or
    # False.
    
    overlap = False
    
    # Now since it's going to be easier for me to use the same type of coordi-
    # nates as in cairo to draw rectangles. I need to purify them first. Meaning
    # convert the width and height into real points on a plane.
    
    r1x, r1y, r1w, r1h = rectangle1
    r2x, r2y, r2w, r2h = rectangle2
    
    
    r1w += r1x
    r1h += r1y
    r2w += r2x
    r2h += r2y
    
    
    # Now we going to simply compare if overlapping lines of x coordinates and if
    # yes. Coordinates of y coordinates.
    
    if line_overlap([r1x, r1w],[r2x, r2w]) and line_overlap([r1y, r1h],[r2y, r2h]):
        overlap = True
    
    
    return overlap
    
def rectangle_surround( win, name, target_pos, target_size, now_pos, now_size  ):
    
    # This function does surrounding operations. For example you have N amount
    # of nodes selected. And you want to draw a rectangle arround all of them.
    # for this you will have to find out the smallest of all values and biggest
    # and so this is what this funtion is for.
    
    now_pos = now_pos.copy()
    now_size = now_size.copy()
    
    # We are going to start by refrashing the current position of the rectangle
    # for this we need to know whether at this frame there was a refresh already.
    
    if win.current["frame"] != win.surround["frame"]:# and win.current["frame"] % 2 == 0:
        win.surround["frame"] = win.current["frame"]
        win.surround["rects"] = []
    
    # Now let's see if a given name is in the data of those refrashed. 
    
    if name not in win.surround["rects"]:
        target_pos[0] = now_pos[0]
        target_pos[1] = now_pos[1]
        target_size[0] = now_size[0]
        target_size[1] = now_size[1]
        
        win.surround["rects"].append(name)
        
        # Now you maybe wondering why I would not just make the whole targer_size
        # for example = [0,0]. And it because lists are linked from some other
        # data. And I don't create new lists. But change the link lists.
    
    
    
    # Now let's do the checking and overwritting of all the rest of the data.
     
    if now_pos[0] < target_pos[0]:
        target_size[0] += target_pos[0] - now_pos[0] 
        target_pos[0] = now_pos[0]
     
    if now_pos[1] < target_pos[1]:
        target_size[1] += target_pos[1] - now_pos[1] 
        target_pos[1] = now_pos[1] 
        
    # Now the size. It's a bit trickier. But not too hard. We need to remember
    # the the ultimate bottom, right point is a combintation of position and 
    # size.
    
    
    if now_pos[0] + now_size[0] > target_pos[0] + target_size[0]:
        target_size[0] = now_size[0] + now_pos[0] - target_pos[0]
    
    if now_pos[1] + now_size[1] > target_pos[1] + target_size[1]:
        target_size[1] = now_size[1] + now_pos[1] - target_pos[1]
    
def filter_arrows(win):
    
    # This function filters the arrow connections. I copied it from the
    # Blender_Organizer's code. at py_data/modules/story_editor.py
    
    # Doing some clever magic I forgot the purpose of
    for num, arrow in enumerate(win.story["arrows"]):
        for num2, arrow2 in enumerate(win.story["arrows"]):
            if arrow[0] == arrow2[0] or arrow[1] == arrow2[1]:
                win.story["arrows"][num] = arrow2

    # Removing doubles    
    tmp = []
    for i in win.story["arrows"]:
        if i not in tmp:
            tmp.append(i)
    win.story["arrows"] = tmp
    
def timestring(tleft):
    
    # This crazy function will convert the microsecond into something a bit
    # more usefull. Like 03:20:90.06 Kind a thing.
    
    tleftX = tleft
    
    tleft = int(tleftX)
    
    addend = tleftX - tleft
    
                                   
    valt = str(tleft)
    
    if tleft > 60 :
        le = tleft
        tleft = int(tleft / 60)
        le = le - int(tleft * 60)
        
        stleft = "0"*(2-len(str(tleft)))+str(tleft)
        sle = "0"*(2-len(str(le)))+str(le)
        
        valt = stleft+":"+ sle
    
        if tleft > 60 :
            lele = le
            le = tleft
            tleft = int(tleft / 60)
            le = le - int(tleft * 60)
            lele = (lele - le)
            if lele < 0:
                lele = int(lele * -1)
            
            stleft = "0"*(2-len(str(tleft)))+str(tleft)
            sle = "0"*(2-len(str(le)))+str(le)
            slele = "0"*(2-len(str(lele)))+str(lele)
            
            valt = stleft+":"+ sle + ":" + slele
    
            if tleft > 24 :
                le = tleft
                tleft = int(tleft / 24)
                le = le - int(tleft * 24)
                valt = str(tleft)+" DAYS AND "+ str(le) + " HRS"
    return valt + "." + str(int(addend*100))