#!/usr/bin/python3

#import json
import os
import random

import pygame
from pygame.sprite import Sprite
from natsort import natsorted

class Sailor_Moon(Sprite):
    def __init__(self, game, records):
        """initializes the processor sailor moon"""
        super().__init__()
        
        #screen set up:
        self.screen = game.screen
        self.screen_rect = self.screen.get_rect()

        #variable flags
        self.stage = 1
        self.blink_timer = 0
        self.shake_ass = False
        self.ass_meter = 3

        #loads
        self.temp = game.load.processor_temp

        self.record_temp = int(records['cpu temp record'])

        minus = int(records['cpu temp record'] / 10)
        self.final = int(records['cpu temp record'] - minus)
        self.first = round(self.final / 3)
        self.secound = round(self.first * 2)

        #loads idle1 images into list
        self.moon_idle1 = []
        self.moon_idle1.append(pygame.image.load('sailor_moon/idle1-1.png'))
        self.moon_idle1.append(pygame.image.load('sailor_moon/idle1-1.png'))
        self.moon_idle1.append(pygame.image.load('sailor_moon/idle1-2.png'))
        self.moon_idle1.append(pygame.image.load('sailor_moon/idle1-3.png'))
        self.moon_idle1.append(pygame.image.load('sailor_moon/idle1-4.png'))
        self.moon_idle1.append(pygame.image.load('sailor_moon/idle1-5.png'))
        self.moon_idle1.append(pygame.image.load('sailor_moon/idle1-6.png'))
        self.moon_idle1.append(pygame.image.load('sailor_moon/idle1-7.png'))
        self.moon_idle1.append(pygame.image.load('sailor_moon/idle1-8.png'))
        self.current_idle1 = 0
        
        #loads idle2 images into list
        self.moon_idle2 = []
        self.moon_idle2.append(pygame.image.load('sailor_moon/idle2_updated1-1.png'))
        self.moon_idle2.append(pygame.image.load('sailor_moon/idle2_updated1-2.png'))
        self.moon_idle2.append(pygame.image.load('sailor_moon/idle2_updated1-3.png'))
        self.moon_idle2.append(pygame.image.load('sailor_moon/idle2_updated1-4.png'))
        self.moon_idle2.append(pygame.image.load('sailor_moon/idle2_updated1-5.png'))
        self.moon_idle2.append(pygame.image.load('sailor_moon/idle2_updated1-6.png'))
        self.current_idle2 = 0

        #loads idle3 images into list
        self.moon_idle3 = []
        self.moon_idle3.append(pygame.image.load('sailor_moon/idle3-1.png'))
        self.moon_idle3.append(pygame.image.load('sailor_moon/idle3-2.png'))
        self.moon_idle3.append(pygame.image.load('sailor_moon/idle3-3.png'))
        self.moon_idle3.append(pygame.image.load('sailor_moon/idle3-4.png'))
        self.moon_idle3.append(pygame.image.load('sailor_moon/idle3-5.png'))
        self.current_idle3 = 0

        #loads transform1 images into list
        self.moon_transform1 = []
        self.moon_transform1.append(pygame.image.load('sailor_moon/transform1-1.png'))
        self.moon_transform1.append(pygame.image.load('sailor_moon/transform1-2.png'))
        self.moon_transform1.append(pygame.image.load('sailor_moon/transform1-3.png'))
        self.moon_transform1.append(pygame.image.load('sailor_moon/transform1-1.png'))
        self.moon_transform1.append(pygame.image.load('sailor_moon/transform1-2.png'))
        self.moon_transform1.append(pygame.image.load('sailor_moon/transform1-3.png'))       
        self.current_transform1 = 0

        #loads transform2 images into list
        self.moon_transform2 = []
        self.moon_transform2.append(pygame.image.load('sailor_moon/transform2-1.png'))
        self.moon_transform2.append(pygame.image.load('sailor_moon/transform2-2.png'))
        self.moon_transform2.append(pygame.image.load('sailor_moon/transform2-3.png'))
        self.moon_transform2.append(pygame.image.load('sailor_moon/transform2-4.png'))
        self.moon_transform2.append(pygame.image.load('sailor_moon/transform2-5.png'))
        self.moon_transform2.append(pygame.image.load('sailor_moon/transform2-6.png'))
        self.moon_transform2.append(pygame.image.load('sailor_moon/transform2-7.png'))
        self.moon_transform2.append(pygame.image.load('sailor_moon/transform2-8.png'))
        self.moon_transform2.append(pygame.image.load('sailor_moon/transform2-9.png'))
        self.current_transform2 = 0

        #loads detrasform1 images into list
        self.moon_detransform1 = []
        self.moon_detransform1.append(pygame.image.load('sailor_moon/detransform1-1.png'))
        self.moon_detransform1.append(pygame.image.load('sailor_moon/detransform1-2.png'))
        self.moon_detransform1.append(pygame.image.load('sailor_moon/detransform1-3.png'))
        self.moon_detransform1.append(pygame.image.load('sailor_moon/detransform1-4.png'))
        self.moon_detransform1.append(pygame.image.load('sailor_moon/detransform1-5.png'))
        self.current_detransform1 = 0

        self.moon_detransform2 = []
        self.moon_detransform2.append(pygame.image.load('sailor_moon/detransform2-1.png'))
        self.moon_detransform2.append(pygame.image.load('sailor_moon/detransform2-2.png'))
        self.moon_detransform2.append(pygame.image.load('sailor_moon/detransform2-3.png'))
        self.moon_detransform2.append(pygame.image.load('sailor_moon/detransform2-1.png'))
        self.moon_detransform2.append(pygame.image.load('sailor_moon/detransform2-2.png'))
        self.moon_detransform2.append(pygame.image.load('sailor_moon/detransform2-3.png'))       
        self.current_detransform2 = 0
        
        #sets image and rect
        self.image = self.moon_idle1[self.current_idle1]
        self.rect = self.image.get_rect()

        #places image and rect in correct location
        self.rect.midleft = game.sailor_mars.rect.midright
        self.rect.x -= 15

    def _civilian_animation(self, game):
        if self.stage == 1:
            if self.current_idle1 >= len(self.moon_idle1):
                self.current_idle1 = 0
        
            self.image = self.moon_idle1[self.current_idle1]
            self.current_idle1 += 1
            self.temp = game.processor_temp

        elif self.stage == 2:
            if self.current_detransform1 >= len(self.moon_detransform1):
                self.current_detransform1 = 0
                self.stage = 1
                
            if self.stage == 2:
                self.image = self.moon_detransform1[self.current_detransform1]
                self.current_detransform1 += 1

        elif self.stage == 3:
            if self.current_detransform2 >= len(self.moon_detransform2):
                self.current_detransform2 = 0
                self.stage = 2
                
            if self.stage == 3:
                self.image = self.moon_detransform2[self.current_detransform2]
                self.current_detransform2 += 1


    def _transformed_animation(self, game):
        if self.stage == 1:
            if self.current_transform1 >= len(self.moon_transform1):
                self.current_transform1 = 0
                self.stage = 2
        
            if self.stage == 1:
                self.image = self.moon_transform1[self.current_transform1]
                self.current_transform1 += 1
        
        elif self.stage == 2:
            #if self.current_idle2 >= len(self.moon_idle2):
            #    self.current_idle2 = 0
            #    self.temp = game.processor_temp
            if self.shake_ass == True:
                self._shake_ass()
            elif self.shake_ass == False:
                self._non_shake_ass()
                self.temp = game.processor_temp

        elif self.stage == 3:
            if self.current_detransform2 >= len(self.moon_detransform2):
                self.current_detransform2 = 0
                self.stage = 2
                
            if self.stage == 3:
                self.image = self.moon_detransform2[self.current_detransform2]
                self.current_detransform2 += 1

    def _shake_ass(self):
        if self.ass_meter >= 6:
            self.ass_meter = 2
            self.shake_ass = False
        else:
            self.image = self.moon_idle2[self.ass_meter]
            self.ass_meter += 1
    
    def _non_shake_ass(self):
        self.blink_timer += 1
        self.current_idle2 = 0
        random_number = random.randrange(50)
        if self.blink_timer >= 15:
            self.current_idle2 = 1
            self.blink_timer = 0
        elif random_number == 10:
            self.current_idle2 = 2
        elif random_number == 2:
            self.shake_ass = True
        self.image = self.moon_idle2[self.current_idle2]
 


    def _damaged_transformed_animation(self, game):
        if self.stage == 1:
            if self.current_transform1 >= len(self.moon_transform1):
                self.current_transform1 = 0
                self.stage = 2
            
            if self.stage == 1:
                self.image = self.moon_transform1[self.current_transform1]
                self.current_transform1 += 1
            
        elif self.stage == 2:
            #if self.current_transform2 >= len(self.moon_transform2):
            #    self.current_transform2 = 0
            #    self.stage = 3
            
            if self.stage == 2:
                #self.image = self.moon_transform2[self.current_transform2]
                #self.current_transform2 += 1
                self.stage = 3

        elif self.stage == 3:
            if self.current_idle3 >= len(self.moon_idle3):
                self.current_idle3 = 0
                self.temp = game.processor_temp

            self.image = self.moon_idle3[self.current_idle3]
            self.current_idle3 += 1


    def update(self, game):
        """this updates the animation for the load on the processor, higher load = less clothing"""
        self.new_record()
        if self.temp <= self.secound:
            self._civilian_animation(game)

        elif self.temp > self.secound and self.temp < self.final:
            self._transformed_animation(game)

        elif self.temp >= self.final:
            self._damaged_transformed_animation(game)

    def new_record(self):
        self.temp = int(self.temp)
        if self.temp > self.record_temp:
            self.record_temp = self.temp


