#!/usr/bin/python3

import time
import random

import pygame 
from pygame.sprite import Sprite


class Sailor_Saturn(Sprite):
    def __init__(self, game):
        super().__init__()

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

        self.sailor_saturn_idle1 = []
        self.sailor_saturn_idle1.append(pygame.image.load('sailor_saturn/sailor_saturn_idle1-1.png'))
        self.sailor_saturn_idle1.append(pygame.image.load('sailor_saturn/sailor_saturn_idle1-2.png'))
        self.sailor_saturn_idle1.append(pygame.image.load('sailor_saturn/sailor_saturn_idle1-3.png'))
        self.sailor_saturn_idle1.append(pygame.image.load('sailor_saturn/sailor_saturn_idle1-4.png'))

        self.sailor_saturn_idle1_peeler = []
        self.sailor_saturn_idle1_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler1-1.png'))
        self.sailor_saturn_idle1_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler1-2.png'))
        self.sailor_saturn_idle1_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler1-3.png'))
        self.sailor_saturn_idle1_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler1-4.png'))
        self.current_idle1 = 0

        self.sailor_saturn_idle2 = []
        self.sailor_saturn_idle2.append(pygame.image.load('sailor_saturn/sailor_saturn_idle2-1.png'))
        self.sailor_saturn_idle2.append(pygame.image.load('sailor_saturn/sailor_saturn_idle2-2.png'))
        self.sailor_saturn_idle2.append(pygame.image.load('sailor_saturn/sailor_saturn_idle2-3.png'))
        self.sailor_saturn_idle2.append(pygame.image.load('sailor_saturn/sailor_saturn_idle2-4.png'))
        self.sailor_saturn_idle2.append(pygame.image.load('sailor_saturn/sailor_saturn_idle2-5.png'))
        self.sailor_saturn_idle2.append(pygame.image.load('sailor_saturn/sailor_saturn_idle2-6.png'))
        self.sailor_saturn_idle2.append(pygame.image.load('sailor_saturn/sailor_saturn_idle2-7.png'))
        
        self.sailor_saturn_idle2_peeler = []
        self.sailor_saturn_idle2_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler2-1.png'))
        self.sailor_saturn_idle2_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler2-2.png'))
        self.sailor_saturn_idle2_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler2-3.png'))
        self.sailor_saturn_idle2_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler2-4.png'))
        self.sailor_saturn_idle2_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler2-5.png'))
        self.sailor_saturn_idle2_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler2-6.png'))
        self.sailor_saturn_idle2_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler2-7.png'))
        self.current_idle2 = 0

        self.sailor_saturn_idle3 = []
        self.sailor_saturn_idle3.append(pygame.image.load('sailor_saturn/sailor_saturn_idle3-1.png'))
        self.sailor_saturn_idle3.append(pygame.image.load('sailor_saturn/sailor_saturn_idle3-2.png'))
        self.sailor_saturn_idle3.append(pygame.image.load('sailor_saturn/sailor_saturn_idle3-3.png'))
        self.sailor_saturn_idle3.append(pygame.image.load('sailor_saturn/sailor_saturn_idle3-4.png'))
        self.sailor_saturn_idle3.append(pygame.image.load('sailor_saturn/sailor_saturn_idle3-5.png'))
        self.sailor_saturn_idle3.append(pygame.image.load('sailor_saturn/sailor_saturn_idle3-6.png'))
        self.sailor_saturn_idle3.append(pygame.image.load('sailor_saturn/sailor_saturn_idle3-7.png'))
        self.sailor_saturn_idle3.append(pygame.image.load('sailor_saturn/sailor_saturn_idle3-8.png'))
        self.sailor_saturn_idle3.append(pygame.image.load('sailor_saturn/sailor_saturn_idle3-9.png'))
        self.sailor_saturn_idle3.append(pygame.image.load('sailor_saturn/sailor_saturn_idle3-10.png'))
        
        self.sailor_saturn_idle3_peeler = []
        self.sailor_saturn_idle3_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler3-1.png'))
        self.sailor_saturn_idle3_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler3-2.png'))
        self.sailor_saturn_idle3_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler3-3.png'))
        self.sailor_saturn_idle3_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler3-4.png'))
        self.sailor_saturn_idle3_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler3-5.png'))
        self.sailor_saturn_idle3_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler3-6.png'))
        self.sailor_saturn_idle3_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler3-7.png'))
        self.sailor_saturn_idle3_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler3-8.png'))
        self.sailor_saturn_idle3_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler3-9.png'))
        self.sailor_saturn_idle3_peeler.append(pygame.image.load('sailor_saturn/sailor_saturn_idle_peeler3-10.png'))
        self.current_idle3 = 0

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

        self.rect.midbottom = self.screen_rect.midbottom
        self.rect.left = self.screen_rect.left
        self.rect.x += 20
        self.orginal_rect = self.rect
        self.money_pit_rect = pygame.Rect((20, 140), (140, 230))

        #flags
        self.grab_tit = False
        self.slam = False
        self.wet = False
        self.blink = 0
        self.peel = False
        self.end_time = 0


        #moeny
        self.money = 0
        self.money_earned = 0


    def update(self, game):
        if game.gpu_temp < 40:
            if self.grab_tit == False:
                self.non_tit_animation()
            elif self.grab_tit == True:
                self.tit_animation()

        elif game.gpu_temp >= 40 and game.gpu_temp < 60:
            if self.wet == False:
                self.not_wet_animation()
            elif self.wet == True:
                self.wet_animation()

        elif game.gpu_temp >= 60:
            if self.slam == False:
                self.not_slam_animation()
            elif self.slam == True:
                self.slam_animation()
        self._peeler(game)
        self._timer()

    def non_tit_animation(self):
        random_number = random.randrange(25)
        if random_number == 5:
            self.grab_tit = True
        else:
            if self.peel == False:
                self.image = self.sailor_saturn_idle1[0]
            elif self.peel == True:
                self.image = self.sailor_saturn_idle1_peeler[0]

    def tit_animation(self):
        if self.current_idle1 >= len(self.sailor_saturn_idle1):
            self.current_idle1 = 0
            self.grab_tit = False

        if self.peel == False:
            self.image = self.sailor_saturn_idle1[self.current_idle1]
            self.current_idle1 += 1
        elif self.peel == True:
            self.image = self.sailor_saturn_idle1_peeler[self.current_idle1]
            self.current_idle1 += 1

    def slam_animation(self):
        if self.current_idle2 < 2:
            self.current_idle2 = 2
        if self.current_idle2 >= len(self.sailor_saturn_idle2):
            self.current_idle2 = 0
            self.slam = False
        
        if self.peel == False:
            self.image = self.sailor_saturn_idle2[self.current_idle2]
            self.current_idle2 += 1
        elif self.peel == True:
            self.image = self.sailor_saturn_idle2_peeler[self.current_idle2]
            self.current_idle2 += 1

    def not_slam_animation(self):
        random_number = random.randrange(25)
        self.blink += 1
        if random_number == 5:
            self.slam = True
        if self.blink >= 15:
            if self.peel == False:
                self.image = self.sailor_saturn_idle2[1]
                self.blink = 0
            elif self.peel == True:
                self.image = self.sailor_saturn_idle2_peeler[1]
                self.blink = 0
        elif self.blink < 15:
            if self.peel == False:
                self.image = self.sailor_saturn_idle2[0]
            elif self.peel == True:
                self.image = self.sailor_saturn_idle2_peeler[0]
 

    def wet_animation(self):
        if self.current_idle3 >= len(self.sailor_saturn_idle3):
            self.current_idle3 = 0
            self.wet = False

        if self.peel == False:
            self.image = self.sailor_saturn_idle3[self.current_idle3]
            self.current_idle3 += 1
        elif self.peel == True:
            self.image = self.sailor_saturn_idle3_peeler[self.current_idle3]
            self.current_idle3 += 1

    def not_wet_animation(self):
        random_number = random.randrange(25)
        self.blink += 1
        if random_number == 5:
            self.wet = True
        elif self.blink >= 15:
            if self.peel == False:
                self.image = self.sailor_saturn_idle3[1]
                self.blink = 0
            elif self.peel == True:
                self.iamge = self.sailor_saturn_idle3_peeler[1]
                self.blink = 0
        elif self.blink < 15:
            if self.peel == False:
                self.image = self.sailor_saturn_idle3[0]
            elif self.peel == True:
                self.iamge = self.sailor_saturn_idle3_peeler[0]

    def _peeler(self, game):
        self.rect = self.money_pit_rect
        if self.peel == False:
            collision = pygame.sprite.spritecollide(game.sailor_saturn, game.moneys, True) 
            if collision:
                if self.peel == False:
                    self.money += 1
                else:
                    pass
            if self.money >= 2:
                self.peel = True
                self.money = 0
                self.end_time = time.perf_counter() + 20
        self.rect = self.orginal_rect

    def _timer(self):
        if self.peel == False:
            pass
        else:
            updated_time = time.perf_counter()
            if updated_time > self.end_time:
                self.peel = False

