import pygame


# CONSTANTS

# window dimensions
WINDOWWIDTH = 700
WINDOWHEIGHT = 900

# board tile location
TILELOC = 80

# board tile size
TILESIZE = 60

# feedback tile size and margin
FBSIZE = 25

# board margins
TOPMARGIN = 40
LEFTMARGIN = 150
FBLEFTMARGIN = 475
FBTOPMARGIN = 70

# screen Surface
SCREEN = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))

# board dimensions
ROWS = 9
COLUMNS = 4

# colors (R, G, B)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 220, 0)
BLUE = (0, 0, 255)
ORANGE = (255, 100, 0)
BLACK = (0, 0, 0)
PURPLE = (150, 25, 246)
WHITE = (255, 255, 255)
TILE = (109, 120, 138)
BGCOLOR = (41, 60, 89)
TEXT = (193, 201, 214)


# GAME SCREENS/TEXT
def start_screen():
    # fonts
    font_lg = pygame.font.Font('GamePlayed-vYL7.ttf', 70)
    font_md = pygame.font.Font('GamePlayed-vYL7.ttf', 25)
    font_sm = pygame.font.Font('GamePlayed-vYL7.ttf', 14)

    color = TEXT

    # display title
    title = font_lg.render('MASTERMIND', True, color)
    title_rect = title.get_rect(center=(WINDOWWIDTH / 2, WINDOWHEIGHT / 14))
    SCREEN.blit(title, title_rect)

    # text buffers
    y_buffer = 40
    x_buffer = 40

    # rules title
    font_md.set_underline(True)
    rule_title = font_md.render('Rules:', True, color)
    rule_title_rect = rule_title.get_rect(center=(title_rect.bottomleft[0] - x_buffer,
                                                  title_rect.bottomleft[1] + y_buffer))
    SCREEN.blit(rule_title, rule_title_rect)

    # rule 1
    rule_1 = font_sm.render('- Objective of the game is to guess the 4 color solution '
                            'generated by the CPU', True, color)

    rule_1_rect = rule_1.get_rect(topleft=(rule_title_rect.center[0] - x_buffer,
                                           rule_title_rect.center[1] + y_buffer))
    SCREEN.blit(rule_1, rule_1_rect)

    # rule 2
    rule_2 = font_sm.render('- Select from 6 color tiles to guess the correct solution', True, color)
    rule_2_rect = rule_2.get_rect(topleft=(rule_1_rect.bottomleft[0],
                                           rule_1_rect.bottomleft[1] + y_buffer))
    SCREEN.blit(rule_2, rule_2_rect)

    # rule 3
    rule_3 = font_sm.render('- If guess is correct color in correct position, a black tile will '
                            'be displayed', True, color)

    rule_3_rect = rule_3.get_rect(topleft=(rule_2_rect.bottomleft[0],
                                           rule_2_rect.bottomleft[1] + y_buffer))
    SCREEN.blit(rule_3, rule_3_rect)

    # rule 4
    rule_4 = font_sm.render('- If guess is correct color in incorrect position, a white tile will '
                            'be displayed', True, color)
    rule_4_rect = rule_4.get_rect(topleft=(rule_3_rect.bottomleft[0],
                                           rule_3_rect.bottomleft[1] + y_buffer))
    SCREEN.blit(rule_4, rule_4_rect)

    # rule 5
    rule_5 = font_sm.render('- Use up and down arrow keys to select color and right and left to'
                            'select column', True, color)
    rule_5_rect = rule_5.get_rect(topleft=(rule_4_rect.bottomleft[0],
                                           rule_4_rect.bottomleft[1] + y_buffer))
    SCREEN.blit(rule_5, rule_5_rect)

    # rule 6
    rule_6 = font_sm.render('- There will be no duplicate colors in the solution', True, color)
    rule_6_rect = rule_6.get_rect(topleft=(rule_5_rect.bottomleft[0],
                                           rule_5_rect.bottomleft[1] + y_buffer))
    SCREEN.blit(rule_6, rule_6_rect)

    # rule 7
    rule_7 = font_sm.render('- Press [ENTER] to submit guessed row', True, color)
    rule_7_rect = rule_7.get_rect(topleft=(rule_6_rect.bottomleft[0],
                                           rule_6_rect.bottomleft[1] + y_buffer))
    SCREEN.blit(rule_7, rule_7_rect)

    # rule 8
    rule_8 = font_sm.render('- Press [N] to reset at any time', True, color)
    rule_8_rect = rule_8.get_rect(topleft=(rule_7_rect.bottomleft[0],
                                           rule_7_rect.bottomleft[1] + y_buffer))
    SCREEN.blit(rule_8, rule_8_rect)

    # begin
    begin = font_md.render('PRESS [SPACE] TO BEGIN', True, color)
    begin_rect = begin.get_rect(topleft=(rule_8_rect.bottomleft[0],
                                         rule_8_rect.bottomleft[1] + y_buffer))

    SCREEN.blit(begin, begin_rect)