class Ahegao(Sprite):
    def __init__(self, game, record):
        """initializes the ahegao face"""
        super().__init__()
        
        self.screen = game.screen
        self.screen_rect = game.screen_rect

        self.cpu_speed_record = record['cpu speed record']
        steps = self.cpu_speed_record / 5
        self.step1 = steps
        self.step2 = steps * 2
        self.step3 = steps * 3
        self.step4 = steps * 4

        #loads the ahegao images and makes rect
        self.image = pygame.image.load('sailor_moon/ahegao1-1.png')
        self.ahegao_1 = pygame.image.load('sailor_moon/ahegao1-1.png')
        self.ahegao_2 = pygame.image.load('sailor_moon/ahegao1-2.png')
        self.ahegao_3 = pygame.image.load('sailor_moon/ahegao1-3.png')
        self.ahegao_4 = pygame.image.load('sailor_moon/ahegao1-4.png')
        self.ahegao_5 = pygame.image.load('sailor_moon/ahegao1-5.png')
        self.rect = self.ahegao_1.get_rect()

        #position rect
        self.rect.midbottom = game.sailor_moon.rect.midtop
        self.rect.y = self.screen_rect.y

    def new_record(self, game):
        if game.processor_freq > self.cpu_speed_record:
            self.cpu_speed_record = game.processor_freq

    def update(self, game):
        self.new_record(game)
        if game.processor_freq > self.step4:
            self.image = self.ahegao_5
        elif game.processor_freq > self.step3:
            self.image = self.ahegao_4
        elif game.processor_freq > self.step2:
            self.image = self.ahegao_3
        elif game.processor_freq > self.step1:
            self.image = self.ahegao_2
        else:
            self.image = self.ahegao_1

