import pygame
from pygame.locals import *

# We will now look at sound
# In many ways, sound is *significantly* more complicated
#
# Humans have the ability to distinguish ~1000 different frequencies of sound
# The cochlea in the inner ear vibrates which vibrates cilia
# which transmit the particular frequency to the brain

# the brain does not average sound in the same way it does light




# Sounds taken from https://github.com/fuhton/piano-mp3/tree/master/piano-mp3
# using MIT License
#
# To run this file, you'll need to copy the 'Piano' folder
# from the class Team into your Project folder

class Key:

    def __init__(self, rect, name, color):

        self.rectangle = rect
        self.name = name
        self.color = color
        self.base_color = color
        self.sound = pygame.mixer.Sound('Piano/'+name+'.mp3')
        self.count = -1000




    def draw(self, surface):
        pygame.draw.rect(surface, self.color, self.rectangle)
        pygame.draw.rect(surface, 'black',self.rectangle,1)

    def played(self, counter):
        self.count = counter

    def update(self, counter):
        if counter < self.count + 100:
            self.color = 'blue'
        else:
            self.color = self.base_color



class Circle:

    def __init__(self):
        self.center_x = 540
        self.center_y = 280
        self.rad = 25

    def draw(self, surface):
        pygame.draw.circle(surface, 'blue',(self.center_x, self.center_y),self.rad)



def draw_all(surface, white, black, c):
    surface.fill('white')

    for key in white:
        key.draw(surface)

    for key in black:
        key.draw(surface)
    c.draw(surface)

    pygame.display.update()

def hit(rect: 'Rect', x,y):
    return rect.collidepoint(x,y)



def main():
    pygame.init()
    surface = pygame.display.set_mode((1080, 400))
    c = Circle()

    notes = 'ABCDEFG'
    white = []
    black = []

    xcurr = 20
    counter = 0
    while len(white) < 52:
        octave = (counter+5) // 7
        note_name = notes[counter % 7] + str(octave)
        r = pygame.Rect(xcurr, 50, 20,140)
        white.append(Key(r,note_name,'white' ))
        xcurr+= 20
        counter +=1

    flats = 'BDEGA'
    skip = [2,1,2,1,1]
    xcurr = 35
    counter = 0
    while len(black) < 36:
        octave = (counter + 4 ) // 5
        note_name = flats[counter % 5] +'b'+ str(octave)

        r = pygame.Rect(xcurr, 50, 10, 100)
        black.append(Key(r, note_name, 'black'))
        xcurr += 20 * skip[counter % 5]
        counter += 1




    running = True
    counter  = 0
    while running:
        counter += 1
        draw_all(surface, white, black, c)

        # The Event Queue!
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False


            elif event.type == MOUSEBUTTONDOWN:
                (x, y) = (event.pos[0], event.pos[1])
                click = False
                for black_key in black:
                    if hit(black_key.rectangle, x, y):
                        pygame.mixer.Sound.play(black_key.sound)
                        click = True
                        black_key.played(counter)
                if not click:
                    for white_key in white:
                        if hit(white_key.rectangle, x, y):
                            pygame.mixer.Sound.play(white_key.sound)
                            white_key.played(counter)


                if (x - c.center_x) ** 2 +  (y - c.center_y) ** 2 <= c.rad ** 2:
                    for key in white:
                        if ('C' in key.name or 'E' in key.name or 'G' in key.name) and '1' not in key.name:
                            pygame.time.delay(350)
                            pygame.mixer.Sound.play(key.sound)
                            key.played(counter)
                            key.update(counter)
                            draw_all(surface, white, black, c)

                for key in white:
                    key.update(counter)

                for key in black:
                    key.update(counter)




main()

