# 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/>.


import pygame
import constants
import helperfunctions

class Weapon():
    def __init__(self):
        self.dagger = dict()
        self.spear = dict()
        self.staff = dict()
        self.rapier = dict()
        self.longsword = dict()
        self.longspear = dict()
        
        #self.spell = dict()
        
        self.loadImages()
        
    def loadImages(self):
        daggerSheet = pygame.image.load("graphic/slash/WEAPON_dagger.png")
        spearSheet = pygame.image.load("graphic/thrust/WEAPON_spear.png")
        staffSheet = pygame.image.load("graphic/thrust/WEAPON_staff.png")
        rapierSheet = pygame.image.load("graphic/slash/WEAPON_rapier.png")
        longswordSheet = pygame.image.load("graphic/slash/WEAPON_longsword.png")
        longspearSheet = pygame.image.load("graphic/thrust/WEAPON_long_spear.png")
        
        
        for d in [constants.NORTH(), constants.WEST(), constants.EAST(), constants.SOUTH()]:
            for scale in [-2, -1, 0, 1, 2]:
                self.dagger[(d, scale)] = []
                self.spear[(d, scale)] = []
                self.staff[(d, scale)] = []
                self.rapier[(d, scale)] = []
                self.longsword[(d, scale)] = []
                self.longspear[(d, scale)] = []
            
        
        for i in range(0, 6):
            self.dagger[(constants.NORTH(), 0)].append(helperfunctions.loadImage(daggerSheet, i, 0, 64, 64))
            self.dagger[(constants.WEST(), 0)].append(helperfunctions.loadImage(daggerSheet, i, 1, 64, 64))
            self.dagger[(constants.SOUTH(), 0)].append(helperfunctions.loadImage(daggerSheet, i, 2, 64, 64))
            self.dagger[(constants.EAST(), 0)].append(helperfunctions.loadImage(daggerSheet, i, 3, 64, 64))
    
        for i in range(0, 8):
            self.spear[(constants.NORTH(), 0)].append(helperfunctions.loadImage(spearSheet, i, 0, 64, 64))
            self.spear[(constants.WEST(), 0)].append(helperfunctions.loadImage(spearSheet, i, 1, 64, 64))
            self.spear[(constants.SOUTH(), 0)].append(helperfunctions.loadImage(spearSheet, i, 2, 64, 64))
            self.spear[(constants.EAST(), 0)].append(helperfunctions.loadImage(spearSheet, i, 3, 64, 64))
    
        for i in range(0, 8):
            self.staff[(constants.NORTH(), 0)].append(helperfunctions.loadImage(staffSheet, i, 0, 64, 64))
            self.staff[(constants.WEST(), 0)].append(helperfunctions.loadImage(staffSheet, i, 1, 64, 64))
            self.staff[(constants.SOUTH(), 0)].append(helperfunctions.loadImage(staffSheet, i, 2, 64, 64))
            self.staff[(constants.EAST(), 0)].append(helperfunctions.loadImage(staffSheet, i, 3, 64, 64))
        
        for i in range(0, 6):
            self.rapier[(constants.NORTH(), 0)].append(helperfunctions.loadImage(rapierSheet, i, 0, 192, 192))
            self.rapier[(constants.WEST(), 0)].append(helperfunctions.loadImage(rapierSheet, i, 1, 192, 192))
            self.rapier[(constants.SOUTH(), 0)].append(helperfunctions.loadImage(rapierSheet, i, 2, 192, 192))
            self.rapier[(constants.EAST(), 0)].append(helperfunctions.loadImage(rapierSheet, i, 3, 192, 192))
    
        for i in range(0, 6):
            self.longsword[(constants.NORTH(), 0)].append(helperfunctions.loadImage(longswordSheet, i, 0, 192, 192))
            self.longsword[(constants.WEST(), 0)].append(helperfunctions.loadImage(longswordSheet, i, 1, 192, 192))
            self.longsword[(constants.SOUTH(), 0)].append(helperfunctions.loadImage(longswordSheet, i, 2, 192, 192))
            self.longsword[(constants.EAST(), 0)].append(helperfunctions.loadImage(longswordSheet, i, 3, 192, 192))
        
        for i in range(0, 8):
            self.longspear[(constants.NORTH(), 0)].append(helperfunctions.loadImage(longspearSheet, i, 0, 192, 192))
            self.longspear[(constants.WEST(), 0)].append(helperfunctions.loadImage(longspearSheet, i, 1, 192, 192))
            self.longspear[(constants.SOUTH(), 0)].append(helperfunctions.loadImage(longspearSheet, i, 2, 192, 192))
            self.longspear[(constants.EAST(), 0)].append(helperfunctions.loadImage(longspearSheet, i, 3, 192, 192))
    
        daggerSheet = spearSheet = staffSheet = rapierSheet = longswordSheet = longspearSheet = 0
    
    
        for scale in [-2, -1, 1, 2]:
            for w in [self.dagger, self.longspear, self.longsword, self.rapier, self.spear, self.staff]:
                for d in [constants.NORTH(), constants.WEST(), constants.EAST(), constants.SOUTH()]:
                    if scale == 2:
                        for i in w[(d, 0)]: 
                            w[(d, scale)].append(pygame.transform.scale2x(pygame.transform.scale2x(i))) 
    
    
    
    def getImage(self, type, direction, imageId, scale = 0):
        imageId += 1
        if type == constants.DAGGER():
            return self.dagger[(direction, scale)][imageId%len(self.dagger[direction, scale])]
        
        elif type == constants.SPEAR():
            return self.spear[(direction, scale)][imageId%len(self.spear[direction, scale])]
        
        elif type == constants.STAFF():
            return self.staff[(direction, scale)][imageId%len(self.staff[direction, scale])]
        
        elif type == constants.RAPIER():
            return self.rapier[(direction, scale)][imageId%len(self.rapier[direction, scale])]
        
        elif type == constants.LONGSWORD():
            return self.longsword[(direction, scale)][imageId%len(self.longsword[direction, scale])]
        
        elif type == constants.LONGSPEAR():
            return self.longspear[(direction, scale)][imageId%len(self.longspear[direction, scale])]
        else:
            print "WEAPON NOT FOUND", type, direction, imageId
            return None
        
    def getImagePos(self, type, (x, y), scale = 0):
        
        if type in [constants.DAGGER(), constants.SPEAR(), constants.STAFF()]:
            return (x, y)
        else:
            if scale == 0:
                return (x-64, y-64)
            elif scale == 2:
                return (x-256, y-256)
        
    def getOffsetX(self, type):
        if type == constants.DAGGER():
            return 0
        
        elif type == constants.SPEAR():
            return 0
        
        elif type == constants.STAFF():
            return 0
        
        elif type == constants.RAPIER():
            return -(192-64/2)
        
        elif type == constants.LONGSWORD():
            return -(192-64/2)
        
        elif type == constants.LONGSPEAR():
            return -(192-64/2)

