#!/usr/bin/python3

import os
import random

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

class Sailor_Mars(Sprite):
    def __init__(self, game):
        """initilizaes sailor mars"""
        super().__init__()

        self.screen = game.screen
        self.screen_rect = self.screen.get_rect()

        #self.load = game.load.processor_load
        self.processor_load = game.load.processor_load

        #variable flags
        self.stage = 1
        self.fire = False
        self.blink_timer = 0
        self.fire_meter = 3

        self.mars_idle1 = []
        self.mars_idle1.append(pygame.image.load('sailor_mars/sailormars1-1.png'))
        self.mars_idle1.append(pygame.image.load('sailor_mars/sailormars1-2.png'))       
        self.mars_idle1.append(pygame.image.load('sailor_mars/sailormars1-3.png'))        
        self.mars_idle1.append(pygame.image.load('sailor_mars/sailormars1-4.png')) 
        self.mars_idle1.append(pygame.image.load('sailor_mars/sailormars1-5.png'))
        self.mars_idle1.append(pygame.image.load('sailor_mars/sailormars1-6.png'))        
        self.mars_idle1.append(pygame.image.load('sailor_mars/sailormars1-7.png'))
        self.current_idle1 = 0

        self.mars_idle2 = []
        self.mars_idle2.append(pygame.image.load('sailor_mars/sailor_mars_idle2-1.png'))
        self.mars_idle2.append(pygame.image.load('sailor_mars/sailor_mars_idle2-2.png'))       
        self.mars_idle2.append(pygame.image.load('sailor_mars/sailor_mars_idle2-3.png'))        
        self.mars_idle2.append(pygame.image.load('sailor_mars/sailor_mars_idle2-4.png')) 
        self.mars_idle2.append(pygame.image.load('sailor_mars/sailor_mars_idle2-5.png'))
        self.current_idle2 = 0

        self.mars_idle3 = []
        self.mars_idle3.append(pygame.image.load('sailor_mars/sailor_mars_idle3-1.png'))
        self.mars_idle3.append(pygame.image.load('sailor_mars/sailor_mars_idle3-2.png'))
        self.mars_idle3.append(pygame.image.load('sailor_mars/sailor_mars_idle3-3.png'))
        self.mars_idle3.append(pygame.image.load('sailor_mars/sailor_mars_idle3-4.png'))
        self.mars_idle3.append(pygame.image.load('sailor_mars/sailor_mars_idle3-5.png'))
        self.current_idle3 = 0

        self.mars_idle4 = []
        self.mars_idle4.append(pygame.image.load('sailor_mars/sailor_mars_idle4-1.png'))
        self.mars_idle4.append(pygame.image.load('sailor_mars/sailor_mars_idle4-2.png'))
        self.mars_idle4.append(pygame.image.load('sailor_mars/sailor_mars_idle4-3.png'))
        self.mars_idle4.append(pygame.image.load('sailor_mars/sailor_mars_idle4-4.png'))
        self.mars_idle4.append(pygame.image.load('sailor_mars/sailor_mars_idle4-5.png'))
        self.current_idle4 = 0

        self.mars_idle5 = []
        self.mars_idle5.append(pygame.image.load('sailor_mars/sailor_mars_stage2-1.png'))
        self.mars_idle5.append(pygame.image.load('sailor_mars/sailor_mars_stage2-2.png'))
        self.mars_idle5.append(pygame.image.load('sailor_mars/sailor_mars_stage2-3.png'))
        self.mars_idle5.append(pygame.image.load('sailor_mars/sailor_mars_stage2-4.png'))
        self.mars_idle5.append(pygame.image.load('sailor_mars/sailor_mars_stage2-5.png'))
        self.current_idle5 = 0

        self.mars_transform1 = []
        self.mars_transform1.append(pygame.image.load('sailor_mars/sailor_mars_transform1-1.png'))
        self.mars_transform1.append(pygame.image.load('sailor_mars/sailor_mars_transform1-2.png'))
        self.mars_transform1.append(pygame.image.load('sailor_mars/sailor_mars_transform1-3.png'))
        self.mars_transform1.append(pygame.image.load('sailor_mars/sailor_mars_transform1-4.png'))
        self.mars_transform1.append(pygame.image.load('sailor_mars/sailor_mars_transform1-5.png'))
        self.current_transform1 = 0

        self.image = self.mars_idle1[self.current_idle1]
        self.rect = self.image.get_rect()

        self.rect.midleft = game.sailor_venus.rect.midright
        self.rect.x -= 15

    def update(self, game):
        """this updates the animation for the load on the processor, higher load = less clothing"""
        if self.processor_load <= 35:
            if self.stage == 1:
                if self.fire == True:
                    self._start_fire()
                elif self.fire == False:
                    self._non_fire()
                    self.processor_load = game.processor_load

            elif self.stage == 2:
                if self.current_idle5 >= len(self.mars_idle5):
                    self.current_idle5 = 0
                    self.stage = 1
                    self.processor_load = game.processor_load

                self.image = self.mars_idle5[self.current_idle5]
                self.current_idle5 += 1

 
        elif self.processor_load >35 and self.processor_load <= 50:
            if self.stage == 1:
                if self.current_idle2 >= len(self.mars_idle2):
                    self.current_idle2 = 0
                    self.processor_load = game.processor_load       
                
                self.image = self.mars_idle2[self.current_idle2]
                self.current_idle2 += 1
            elif self.stage == 2:
                if self.current_idle5 >= len(self.mars_idle5):
                    self.current_idle5 = 0
                    self.stage = 1
                    self.processor_load = game.processor_load

                self.image = self.mars_idle5[self.current_idle5]
                self.current_idle5 += 1
          

        elif self.processor_load >50 and self.processor_load <= 70:
            if self.stage == 1:
                if self.current_idle3 >= len(self.mars_idle3):
                    self.current_idle3 = 0
                    self.processor_load = game.processor_load

                self.image = self.mars_idle3[self.current_idle3]
                self.current_idle3 += 1
            elif self.stage == 2:
                if self.current_idle5 >= len(self.mars_idle5):
                    self.current_idle5 = 0
                    self.stage = 1
                    self.processor_load = game.processor_load

                self.image = self.mars_idle5[self.current_idle5]
                self.current_idle5 += 1

        elif self.processor_load > 70 and self.processor_load <= 85:
            if self.stage == 1:
                if self.current_idle4 >= len(self.mars_idle4):
                    self.current_idle4 = 0
                    self.processor_load = game.processor_load

                self.image = self.mars_idle4[self.current_idle4]
                self.current_idle4 += 1
            elif self.stage == 2:
                if self.current_idle5 >= len(self.mars_idle5):
                    self.current_idle5 = 0
                    self.stage = 1
                    self.processor_load = game.processor_load

                self.image = self.mars_idle5[self.current_idle5]
                self.current_idle5 += 1

        elif self.processor_load > 85:
            if self.stage == 1:
                if self.current_transform1 >= len(self.mars_transform1):
                    self.current_transform1 = 0
                    self.stage = 2
            
                if self.stage == 1:
                    self.image = self.mars_transform1[self.current_transform1]
                    self.current_transform1 += 1

            elif self.stage == 2:
                if self.current_idle5 >= len(self.mars_idle5):
                    self.current_idle5 = 0
                    self.processor_load = game.processor_load

                self.image = self.mars_idle5[self.current_idle5]
                self.current_idle5 += 1

    def _start_fire(self):
        if self.fire_meter >= 7:
            self.fire_meter = 2
            self.fire = False
        else:
            self.image = self.mars_idle1[self.fire_meter]
            self.fire_meter += 1

    def _non_fire(self):
        self.blink_timer += 1
        self.current_idle1 = 0
        random_number = random.randrange(50)
        if self.blink_timer >= 15:
            self.current_idle1 = 1
            self.blink_timer = 0
        elif random_number == 10:
            self.fire = True
        self.image = self.mars_idle1[self.current_idle1]

