from tkinter import *
from tkinter import ttk, colorchooser


class main:
    def __init__(self, master):
        self.master = master
        self.colors = ["black", "red", "orange", "yellow", "green", "teal", "cyan", "blue", "purple", "magenta", "gold",
                       "pink", "aqua", "indigo", "violet" ]
        self.color_fg = 'black'
        self.previous_color = 'black'
        self.color_bg = 'white'
        self.old_x = None
        self.old_y = None
        self.penwidth = 10
        self.colorPosition = 0
        self.drawWidgets()
        self.c.bind('<B1-Motion>', self.paint)
        self.c.bind('<ButtonRelease-1>', self.reset)

    def paint(self, e):
        if self.old_x and self.old_y:
            self.c.create_line(self.old_x, self.old_y, e.x, e.y, width=self.penwidth, fill=self.color_fg,
                               capstyle=ROUND, smooth=True)

        self.old_x = e.x
        self.old_y = e.y

    def reset(self, e):  # reseting or cleaning the canvas
        self.old_x = None
        self.old_y = None

    def clear(self):
        self.c.delete(ALL)

    def change_fg(self):  # changing the pen color
        self.color_fg = colorchooser.askcolor(color=self.color_fg)[1]

    def change_bg(self):  # changing the background color canvas
        self.color_bg = colorchooser.askcolor(color=self.color_bg)[1]
        self.c['bg'] = self.color_bg

    def set_fg_color(self, color):
        self.color_fg = color

    def decreaseWidth(self):
        if self.penwidth > 1:  # Set a lower limit for pen width
            self.penwidth -= 1
            self.width_display.config(text=str(self.penwidth))

    def increaseWidth(self):
        if self.penwidth < 100:  # Set an upper limit for pen width
            self.penwidth += 1
            self.width_display.config(text=str(self.penwidth))

    def downColor(self):
        # Move the index down, looping back to the end if at the start
        self.colorPosition = (self.colorPosition - 1) % len(self.colors)
        self.updateColor()

    def upColor(self):
        # Move the index up, looping back to the start if at the end
        self.colorPosition = (self.colorPosition + 1) % len(self.colors)
        self.updateColor()

    def updateColor(self):
        # Update the displayed color and set the color in use
        self.color_display.config(text=self.colors[self.colorPosition])
        self.set_fg_color(self.colors[self.colorPosition])

    def useEraser(self):
        # Switch to eraser mode
        self.eraser_on = True
        self.previous_color = self.color_fg  # Save the current pen color
        self.set_fg_color(self.color_bg)  # Set pen color to background color

    def usePen(self):
        # Switch back to pen mode
        self.eraser_on = False
        self.set_fg_color(self.previous_color)  # Restore the previous pen color

    def drawWidgets(self):
        self.controls = Frame(self.master, padx=5, pady=5)

        # Add the pen button
        self.pen_button = Button(self.controls, text="Pen", command=self.usePen, font=('times 16'), padx=20, pady=20)
        self.pen_button.grid(row=0, column=0)

        # Add the eraser button
        self.eraser_button = Button(self.controls, text="Eraser", command=self.useEraser, font=('times 16'), padx=10,
                                    pady=20)
        self.eraser_button.grid(row=0, column=2)

        # Label Pen Width
        Label(self.controls, text='Pen Width:', font=('times 16')).grid(row=1, column=0)

        # Button to decrease pen width
        self.decrease_button = Button(self.controls, text='-', command=self.decreaseWidth, font=('times 16'), padx=15, pady=15)
        self.decrease_button.grid(row=1, column=1)

        # Display the pen width value
        self.width_display = Label(self.controls, text=str(self.penwidth), font=('times 16'), padx=5, pady=5)
        self.width_display.grid(row=1, column=2)

        # Button to increase pen width
        self.increase_button = Button(self.controls, text='+', command=self.increaseWidth, font=('times 16'), padx=15, pady=15)
        self.increase_button.grid(row=1, column=3)

        # Label Colors
        Label(self.controls, text='Colors:', font=('times 16')).grid(row=2, column=0)

        # Button to move back colors
        self.decrease_button = Button(self.controls, text='<', command=self.downColor, font=('times 16'), padx=14,
                                      pady=15)
        self.decrease_button.grid(row=2, column=1)

        # Display the current color
        self.color_display = Label(self.controls, text=self.colors[self.colorPosition], font=('times 16'), padx=5, pady=5)
        self.color_display.grid(row=2, column=2)

        # Button to move up colors
        self.increase_button = Button(self.controls, text='>', command=self.upColor, font=('times 16'), padx=14, pady=15)
        self.increase_button.grid(row=2, column=3)

        # Button to Clear
        Button(self.controls, text="CLEAR", command=self.clear, font=('times 16'), padx=10, pady=20).grid(row=0, column=4)

        self.controls.pack(side=LEFT)

        self.c = Canvas(self.master, width=14400, height=800, bg=self.color_bg, )
        self.c.pack(fill=BOTH, expand=True)


if __name__ == '__main__':
    root = Tk()
    main(root)
    root.title('Paint App')
    root.mainloop()