def finish_screen(outcome):
    # fonts
    font_lg = pygame.font.Font('GamePlayed-vYL7.ttf', 70)
    font_md = pygame.font.Font('GamePlayed-vYL7.ttf', 25)

    color = TEXT

    # player win
    if outcome == 'p':
        congrats = font_lg.render('CONGRATULATIONS!!', True, color)
        congrats_rect = congrats.get_rect(center=(WINDOWWIDTH/2, WINDOWHEIGHT/5))
        SCREEN.blit(congrats, congrats_rect)

        you_won = font_lg.render('YOU WON!!', True, color)
        you_won_rect = you_won.get_rect(center=(WINDOWWIDTH / 2, (WINDOWHEIGHT / 5) + 150))
        SCREEN.blit(you_won, you_won_rect)

    # cpu win
    if outcome == 'c':
        sorry = font_lg.render('SORRY :(', True, color)
        sorry_rect = sorry.get_rect(center=(WINDOWWIDTH / 2, WINDOWHEIGHT / 5))
        SCREEN.blit(sorry, sorry_rect)

        you_lost = font_lg.render('YOU LOST', True, color)
        you_lost_rect = you_lost.get_rect(center=(WINDOWWIDTH / 2, (WINDOWHEIGHT / 5) + 150))
        SCREEN.blit(you_lost, you_lost_rect)

    play_again = font_md.render('PLAY AGAIN??', True, color)
    play_again_rect = play_again.get_rect(center=(WINDOWWIDTH / 2, (WINDOWHEIGHT / 5) + 250))
    SCREEN.blit(play_again, play_again_rect)

    press_enter = font_md.render('Press [SPACE] to play again', True, color)
    press_enter_rect = press_enter.get_rect(center=(WINDOWWIDTH / 2, (WINDOWHEIGHT / 5) + 350))
    SCREEN.blit(press_enter, press_enter_rect)


def main_legend():
    font_sm = pygame.font.Font('GamePlayed-vYL7.ttf', 14)

    color = TEXT

    # legend 1
    legend_1 = font_sm.render('White Tile = correct color in wrong position', True, color)
    legend_1_rect = legend_1.get_rect(topleft=(150, WINDOWHEIGHT - 130))
    SCREEN.blit(legend_1, legend_1_rect)

    # legend 2
    legend_2 = font_sm.render('Black Tile = correct color in correct position', True, color)
    legend_2_rect = legend_2.get_rect(topleft=(legend_1_rect.bottomleft[0],
                                               legend_1_rect.bottomleft[1]))
    SCREEN.blit(legend_2, legend_2_rect)

    # legend 3
    legend_3 = font_sm.render('Order of feedback tiles do not correspond to  main tiles', True, color)
    legend_3_rect = legend_3.get_rect(topleft=(legend_2_rect.bottomleft[0],
                                               legend_2_rect.bottomleft[1]))
    SCREEN.blit(legend_3, legend_3_rect)

    # legend 3
    legend_3 = font_sm.render('Order of feedback tiles do not correspond to  main tiles', True, color)
    legend_3_rect = legend_3.get_rect(topleft=(legend_2_rect.bottomleft[0],
                                               legend_2_rect.bottomleft[1]))
    SCREEN.blit(legend_3, legend_3_rect)

    # legend 4
    legend_4 = font_sm.render('There are no duplicates in the solution', True, color)
    legend_4_rect = legend_4.get_rect(topleft=(legend_3_rect.bottomleft[0],
                                               legend_3_rect.bottomleft[1]))
    SCREEN.blit(legend_4, legend_4_rect)

    # legend 5
    legend_5 = font_sm.render('Press [N] at any time to begin a new game', True, color)
    legend_5_rect = legend_5.get_rect(topleft=(legend_4_rect.bottomleft[0],
                                               legend_4_rect.bottomleft[1]))
    SCREEN.blit(legend_5, legend_5_rect)
