# Concave
# Copyright (C) 2012 Daniel Malmqvist
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from pygame import Rect,Surface,init,image
from pygame.locals import SRCALPHA,RLEACCEL
import g

def loadImage(sheet, indexX, indexY, gridSizeX=32, gridSizeY = 32):
    rect = Rect((indexX*gridSizeX,indexY*gridSizeY, gridSizeX, gridSizeY))
            
    image = Surface(rect.size, SRCALPHA)
    
    image.blit(sheet, (0, 0), rect)
    image.set_colorkey(-1, RLEACCEL)
    return image


def renderLines(lines, font, antialias, color, background=None):
    fontHeight = font.get_height()

    surfaces = [font.render(ln, antialias, color) for ln in lines]
    # can't pass background to font.render, because it doesn't respect the alpha

    maxwidth = max([s.get_width() for s in surfaces])
    result = Surface((maxwidth, len(lines)*fontHeight), SRCALPHA)
    if background == None:
        result.fill((90,90,90,0))
    else:
        result.fill(background)

    for i in range(len(lines)):
        result.blit(surfaces[i], (0,i*fontHeight))
    return result

def renderTextBlock(text, font, antialias, color, background=None):
    "This is renderTextBlock"
    brokenText = text.replace("\r\n","\n").replace("\r","\n")
    return renderLines(brokenText.split("\n"), font, antialias, color, background)


def addBorder(image):
    width = image.get_width()/16 + 2
    height = image.get_height()/16 + 2

    rect = Rect(0,0,width*16, height*16)
    borderImage = Surface(rect.size, SRCALPHA)

    
    for x in range (0, width):
        for y in range (0, height):
            if x == 0 and y == 0:
                borderImage.blit(itemBox[0][0], (16*x, 16*y))
            elif x == 0 and y == height -1:
                borderImage.blit(itemBox[0][2], (16*x, 16*y))
            elif x == 0:
                borderImage.blit(itemBox[0][1], (16*x, 16*y))
            elif y == 0 and x == width - 1:
                borderImage.blit(itemBox[2][0], (16*x, 16*y))
            elif y == 0:
                borderImage.blit(itemBox[1][0], (16*x, 16*y))
            elif x == width - 1 and y == height -1:
                borderImage.blit(itemBox[2][2], (16*x, 16*y))
            elif x == width - 1:
                borderImage.blit(itemBox[2][1], (16*x, 16*y))
            elif y == height -1:
                borderImage.blit(itemBox[1][2], (16*x, 16*y))
            else:
                borderImage.blit(itemBox[1][1], (16*x, 16*y))
    
    
    borderImage.blit(image, (4,4))
    
    return borderImage


def addBorderCentered(image, width = -1):
    if width == -1:
        width = image.get_width()/16 + 2
    height = image.get_height()/16 + 2

    rect = Rect(0,0,width*16, height*16)
    borderImage = Surface(rect.size, SRCALPHA)

    
    for x in range (0, width):
        for y in range (0, height):
            if x == 0 and y == 0:
                borderImage.blit(itemBox[0][0], (16*x, 16*y))
            elif x == 0 and y == height -1:
                borderImage.blit(itemBox[0][2], (16*x, 16*y))
            elif x == 0:
                borderImage.blit(itemBox[0][1], (16*x, 16*y))
            elif y == 0 and x == width - 1:
                borderImage.blit(itemBox[2][0], (16*x, 16*y))
            elif y == 0:
                borderImage.blit(itemBox[1][0], (16*x, 16*y))
            elif x == width - 1 and y == height -1:
                borderImage.blit(itemBox[2][2], (16*x, 16*y))
            elif x == width - 1:
                borderImage.blit(itemBox[2][1], (16*x, 16*y))
            elif y == height -1:
                borderImage.blit(itemBox[1][2], (16*x, 16*y))
            else:
                borderImage.blit(itemBox[1][1], (16*x, 16*y))
    
    x = (borderImage.get_width() - image.get_width()) / 2
    y = (borderImage.get_height() - image.get_height()) / 2
    
    borderImage.blit(image, (x,y))
    
    return borderImage


def addBorderFixedSize(image, x, y):
    width = x
    height = y

    rect = Rect(0,0,width*16, height*16)
    borderImage = Surface(rect.size, SRCALPHA)

    
    for x in range (0, width):
        for y in range (0, height):
            if x == 0 and y == 0:
                borderImage.blit(itemBox[0][0], (16*x, 16*y))
            elif x == 0 and y == height -1:
                borderImage.blit(itemBox[0][2], (16*x, 16*y))
            elif x == 0:
                borderImage.blit(itemBox[0][1], (16*x, 16*y))
            elif y == 0 and x == width - 1:
                borderImage.blit(itemBox[2][0], (16*x, 16*y))
            elif y == 0:
                borderImage.blit(itemBox[1][0], (16*x, 16*y))
            elif x == width - 1 and y == height -1:
                borderImage.blit(itemBox[2][2], (16*x, 16*y))
            elif x == width - 1:
                borderImage.blit(itemBox[2][1], (16*x, 16*y))
            elif y == height -1:
                borderImage.blit(itemBox[1][2], (16*x, 16*y))
            else:
                borderImage.blit(itemBox[1][1], (16*x, 16*y))
    
    x = (borderImage.get_width() - image.get_width()) / 2
    y = (borderImage.get_height() - image.get_height()) / 2
    
    borderImage.blit(image, (x,y))
    
    return borderImage
    
    
def drawImageTextBox(item, surface, yStart = 288, comparedTo = None):
    itemText = renderTextBlock(item.getText(comparedTo), g.fontNormal, 1, (255,255,255))
        
    width = itemText.get_width()/16 + 2
    height = itemText.get_height()/16 + 2
    
    xStart = 448 - width*16
    
    for x in range (0, width):
        for y in range (0, height):
            if x == 0 and y == 0:
                surface.blit(itemBox[0][0], (xStart+16*x, yStart+16*y))
            elif x == 0 and y == height -1:
                surface.blit(itemBox[0][2], (xStart+16*x, yStart+16*y))
            elif x == 0:
                surface.blit(itemBox[0][1], (xStart+16*x, yStart+16*y))
            elif y == 0 and x == width - 1:
                surface.blit(itemBox[2][0], (xStart+16*x, yStart+16*y))
            elif y == 0:
                surface.blit(itemBox[1][0], (xStart+16*x, yStart+16*y))
            elif x == width - 1 and y == height -1:
                surface.blit(itemBox[2][2], (xStart+16*x, yStart+16*y))
            elif x == width - 1:
                surface.blit(itemBox[2][1], (xStart+16*x, yStart+16*y))
            elif y == height -1:
                surface.blit(itemBox[1][2], (xStart+16*x, yStart+16*y))
            else:
                surface.blit(itemBox[1][1], (xStart+16*x, yStart+16*y))
    surface.blit(itemText, (xStart+4,yStart+4))
    
init()

itemBoxA = image.load("graphic/itembox.png")
itemBox = [ [ 0 for i in range(3) ] for j in range(3) ]
for x in range (0, 3):
    for y in range (0, 3):
        itemBox[x][y] = loadImage(itemBoxA,x,y, 16,16)
        