import pygame
import pytmx
from game import ui
from pytmx.util_pygame import load_pygame

colors = {"damage":[255, 128, 64], "range":[177, 62, 255], "firerate":[244, 255, 38], "profit":[255, 228, 56]}
fielddata = {"1" : [[[1,1], [5, 2], "damage", 1.25, {"a":3}], [[12, 9], [2, 5], "damage", 1.50, {"cs":1, "f":2}], [[24, 6], [2, 1], "damage", 2, {"a":2}]],
             "2" : [[[1,3], [5, 2], "damage", 1.5, {"a":3}], [[11, 7], [5, 2], "range", 1.5, {"w":3}], [[11, 11], [5, 2], "range", 1.5, {"f":3}], [[23, 15], [4, 2], "damage", 2, {"a":1, "cs":1, "rs":1}]],
             "3" : [[[1, 4], [3, 5], "damage", 1.5, {"s":3, "w":2}], [[12, 4], [5, 2], "firerate", 1.5, {"rs":3, "g":1, "f":1}], [[20, 7], [2, 4], "range", 1.5, {"s":1, "a":2}], [[14, 16], [3, 3], "damage", 1.5, {"cs":1, "g":1, "a":1}]],
             "4" : [[[10, 9], [3, 2], "range", 2, {"ss":4, "f":1, "g":1, "w":1}], [[4, 4], [2, 2], "damage", 1.5, {"a":2}], [[20, 4], [2, 2], "firerate", 1.5, {"g":1, "s":2}], [[20, 14], [2, 2], "damage", 2, {"s":2, "w":1}], [[4, 14], [2, 2], "firerate", 2, {"s":2, "a":3}], [[24, 7], [3, 6], "profit", 1.75, {"a":5}]],
             "5" : [[[3, 5], [4, 2], "firerate", 2, {"a":3, "s":2}], [[5, 11], [5, 4], "profit", 2, {"s":2, "ss":1}], [[17, 12], [4, 2], "range", 2, {"g":2, "a":4}], [[18, 5], [8, 2], "damage", 2, {"a":5, "w":2}]],
             "6" : [[[6, 6], [4, 6], "firerate", 1.25, {"a":5, "ss":1}], [[12, 4], [3, 2], "profit", 3, {"g":3, "cs":1}], [[12, 13], [3, 3], "range", 1.75, {"rs":1, "a":3, "w":1}], [[17, 6], [4, 6], "damage", 2, {"a":7}]],
             "7" : [[[1, 8], [4, 4], "damage", 1.75, {"a":5, "s":2}], [[13, 4], [4, 3], "profit", 1.25, {"a":5, "ss":1}], [[13, 13], [4, 3], "firerate", 1.5, {"a":5, "cs":2}]],
             "8" : [[[1, 16], [3, 3], "range", 3, {"g":2, "ss":4}], [[23, 1], [3, 3], "range", 3, {"g":2, "ss":4}], [[12, 6], [5, 2], "damage", 1.5, {"w":1, "s":1, "a":4}], [[10, 10], [5, 2], "firerate", 1.5, {"cs":2, "a":4}]],
             "9" : [[[1, 11], [6, 6], "range", 1.75, {"ss":3, "w":1}], [[13, 4], [7, 3], "damage", 1.75, {"a":5, "g":1, "w":1, "f": 1}], [[15, 9], [7, 2], "profit", 1.5, {"a":5, "s":1}], [[13, 13], [7, 3], "firerate", 1.75, {"a":5, "cs":2, "g":1}]]}

#[[, ], [, ], "", 1., {"":}]

def loadmap(map):
    l = fielddata[str(map)]
    mapdata = pytmx.TiledMap("./maps/"+str(map)+".tmx")
    mapgrp = pygame.sprite.Group()
    fields = pygame.sprite.Group()
    for x in range(27):
        for y in range(20):
            p = mapdata.get_tile_properties(x, y, 0)
            t = Tile(p['type'], [x*48,y*48])
            mapgrp.add(t)
    for i in l:
        if i[1][0] < 3:
            field = Field(i[0], i[1], i[2], i[3], i[4], False)
        else:
            field = Field(i[0], i[1], i[2], i[3], i[4])
        fields.add(field)
    return mapgrp, fields

class Tile(pygame.sprite.Sprite):
    def __init__(self, type, pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("./images/tiles/"+str(type)+".png")
        self.rect = self.image.get_rect()
        self.rect.topleft = list(pos)
        self.type = str(type)
        self.occupied = False
    def update(self, pa, mouse, occupy=False, unoccupycoords=None):
        if self.rect.collidepoint(mouse):
            pa.htile = self
            if occupy != False:
                if occupy == "occupy" or occupy == True:
                    self.occupied = True
        elif occupy != False:
            if occupy == "unoccupy" and unoccupycoords != None:
                if self.rect.collidepoint(unoccupycoords):
                    self.occupied = False

class Field(pygame.sprite.Sprite):
    def __init__(self, pos, size, type, boost, req, drawtext=True):
        global colors, font, ui
        pygame.sprite.Sprite.__init__(self)
        self.oimage = pygame.surface.Surface([(size[0]*48)-6, (size[1]*48)-6])
        oldsize = ui.size
        ui.Font(12)
        self.type = type
        self.itext = ui.font.render(self.type + " boost - inactive", 1, [255, 255, 255])
        self.atext = ui.font.render(self.type + " boost - active", 1, [255, 255, 255])
        ui.Font(oldsize)
        self.rect = self.oimage.get_rect()
        try:
            self.oimage.fill(colors[self.type])
        except:
            self.oimage.fill([255, 255, 255])
        oimage = self.oimage
        self.image = oimage
        self.image.set_alpha(128)
        self.rect.topleft = [(pos[0]*48)+3, (pos[1]*48)+3]
        self.requirements = req
        self.oreq = req
        self.drawtext = drawtext
        self.boost = float(boost)
    def requirements_met(self):
        rv = False
        reqlength = len(self.requirements.keys())
        reqcount = 0
        for i in self.requirements.keys():
            if self.requirements[i] <= 0:
                reqcount += 1
        if reqcount == reqlength:
            rv = True
        return rv
    def update(self, pa, mouse, towers, window, clock, enemies, bulletgrp, fields, sounds, sfx):
        self.image.set_alpha(255)
        if self.drawtext:
            if self.requirements_met():
                window.blit(self.atext, [self.rect.left + 1, self.rect.top+1])
            else:
                window.blit(self.itext, [self.rect.left + 1, self.rect.top+1])
        if self.requirements_met():
            towers.update(pa, mouse, clock, enemies, bulletgrp, fields, sounds, sfx, getboost=[self.type, self.boost])
        if self.rect.collidepoint(mouse):
            pa.hfield = self
            self.image.set_alpha(192)
        else:
            self.image.set_alpha(128)
        self.requirements = dict(self.oreq)