import pygame
import functools
import time
import inspect
import settings
import variables
import datetime


def check_key(_dict, *args):
    try:
        for arg in args:
            _dict = _dict[arg]
            print(_dict)
    except:
        return False
    else:
        return True


def timer(func):
    @functools.wraps(func)
    def _wrapper(*args, **kwargs):
        start = time.perf_counter()
        result = func(*args, **kwargs)
        runtime = time.perf_counter() - start
        print(f"{func.__name__} took {runtime:.4f} secs")
        return result
    return _wrapper


def erase_alpha(image):
    '''for x in range(image.get_width()):
        for y in range(image.get_height()):
            if image.get_at((x, y)) == (255, 255, 255, 255):
                image.set_at((x, y), (255, 255, 255, 0))'''
    image.set_colorkey((255, 255, 255))
    return image


def list_remove_all(_list:list, value):
    while _list.count(value) != 0:
        _list.remove(value)


delay_list = {}


def delay(second, _id=None, once=False):
    if _id is None:
        key = str(datetime.datetime.now())
    else:
        key = _id

    if once:
        if delay_list.get(key) is None:
            delay_list[key] = variables.global_tick

    if delay_list.get(key) is None or variables.global_tick - delay_list[key] > settings.TICK * second:
        delay_list[key] = variables.global_tick
        if once:
            delay_list.pop(key)
        return True
    else:
        return False


'''def delay_d(second):
    def delay_decorator(func):
        def _wrapper(*args, **kwargs):
            if delay_list.get(func) is None:
                delay_list[func] = variables.global_tick
            if variables.global_tick - delay_list[func] > settings.TICK * second:
                delay_list[func] = variables.global_tick
                func(*args, **kwargs)

        return _wrapper
    return delay_decorator'''


def get_seconds(datetime1, datetime2):
    t = datetime2 - datetime1
    t = float(str(t).split(':')[2])

    return t

'''def set_parametr(var, key):
    if parametres.get('hull'):
        self.hull = parametres['hull']'''