import pygame
from pygame.locals import *

from locals import *

import os

class Item:
    def __init__(self, name, amount):
        self.name = name
        self.amount = amount

        self.itemtype = "item"
        self.ammo = None
        self.stun = 0
        self.damage = 0
        self.accuracy = 1
        self.range = 0.5
        self.accrange = 0
        self.char_animation = "idle"

        f = open(os.path.join("items", name) + ".txt")
        for line in f.readlines():
            values = line.split()
            variable, values = values[0], values[1:]
            if variable == "type":
                self.itemtype = values[0]
            elif variable == "damage":
                self.damage = float(values[0])
            elif variable == "stun":
                self.stun = int(values[0])
            elif variable == "ammo":
                self.ammo = values[0]
            elif variable == "accuracy":
                self.accuracy = float(values[0])
            elif variable == "char_animation":
                self.char_animation = values[0]
            elif variable == "range":
                self.range = float(values[0])
            elif variable == "accurate_range":
                self.accrange = float(values[0])
                
        if self.accrange == 0:
           self.accrange = range

        if self.is_weapon():
            try:
                self.ui_image = pygame.image.load(os.path.join("ui", name + ".png")).convert_alpha()
                self.ui_rect = self.ui_image.get_rect()
            except:
                pass


    def is_weapon(self):
        return (self.itemtype == "weapon_ranged" or self.itemtype == "weapon_melee")
