# Day 22
#                                  __                 _
#   /\/\   ___  _   _ ___  ___    /__\_   _____ _ __ | |_ ___
#  /    \ / _ \| | | / __|/ _ \  /_\ \ \ / / _ \ '_ \| __/ __|
# / /\/\ \ (_) | |_| \__ \  __/ //__  \ V /  __/ | | | |_\__ \
# \/    \/\___/ \__,_|___/\___| \__/   \_/ \___|_| |_|\__|___/  and grid layout

from tkinter import *
import tkinter as tk
from tkinter import messagebox

'''
at least:
- [X] 5 drawing shapes
- [X] 5 sizes
- [X] 5 colors
- [X] 2 other improvements
but add:
- [X] full palette of colors
- [X] custom color picker (input color code in textbox)
- [X] clear canvas button
- [X] backround color change
- [X] eraser tool
- [ ] make text
- [X] nice ui for everything (use frames for organization)
'''

# defining functions
# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

#TODO: put all this in one function
def red():
    global colour
    colour = "red"
def orange ():
    global colour
    colour = "orange"
def yellow ():
    global colour
    colour = "yellow"
def green ():
    global colour
    colour = "green"
def blue ():
    global colour
    colour = "blue"
def indigo ():
    global colour
    colour = "indigo"
def violet ():
    global colour
    colour = "violet"
def black ():
    global colour
    colour = "black"
def white ():
    global colour
    colour = "white"

def colorpicker():
    global colour
    colour = f"{colorbox.get()}"

def square():
    global shape
    shape = "square"
def triangle():
    global shape
    shape = "triangle"
def circle():
    global shape
    shape = "circle"
def lank():
    global shape
    shape = "lank"
def stout():
    global shape
    shape = "stout"
def ratriangle():
    global shape
    shape = "ratriangle"

def sizechange(val): #FIXME: you have to move the size scale to be able to draw
    global size
    size = int(val)

def changebg(event):
    global bgcolor
    bgcolor = colour
    c.configure(bg=colour)
    root.update()
def eraser():
    global colour
    colour = bgcolor

def draw(event):
    try:
        if shape == "square":
            c.create_rectangle(event.x, event.y, event.x + size, event.y + size, fill=colour, outline=colour)
        if shape == "triangle":
            c.create_polygon(event.x, event.y, event.x + (size/2), event.y + size, event.x - (size/2), event.y + size, fill=colour, outline=colour)
        if shape == "circle":
            c.create_oval(event.x, event.y, event.x + size, event.y + size, fill=colour, outline=colour)
        if shape == "lank":
            c.create_rectangle(event.x, event.y, event.x + (size/2), event.y + size + (size/2), fill=colour, outline=colour)
        if shape == "stout":
            c.create_rectangle(event.x, event.y, event.x + size + (size/2), event.y + (size/2), fill=colour, outline=colour)
        if shape == "ratriangle":
            c.create_polygon(event.x, event.y, event.x, event.y + size, event.x - size, event.y + size, fill=colour, outline=colour)
        root.update()
    except tk.TclError:
        messagebox.showinfo("Error", "Invalid Color Input")
    except TypeError:
        messagebox.showinfo("Error", "Set Brush Size to Start!")

def drag(event):
    if shape == "square":
        c.create_rectangle(event.x, event.y, event.x + size, event.y + size, fill=colour, outline=colour)
    if shape == "triangle":
        c.create_polygon(event.x, event.y, event.x + (size/2), event.y + size, event.x - (size/2), event.y + size, fill=colour, outline=colour)
    if shape == "circle":
        c.create_oval(event.x, event.y, event.x + size, event.y + size, fill=colour, outline=colour)
    if shape == "lank":
        c.create_rectangle(event.x, event.y, event.x + (size/2), event.y + size + (size/2), fill=colour, outline=colour)
    if shape == "stout":
        c.create_rectangle(event.x, event.y, event.x + size + (size/2), event.y + (size/2), fill=colour, outline=colour)
    if shape == "ratriangle":
        c.create_polygon(event.x, event.y, event.x, event.y + size, event.x - size, event.y + size, fill=colour, outline=colour)
    root.update()

# Defining Variables
# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

# windows
root=Tk()
root.title("Mouse Events")
root.geometry("650x500+10+10")

# canvas
c=Canvas(root,width=500,height=500,bg="black")
c.place(x=150, y=10)

# keybinds
c.bind("<Button-1>",draw)   # left click
c.bind("<B1-Motion>", drag) # left click and drag
root.bind("z", changebg)

colour ="blue"
bgcolor = "black"
shape ="square"
size = 10

# grid layout (to organize ui elements)
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=14)
root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=2)
root.rowconfigure(2, weight=1)
root.rowconfigure(3, weight=1)
root.rowconfigure(4, weight=1)
root.rowconfigure(5, weight=1)
root.rowconfigure(6, weight=1)
root.rowconfigure(7, weight=2)

# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_
# UI - UI - UI - UI - UI - UI - UI - UI -UI - UI - UI - UI- UI - UI - UI - UI - UI - UI - UI - UI -UI - UI - UI - UI
# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

