from settings import *
import pygame as pg
from pytmx.util_pygame import load_pygame
import os

CHAR_ROWS = 4
CHAR_COLS = 4
NOMEKOP_ROWS = 2
NOMEKOP_COLS = 4

class Sproot:
	def __init__(self, image=None, rect=None, direction=None, all_images=None, kind=None, active=False):
		self.image = image
		self.rect = rect
		self.direction = direction
		self.all_images = all_images
		self.kind = kind
		self.active = active


def draw(screen, image, x_pos, y_pos):
    screen.blit(image, [x_pos, y_pos])

      
def load_image(image_name):
    # loads a nomekop image icon given the name of the image, e.g., "Draem.png"
    path = os.path.join("..", "graphics", "icons", image_name)
    return pg.image.load(path).convert_alpha()

def flip_image(image):
    # flips an image horizontally
    return pg.transform.flip(image, True, False)

def load_char(image_name, flip=False):
    path = os.path.join("..", "graphics", "characters", image_name)
    images = load_all_images(path, 4, 4)
    image = images[4]
    if flip:
        image = flip_image(image)
    return image

def load_frames(nomekop, flip):
    # loads all frames relevant to a nomekop
    frames = []
    image_name = nomekop + ".png"
    path = os.path.join("..", "graphics", "nomekop", image_name)
    frames = load_all_images(path, 2, 4)[0:4]
    if nomekop in LEGENDARY:
        path = os.path.join("..", "graphics", "attacks", "ice.png")
        frames = frames + load_all_images(path, 1, 4)
    elif nomekop in FIRE:
        path = os.path.join("..", "graphics", "attacks", "fire.png")
        frames = frames + load_all_images(path, 1, 4)

    elif nomekop in PLANT:
        path = os.path.join("..", "graphics", "attacks", "plant.png")
        frames = frames + load_all_images(path, 1, 4)

    elif nomekop in WATER:
        path = os.path.join("..", "graphics", "attacks", "water.png")
        frames = frames + load_all_images(path, 1, 4)

    if flip:
        frames = [flip_image(image) for image in frames]
    return frames

def load_icons(nomekop_list, flip):
    icons = []
    i = 0
    while i < len(nomekop_list):
        image_name = nomekop_list[i] + ".png"
        image = load_image(image_name)
        if flip:
            image = flip_image(image)
        icons = icons + [image]
        i = i + 1
    return icons

def player_is_blocked_by_object(player, collidable_sprites):
	r1 = player.rect
	
	i = 0
	while i < len(collidable_sprites):
		r2 = collidable_sprites[i].rect
		i += 1
		if player.direction == "right" and (r1.right - 50 > r2.left and r1.left < r2.left) and (r2.top <= r1.centery <= r2.bottom):
			return True
		elif player.direction == "left" and (r1.left + 50 < r2.right and r1.right > r2.right) and (r2.top <= r1.centery <= r2.bottom):
			return True
		elif player.direction == "down" and (r1.bottom - 50 > r2.top and r1.top < r2.top) and (r2.left <= r1.centerx <= r2.right):
			return True
		elif player.direction == "up" and (r1.top + 50 < r2.bottom and r1.bottom > r2.bottom) and (r2.left <= r1.centerx <= r2.right):
			return True
	
	return False
	
def return_y(s):
	if s.kind == "monster_patch":
		return s.rect.centery - 40
	else:
		return s.rect.centery


def import_maps(map_names):

    maps = {}

    for name in map_names:
        path = os.path.join("..", "data", "maps", name)
        maps[name] = load_pygame(path)

    return maps

def load_all_images(full_path, rows, cols):
	
	all_imgs = pg.image.load(full_path).convert_alpha()
	cell_width, cell_height = all_imgs.get_width() / cols, all_imgs.get_height() / rows

	frames = []
	for row in range(rows):
		for col in range(cols):
			img = all_imgs.subsurface((col * cell_width, row * cell_height, cell_width, cell_height))
			frames.append(img)
		
	return frames