class Mars_Ahegao(Sprite):
    def __init__(self, game):
        """initializes the ahegao face for sailor mars"""
        super().__init__()

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

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

    def update(self, game):
        if game.processor_load > 80:
            self.image = self.ahegao_5
        elif game.processor_load > 60:
            self.image = self.ahegao_4
        elif game.processor_load > 40:
            self.image = self.ahegao_3
        elif game.processor_load > 20:
            self.image = self.ahegao_2
        else:
            self.image = self.ahegao_1


class Mars_Up_Dog(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.up_dog = []
        self.up_dog.append(pygame.image.load('sailor_mars/up_dog1-1.png'))
        self.up_dog.append(pygame.image.load('sailor_mars/up_dog1-1.png'))
        self.up_dog.append(pygame.image.load('sailor_mars/up_dog1-2.png'))
        self.up_dog.append(pygame.image.load('sailor_mars/up_dog1-3.png'))
        self.up_dog.append(pygame.image.load('sailor_mars/up_dog1-4.png'))

        self.current_up_dog = 0

        #creates sailor moon and positions her
        self.image = self.up_dog[self.current_up_dog]
        self.rect = self.image.get_rect()
        self.rect.bottomright = self.screen_rect.bottomright

    def update(self):
        if self.current_up_dog >= len(self.up_dog):
            self.current_up_dog = 0

        self.image = self.up_dog[self.current_up_dog]
        self.current_up_dog += 1

class Mars_Gauge(Sprite):
    def __init__(self, game):
        """initilizes the Gauge"""
        super().__init__()

        #screen set up:
        self.screen = game.screen
        self.screen_rect = self.screen.get_rect()

        #self.load = Load()
        
        #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.processor_load = int(self.load.processor_load)
        self.image = self.gauge_images[self.processor_load]