# Header
header = Label(root, font=("Helvetica 10 bold"), text="we pimp gimpin'", fg="blue")
header.grid(column=0, row=0, sticky=tk.N)

colorheader = Label(root, font=("Helvetica 10"), text="Select Colors", fg="orange")
colorheader.grid(column=0, row=0, sticky=tk.S)

# Colour Palette

# create buttons
button1=Button(root,
    text="■",
    fg='red',
    font=("Helvetica", 10),
    command = red)
button1.grid(column=0, row=1, sticky=tk.NW, pady=5)

button2=Button(root,
    text="■",
    fg='orange',
    font=("Helvetica", 10),
    command = orange)
button2.grid(column=0, row=1, sticky=tk.N, pady=5)

button3=Button(root,
    text="■",
    fg='yellow',
    font=("Helvetica", 10),
    command = yellow)
button3.grid(column=0, row=1, sticky=tk.NE, pady=5)

button4=Button(root,
    text="■",
    fg='green',
    font=("Helvetica", 10),
    command = green)
button4.grid(column=0, row=1, sticky=tk.W, pady=5)

button5=Button(root,
    text="■",
    fg='blue',
    font=("Helvetica", 10),
    command = blue)
button5.grid(column=0, row=1, pady=5)

button6=Button(root,
    text="■",
    fg='indigo',
    font=("Helvetica", 10),
    command = indigo)
button6.grid(column=0, row=1, sticky=tk.E, pady=5)

button7=Button(root,
    text="■",
    fg='violet',
    font=("Helvetica", 10),
    command = violet)
button7.grid(column=0, row=1, sticky=tk.SW, pady=5)

button8=Button(root,
    text="■",
    fg='black',
    font=("Helvetica", 10),
    command = black)
button8.grid(column=0, row=1, sticky=tk.S, pady=5)

button9=Button(root,
    text="■",
    fg='white',
    font=("Helvetica", 10),
    command = white)
button9.grid(column=0, row=1, sticky=tk.SE, pady=5)

# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

# Custom Colors
colorbox = Entry(root, bd=2, width=8)
colorbox.grid(column=0, row=2, sticky=tk.NE)

buttoncb=Button(root,
    text="Choose",
    fg='blue',
    font=("Helvetica", 10),
    command = colorpicker)
buttoncb.grid(column=0, row=2, sticky=tk.NW)

# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

# Cursor shapes

shapeheader = Label(root, text="Cursor Shape", foreground="green")
shapeheader.grid(column=0, row=2, sticky=tk.S, padx=5)

buttonsq=Button(root,
    text="■",
    fg='black',
    font=("Helvetica", 10),
    command = square)
buttonsq.grid(column=0, row=3, sticky=tk.NW, padx=5, pady=5)

buttontr=Button(root,
    text="▲",
    fg='black',
    font=("Helvetica", 10),
    command = triangle)
buttontr.grid(column=0, row=3, sticky=tk.N, padx=5, pady=5)

buttonci=Button(root,
    text="⬤",
    fg='black',
    font=("Helvetica", 10),
    command = circle)
buttonci.grid(column=0, row=3, sticky=tk.NE, padx=5, pady=5)

buttonla=Button(root,
    text="▮",
    fg='black',
    font=("Helvetica", 10),
    command = lank)
buttonla.grid(column=0, row=3, sticky=tk.SW, padx=5, pady=5)

buttonst=Button(root,
    text="▬",
    fg='black',
    font=("Helvetica", 10),
    command = stout)
buttonst.grid(column=0, row=3, sticky=tk.S, padx=5, pady=5)

buttonra=Button(root,
    text="◢",
    fg='black',
    font=("Helvetica", 10),
    command = ratriangle)
buttonra.grid(column=0, row=3, sticky=tk.SE, padx=5, pady=5)

# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

# change size
size = Scale(root, from_=1, to=50, orient=HORIZONTAL, command=sizechange, showvalue=0)
size.grid(column=0, row=4, sticky=tk.N, padx=5, pady=5)

# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

clearheader = Label(root, text="Clear Canvas", foreground="purple")
clearheader.grid(column=0, row=5, sticky=tk.N, padx=5)

# clear canvas
buttoncl=Button(root,
    text="Clear",
    fg='red',
    font=("Helvetica", 10),
    command = lambda: c.delete("all"))
buttoncl.grid(column=0, row=5, sticky=tk.W, padx=5, pady=5)

# eraser
buttoner=Button(root,
    text="Eraser",
    fg='black',
    bg="pink",
    font=("Helvetica", 10),
    command = eraser)
buttoner.grid(column=0, row=5, sticky=tk.E, padx=5, pady=5)

# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

# Custom Text
# textbox = Entry(root, bd=2, width=10)
# textbox.grid(column=0, row=6, sticky=tk.SW)

# buttontb=Button(root,
#     text="Text",
#     fg='blue',
#     font=("Helvetica", 10),
#     command = colorpicker)
# buttontb.grid(column=0, row=6, sticky=tk.SE)

root.mainloop()