class Moon_Walk(Sprite):
    def __init__(self, game):
        """initializes the sailor moon walking"""
        super().__init__()
        
        #screen set up:
        self.screen = game.screen
        self.screen_rect = self.screen.get_rect()

        #images
        self.moon_walk = []
        self.moon_walk.append(pygame.image.load('sailor_moon/walking1-1.png'))
        self.moon_walk.append(pygame.image.load('sailor_moon/walking1-2.png'))
        self.moon_walk.append(pygame.image.load('sailor_moon/walking1-3.png'))
        self.moon_walk.append(pygame.image.load('sailor_moon/walking1-4.png'))
        self.moon_walk.append(pygame.image.load('sailor_moon/walking1-5.png'))
        self.moon_walk.append(pygame.image.load('sailor_moon/walking1-6.png'))
        self.moon_walk.append(pygame.image.load('sailor_moon/walking1-7.png'))
        self.current_walk = 0

        #creates sailor moon and positions her
        self.image = self.moon_walk[self.current_walk]
        self.rect = self.image.get_rect()
        self.rect.topright = self.screen_rect.topright

    def update(self):
        if self.current_walk >= len(self.moon_walk):
            self.current_walk = 0

        self.image = self.moon_walk[self.current_walk]
        self.current_walk += 1

class Wand(Sprite):
    def __init__(self, game):
        """initilizes the wand"""
        super().__init__()

        #screen set up:
        self.screen = game.screen
        self.screen_rect = self.screen.get_rect()
        
        #load images ohh boy here we go
        self.gauge_names = []
        self.gauge_images = []
        all_gauge_names = os.listdir('sailor_moon/gauge/')
        for files in all_gauge_names:
            if 'up' not in files:
                self.gauge_names.append(files)
        self.gauge_names = natsorted(self.gauge_names)
        for files in self.gauge_names:
                self.gauge_images.append(pygame.image.load(f'sailor_moon/gauge/{files}'))           

        #images
        self.image = pygame.image.load('sailor_moon/gauge/gauge0.png')
        self.rect = self.image.get_rect()
        self.rect.topleft = self.screen_rect.topleft
    
    def update(self, game):
        self.load = game.load
        self.image = self.gauge_images[int(self.load.processor_temp)]
