# -*- coding: utf-8 -*-
import logging

import pygame

from game import ptext, tweening
from game.tweening import tweener

logger = logging.getLogger(__name__)


class Effect:

    def __init__(self, duration):
        self.duration = duration

    def update(self, dt):
        self.duration -= dt
        return self.duration > 0

    def draw(self, screen, cam, fdt):
        pass


class MessageEffect(Effect):

    def __init__(self, message, duration):
        super().__init__(duration)
        self.message = message
        label = ptext.getsurf(self.message, align="center", background=None, fontsize=30)
        # rect = label.get_rect().inflate(30, 30)
        # self.image = pygame.Surface(rect.size, pygame.SRCALPHA)
        # pygame.draw.rect(self.image, (150, 150, 150), rect, 0, 3)
        # pygame.draw.rect(self.image, (200, 200, 200), rect, 3, 3)
        # self.image.blit(label, label.get_rect(center=rect.center))
        self.image = label
        self.rect = self.image.get_rect(midbottom=(1024 / 2, 768 - 30))
        self.alpha = 255
        tweener.create_tween_by_end(self, "alpha", 255, 0, duration, tweening.ease_in_expo)

    def draw(self, screen, cam, fdt):
        img = self.image.copy()
        img.fill((255, 255, 255, self.alpha), None, pygame.BLEND_RGBA_MULT)
        screen.blit(img, self.rect)


class ThoughtEffect(Effect):

    def __init__(self, start_pos, end_pos, duration):
        super().__init__(duration)
        screen_shot = pygame.display.get_surface().copy()
        self.image = pygame.Surface(screen_shot.size, pygame.SRCALPHA)
        self.image.blit(screen_shot, (0, 0))
        self.scale = 1
        self.alpha = 255
        self.position = start_pos.copy()
        ease = tweening.ease_out_expo
        tweener.create_tween_by_end(self, "scale", 1, 0.05, duration, ease)
        tweener.create_tween_by_end(self, "alpha", 255, 0, duration, ease)
        tweener.create_tween_by_end(self, "position", start_pos, end_pos.copy(), duration,
                                    ease, delta_update=False)

    def draw(self, screen, cam, fdt):
        img = pygame.transform.scale_by(self.image, self.scale)
        img.fill((255, 255, 255, self.alpha), None, pygame.BLEND_RGBA_MULT)
        r = img.get_rect(center=cam.world_to_screen(self.position))
        screen.blit(img, r)
