#!/usr/bin/python3

import random

import pygame
from settings import Settings
from settings import Money
from display import Display_Text 
from sailor_saturn import Sailor_Saturn
from sailor_pluto import Sailor_Pluto

class GPU_Load_Display:
    def __init__(self, run):
        pygame.init()

        self.settings = Settings()

        self.i_need_this_to_prevent_errors = run
        self.gpu_temp = 0
        self.cpu_temp = 0
        self.gpu_utilization = 0

        self.clock = pygame.time.Clock()

        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        self.screen_rect = self.screen.get_rect()
        pygame.display.set_caption('GPU load')


        self.settings.background_rect.center = self.screen_rect.center

        self.scouts = pygame.sprite.Group() 
        self.moneys = pygame.sprite.Group()

        self.money = 0

        self.sailor_saturn = Sailor_Saturn(self)
        self.sailor_pluto = Sailor_Pluto(self)

        self.scouts.add(self.sailor_saturn)
        self.scouts.add(self.sailor_pluto)

        self.display_text = Display_Text(self)   

    def check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.i_need_this_to_prevent_errors = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    self.i_need_this_to_prevent_errors = False
                if event.key == pygame.K_SPACE:
                    self.settings.make_it_rain(self)

    def stop_thread(self):
        return self.i_need_this_to_prevent_errors

    def update_screen(self):
        self.screen.fill(self.settings.screen_color)
        self.screen.blit(self.settings.background_image, self.settings.background_rect)
        self.scouts.draw(self.screen)
        self.moneys.draw(self.screen)
        self.display_text.show_text()
        pygame.display.flip()

    def window_loop(self):
        while self.i_need_this_to_prevent_errors == True:
            self.check_events()
            self.display_text.prep_message(self, self.gpu_temp, self.gpu_utilization, self.cpu_temp)
            self.update_screen()
            self.scouts.update(self)
            self.moneys.update(self)
            self.clock.tick(3)
        pygame.quit()

    def load_update(self, gpu, utilzation, cpu):
        self.gpu_temp = gpu
        self.gpu_utilization = utilzation
        self.cpu_temp = cpu
        
