import pygame
from pygame.locals import *


# Reminders

# Next Week's Plan
# Monday -- Final Project Workshop
# Wednesday -- Final Project Workshop
# Wed/Thu Lab -- Final Project Workshop
#   -- missing lab without excuse will count as unexcused
# Friday:
#  -- Quiz #11 - Recursion
#  -- Make Up Quiz Opportunities -- talk to me *AHEAD* of time
#  -- Project Workshop

# Office Hours During Finals Week (subject to change)
#    -- I will post and update as need on Teams
#
# Monday: 7:30am - 10am
# Tuesday: 7:30am - 1:45pm
# Wednesday: 7:30am - 3pm
# Thursday: 7:30am - 11:30am

# Final Project Demos
#  Friday, December 11
#  We will start at 9am -- likely done by ~10:30am


# Today, we are going to talk a bit about how computers make:
# * color
# * sound
#
# For each, I have a simple demonstration using PyGame
# and we will talk a bit about human perceptions

# COLOR
# Your eyes have 2 kinds of light sensitive cells:
# - rods, which basically work in grayscale
# - cones, which do the color part
#     - you have 3 types of cones
#          - red
#          - green
#          - blue
#  - light works additively - mix red and green and you see yellow
# - our brain processes color by essentially averaging the colors


###################################
class Slider:

    def __init__(self, location, name):
        self.loc = location
        self.name = name

        self.curr = 0
        self.active = False

        self.button = pygame.Rect(self.loc - 10,50+ (255 - self.curr)-5, 20, 10)
        self.button_color = 'light gray'

        self.outline = pygame.Rect(self.loc-5,50,10,255)

        self.filled = pygame.Rect(self.loc-5,50+ (255 - self.curr),10,self.curr)

    def update_button(self):
        self.button = pygame.Rect(self.loc - 10, 50 + (255 - self.curr) - 5, 20, 10)

    def update_filled(self):
        self.filled = pygame.Rect(self.loc - 5, 50 + (255 - self.curr), 10, self.curr)

    def get_value(self):
        return self.curr

    def return_button(self) -> 'Rect':
        return self.button

    def return_rect(self) -> 'Rect':
        return self.outline

    def return_rect(self) -> 'Rect':
        return pygame.Rect(self.loc-5,50,10,255)

    def draw(self,surface):

        pygame.draw.rect(surface, 'black', self.outline,2)
        pygame.draw.rect(surface, self.name, self.filled)

        #pygame.draw.rect(surface, 'light gray', (self.loc - 10,50+ (255 - self.curr)-5, 20, 10 ))
        clr = 'light gray'
        if self.active:
            clr = 'dark gray'
        pygame.draw.rect(surface, clr, self.return_button())

        totalText = set_text(self.name, self.loc, 30, 20, self.name)
        surface.blit(totalText[0], totalText[1])

        totalText = set_text(str(self.curr), self.loc, 325, 20, 'black')
        surface.blit(totalText[0], totalText[1])


    def down(self):
        self.curr -= 1
        if self.curr < 0:
            self.curr = 0

    def up(self):
        self.curr += 1
        if self.curr > 255:
            self.curr = 255



def set_text(string, coordx, coordy, fontSize, color): # Function to set text
    font = pygame.font.Font('freesansbold.ttf', fontSize)
    text = font.render(string, True, color)
    textRect = text.get_rect()
    textRect.center = (coordx, coordy)
    return (text, textRect)


def draw_all(surface, sliders):
    surface.fill('white')
    for item in sliders:
        item.draw(surface)


    r_clr = sliders[0].get_value()
    g_clr = sliders[1].get_value()
    b_clr = sliders[2].get_value()



    pygame.draw.rect(surface, pygame.Color(r_clr,g_clr, b_clr), (200,100,200,200))
    pygame.draw.rect(surface, 'black', (200, 100, 200, 200),  3)



    pygame.display.update()

def hit(rect: 'Rect', x,y):
    return rect.collidepoint(x,y)


def main():
    pygame.init()



    surface = pygame.display.set_mode((1000, 400))

    red = Slider(600,  'red')
    green = Slider(650,  'green')
    blue = Slider(700, 'blue')
    sliders = [red, green, blue]

    active = red





    running = True

    while running:
        draw_all(surface, sliders)

        # The Event Queue!
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
            elif event.type == KEYDOWN:
                if event.key == K_r:
                    active = red
                    red.active = True
                    green.active = False
                    blue.active = False
                elif event.key == K_g:
                    active = green
                    green.active = True
                    red.active = False
                    blue.active = False
                elif event.key == K_b:
                    active = blue
                    blue.active=True
                    red.active = False
                    green.active = False



                elif event.key == K_UP:
                    active.up()


                elif event.key == K_DOWN:
                    active.down()

            elif event.type == MOUSEBUTTONDOWN:
                (x, y) = (event.pos[0], event.pos[1])

                for item in sliders:

                    if hit(item.return_button(),x,y):
                        active = item
                        for items in sliders:
                            if items != item:
                                items.active = False
                            else:
                                items.active = True

                    elif hit(item.return_rect(),x,y) and item.active:
                        item.curr = 50+ (255 - y)

        for item in sliders:
            item.update_button()
            item.update_filled()







main()

