#!/usr/bin/python3

import sys
import json


import pygame
from settings import Settings
from settings import Money
from load import Load
from display_load import Display_Load
from sailor_mars import Sailor_Mars
from sailor_mars import Mars_Ahegao
from sailor_moon import Sailor_Moon
from sailor_moon import Ahegao
from sailor_venus import Sailor_Venus
from sailor_venus import Venus_Ahegao 
from sailor_mercury import Sailor_Mercury
from sailor_jupitor import Sailor_Jupitor
from spacing import Spacing
from cpu_temp import Cpu_Temp_Spec
from cpu_temp import Cpu_Load_Spec
from cpu_temp import Memory_Load_Spec
from cpu_temp import Disk_Usage_Spec

class Gauge2:
    def __init__(self):
        """initilizes the Gauge"""

        try:
            with open('/tmp/log4j.json') as f:
                self.records = json.load(f)
        except FileNotFoundError:
            with open('/tmp/log4j.json', 'w') as f:
                self.records = {}
                self.records['cpu temp record'] = 45
                self.records['cpu speed record'] = 2300
                json.dump(self.records, f)

        pygame.init()
        
        self.clock = pygame.time.Clock()

        #settings
        self.settings = Settings()
        
        #screens
        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('Hardware Gauges (h-series)')
         
        #modules
        self.load = Load()
        self.cpu_temp_spec = Cpu_Temp_Spec()
        
        #sprites
        self.scouts = pygame.sprite.Group()
        self.moneys = pygame.sprite.Group()
        
        self.sailor_venus = Sailor_Venus(self)
        self.sailor_venus_ahegao = Venus_Ahegao(self)       
        self.sailor_mars = Sailor_Mars(self)
        self.sailor_mars_ahegao = Mars_Ahegao(self)
        self.sailor_moon = Sailor_Moon(self, self.records)
        self.ahegao = Ahegao(self, self.records)
        self.sailor_mercury = Sailor_Mercury(self)
        self.sailor_jupitor = Sailor_Jupitor(self)
        
        self.scouts.add(self.sailor_mars)
        self.scouts.add(self.sailor_mars_ahegao)
        self.scouts.add(self.sailor_moon)
        self.scouts.add(self.sailor_venus)
        self.scouts.add(self.sailor_venus_ahegao)
        self.scouts.add(self.ahegao)
        self.scouts.add(self.sailor_mercury)
        self.scouts.add(self.sailor_jupitor)
        
        #module for showing loads in text
        self.display_load = Display_Load(self) 

        #testing
        """used when i want to try something new but dont know how i want it to look yet"""
        self.warning = False
        self.warning_counter = 0

    def save_stats(self):
        try:
            with open('/tmp/log4j.json', 'w') as f:
                records = {}
                records['cpu temp record'] = self.sailor_moon.record_temp
                records['cpu speed record'] = self.ahegao.cpu_speed_record
                json.dump(records, f)
        except FileNotFoundError:
            print('log4j corrupt')

    def check_events(self):
        """checks for events"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.save_stats()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if self.warning == False:
                    mouse_pos = pygame.mouse.get_pos()
                    self._check_mouse(mouse_pos)
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    self.save_stats()
                    sys.exit()
                if event.key == pygame.K_SPACE:
                    self.settings.make_it_rain(self)

    def _check_mouse(self, mouse):
        if self.sailor_moon.rect.collidepoint(mouse):
            self.cpu_temp_spec = Cpu_Temp_Spec()
            self.cpu_temp_spec.run_walk()
        if self.sailor_mars.rect.collidepoint(mouse):
            self.cpu_load_spec = Cpu_Load_Spec()
            self.cpu_load_spec.up_dog_loop()
        if self.sailor_venus.rect.collidepoint(mouse):
            self.memory_load_spec = Memory_Load_Spec()
            self.memory_load_spec.major_cakes_loop()
        if self.sailor_mercury.rect.collidepoint(mouse):
            self.disk_usage_spec = Disk_Usage_Spec()
            self.disk_usage_spec.mercury_office_loop()

    def warning_message(self):
        self.warning_counter += 1
        self.screen.fill(self.settings.screen_color)
        self.display_load.warning_message(self)
        self.screen.blit(self.settings.andex_image, self.settings.andex_rect) 
        pygame.display.flip()
        if self.warning_counter == 30:
            self.warning = False

    def update_screen(self):
        """updates the screen"""
        self.screen.fill(self.settings.screen_color)
        self.screen.blit(self.settings.image, self.settings.rect)
        self.moneys.draw(self.screen)
        self.scouts.draw(self.screen) 
        self.scouts.update(self.load)
        self.display_load.prep_msg(self.load)
        self.display_load.show_loads()
        #self.screen.blit(self.settings.water_mark_image, self.settings.water_mark_rect)
        pygame.display.flip()


    def run_game(self):
        """main loop"""
        while True:
            self.check_events()
            self.load.load_update()
            if self.warning == False:
                self.update_screen()
                self.moneys.update(self)
            elif self.warning == True:
                self.warning_message()
            self.clock.tick(3)

if __name__ == "__main__":
    gauge2 = Gauge2()
    gauge2.run_game()
