from turtle import Turtle, TurtleScreen, _Screen
from random import *

class window(_Screen):
        def __init__(self):
            super().__init__()
            TurtleScreen.__init__(self, window._canvas)
            if Turtle._screen is None:
                Turtle._screen = self
            self.setup(500,500)
            self.screensize(350,350)
            self.title("title")
            self.bgcolor("white")

class segments(Turtle):
    instances = []

    def __init__(self, xinit, yinit, size, shade, info):
        super().__init__()
        self.hideturtle()
        self.x = xinit
        self.y = yinit
        self.size = size
        self.shade = shade
        self.info = info
        self.speed(0)
        self.width(0)
        segments.instances.append(self)
        
    def rect(self):
        self.penup()
        self.color(self.shade)
        self.setx(self.x)
        self.sety(self.y)
        self.pendown()
        self.begin_fill()
        for i in range(3):
            self.forward(self.size)
            self.right(90)
        self.end_fill()
        self.penup()
        print(len(self.instances))
        
    def info(self, info):
        pass
#handle all states
#implment update function call for redrawing.
#if false: update old, draw new with properties at new location.
#if true 0: left side select.
#if true 1: right side select
def handle(mousex,mousey):
    global returnval
    global oldkey
    global flag

    mxadj = int()
    myadj = int()
    if returnval[0] == True:
        for i, j in slist.items():
            if j[1] - j[2] < mousey and j[1] > mousey and j[0] < mousex and j[0] + j[2] > mousex:
                if mousex < (j[0] + (j[0] + j[2])) / 2:
                    flag = True
                    newx = j[0] - j[2]
                    break
                else:
                    flag = True
                    newx = j[0] + j[2]
                    break
        if flag == True:
            newy = j[1]
            update(newx,newy,i)
        returnval = [False, 0]
        return returnval

    for i, j in slist.items():
        #i = id
        #j[0] = x
        #j[1] = y
        #j[2] = size
        if j[1] - j[2] < mousey and j[1] > mousey and j[0] < mousex and j[0] + j[2] > mousex:
            if mousex < (j[0] + (j[0] + j[2])) / 2:
                returnval = [True, 0]
                oldkey = i
                print(oldkey)
                return returnval     
            else:
                returnval = [True, 1]
                oldkey = i
                print(oldkey)
                return returnval  
        else:
            returnval = [False, 0]
    return returnval

def update(newx, newy, key):
    global oldkey
    global flag
    print(oldkey)
    trect = slist.get(oldkey)
    s = segments(trect[0], trect[1], trect[2], "green", "null")
    s.rect()
    print(newx,newy)
    #delete garbage square generated
    #slist.popitem()
    s.instances.pop()
    #delete old square via key
    del slist[oldkey]
    #insert data for new key
    trect = slist.get(key)
    s = segments(trect[0], trect[1], trect[2], "blue", "null")
    s.rect()
    flag = False
    print(s.instances)
    print(slist)

s = segments(0,0,50,"black","null")
w = window()
returnval = [False,0]

def draw ():
    global slist
    oldkey = 0
    flag = False
    #initialize segments
    screenx = w.screensize()[0]
    screeny = w.screensize()[1]
    for i in range(5):
        randx = randrange(-screenx,screenx)
        randy = randrange(-screeny,screeny)
        s = segments(randx,randy,26,"red","null")
        s.rect()
    #handle click and establish list - make changes as necessary 
    slist = {id(instance): (instance.x, instance.y, instance.size, instance.color) for instance in segments.instances}
    slist.pop(next(iter(slist)))
    s.instances.pop(0)
    print(slist)
    w.onscreenclick(handle)
    
draw()
w.mainloop()

#click left side of square, attach left side
#calculate flag via referencing list values/appendix values?
