import pygame
from pygame.sprite import Sprite


class Coins(Sprite):
    """Coins"""
    def __init__(self, hub, x, y, name='coin', state='floating'):
        super().__init__()
        # Values
        self.name = name
        self.hub = hub
        self.original_pos = [x, y]

        self.rest_height = y

        self.rest_x = x
        self.velY = 0
        self.upwards = True
        self.state = state
        self.scale = (30, 50)
        self.scale2 = (14, 50)
        self.scale3 = (4, 50)

        # Screen Camera
        self.screen = self.hub.main_screen
        self.screen_rect = self.screen.get_rect()
        self.camera = hub.camera

        # Images
        self.index = 0
        self.change_freq = 120
        self.player_clock = pygame.time.get_ticks() + self.change_freq
        self.frameRate = 30
        self.clock = pygame.time.get_ticks() + self.frameRate
        self.image_index = [pygame.image.load("imgs/Items/coin1.png"),
                            pygame.image.load("imgs/Items/coin2.png"),
                            pygame.image.load("imgs/Items/coin3.png"),
                            pygame.image.load("imgs/Items/coin2.png")]

        self.image_index[0] = pygame.transform.scale(self.image_index[0], self.scale)
        self.image_index[1] = pygame.transform.scale(self.image_index[1], self.scale2)
        self.image_index[2] = pygame.transform.scale(self.image_index[2], self.scale3)
        self.image_index[3] = pygame.transform.scale(self.image_index[3], self.scale2)

        self.resting_index = [pygame.image.load("imgs/Items/CoinForBlackBG.png"),
                              pygame.image.load("imgs/Items/CoinForBlackBG1.png"),
                              pygame.image.load("imgs/Items/CoinForBlackBG2.png"),
                              pygame.image.load("imgs/Items/CoinForBlackBG1.png")]

        for i in range(len(self.resting_index)):
            self.resting_index[i] = pygame.transform.scale(self.resting_index[i], self.scale)

        if self.state == "floating":
            self.image = self.image_index[self.index]
        else:
            self.image = self.resting_index[self.index]

        self.rect = self.image.get_rect()

        self.rect.x = self.original_pos[0]
        self.rect.y = self.original_pos[1]

    def draw(self):
        self.screen.blit(self.image, self.rect)

    def update(self):
        self.check_state()

    def check_state(self):
        if self.state == "floating":
            self.start_anim()
        elif self.state == "resting":
            self.resting()

    def start_anim(self):
        """Starts coin spin animation"""
        self.velY = 5

        if self.rect.y == (self.rest_height - 60):
            self.upwards = False

        if self.upwards:
            self.rect.y -= self.velY
        else:
            self.rect.y += self.velY

        # start timer
        if pygame.time.get_ticks() > self.player_clock:
            self.player_clock = pygame.time.get_ticks() + self.change_freq
            if self.index == 0:
                self.original_pos[0] += 8
            elif self.index == 1:
                self.original_pos[0] += 5
            elif self.index == 2:
                self.original_pos[0] -= 5
            elif self.index == 3:
                self.original_pos[0] -= 8
            self.index += 1
            self.index %= len(self.image_index)
            self.image = self.image_index[self.index]

        if self.rect.y == self.rest_height:
            self.hub.gamemode.coins += 1
            self.hub.gamemode.check_coins()
            self.hub.gamemode.score += 200
            self.kill()

    def resting(self):
        """Starts coin rest animation"""
        # start timer
        if pygame.time.get_ticks() > self.player_clock:
            self.player_clock = pygame.time.get_ticks() + self.change_freq
            self.index += 1
            self.index %= len(self.resting_index)
            self.image = self.resting_index[self.index]

[((40, 30, 40, 85), 'pygame.transform.scale', 'pygame.transform.scale', ({(40, 53, 40, 72): 'self.image_index[0]', (40, 74, 40, 84): 'self.scale'}, {}), '(self.image_index[0], self.scale)', False, 'import pygame\n'), ((41, 30, 41, 86), 'pygame.transform.scale', 'pygame.transform.scale', ({(41, 53, 41, 72): 'self.image_index[1]', (41, 74, 41, 85): 'self.scale2'}, {}), '(self.image_index[1], self.scale2)', False, 'import pygame\n'), ((42, 30, 42, 86), 'pygame.transform.scale', 'pygame.transform.scale', ({(42, 53, 42, 72): 'self.image_index[2]', (42, 74, 42, 85): 'self.scale3'}, {}), '(self.image_index[2], self.scale3)', False, 'import pygame\n'), ((43, 30, 43, 86), 'pygame.transform.scale', 'pygame.transform.scale', ({(43, 53, 43, 72): 'self.image_index[3]', (43, 74, 43, 85): 'self.scale2'}, {}), '(self.image_index[3], self.scale2)', False, 'import pygame\n'), ((32, 28, 32, 51), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((34, 21, 34, 44), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((35, 28, 35, 69), 'pygame.image.load', 'pygame.image.load', ({(35, 46, 35, 68): '"""imgs/Items/coin1.png"""'}, {}), "('imgs/Items/coin1.png')", False, 'import pygame\n'), ((36, 28, 36, 69), 'pygame.image.load', 'pygame.image.load', ({(36, 46, 36, 68): '"""imgs/Items/coin2.png"""'}, {}), "('imgs/Items/coin2.png')", False, 'import pygame\n'), ((37, 28, 37, 69), 'pygame.image.load', 'pygame.image.load', ({(37, 46, 37, 68): '"""imgs/Items/coin3.png"""'}, {}), "('imgs/Items/coin3.png')", False, 'import pygame\n'), ((38, 28, 38, 69), 'pygame.image.load', 'pygame.image.load', ({(38, 46, 38, 68): '"""imgs/Items/coin2.png"""'}, {}), "('imgs/Items/coin2.png')", False, 'import pygame\n'), ((45, 30, 45, 80), 'pygame.image.load', 'pygame.image.load', ({(45, 48, 45, 79): '"""imgs/Items/CoinForBlackBG.png"""'}, {}), "('imgs/Items/CoinForBlackBG.png')", False, 'import pygame\n'), ((46, 30, 46, 81), 'pygame.image.load', 'pygame.image.load', ({(46, 48, 46, 80): '"""imgs/Items/CoinForBlackBG1.png"""'}, {}), "('imgs/Items/CoinForBlackBG1.png')", False, 'import pygame\n'), ((47, 30, 47, 81), 'pygame.image.load', 'pygame.image.load', ({(47, 48, 47, 80): '"""imgs/Items/CoinForBlackBG2.png"""'}, {}), "('imgs/Items/CoinForBlackBG2.png')", False, 'import pygame\n'), ((48, 30, 48, 81), 'pygame.image.load', 'pygame.image.load', ({(48, 48, 48, 80): '"""imgs/Items/CoinForBlackBG1.png"""'}, {}), "('imgs/Items/CoinForBlackBG1.png')", False, 'import pygame\n'), ((51, 36, 51, 93), 'pygame.transform.scale', 'pygame.transform.scale', ({(51, 59, 51, 80): 'self.resting_index[i]', (51, 82, 51, 92): 'self.scale'}, {}), '(self.resting_index[i], self.scale)', False, 'import pygame\n'), ((88, 11, 88, 34), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((111, 11, 111, 34), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((89, 32, 89, 55), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((112, 32, 112, 55), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n')]