from clasesrpg.game import bcolors, Person
from clasesrpg.magic import Spell
from clasesrpg.inventory import Item
import random


'''
print("\n\n")
print("NAME                HP                                     MP")
print("                    _________________________              _______________")
print(bcolors.BOLD + "Naruto:    " + "460/460 |" + bcolors.OKGREEN +"██████████         " + bcolors.ENDC + bcolors.BOLD +
      "|      " + "65/65 |" + bcolors.OKBLUE + "██████████" + bcolors.ENDC + "|")

print("                    _________________________           __________ ")
print("Sasuke:     460/460|                         |    65/65|          |")

print("                    _________________________           __________ ")
print("Sakura:     460/460|                         |    65/65|          |")
'''

# magic = [{"Name" : "Fire", "Cost" : 10, "dmg" : 100},{"Name" : "Thunder", "Cost" : 12, "dmg" : 124},
#          {"Name" : "Blizzard", "Cost" : 10, "dmg" : 100}]

# player = Person(460, 65, 60, 34, magic)
# enemy = Person(1200, 65, 45, 25, magic)

# Create Black Magic
fire = Spell("Fire", 25, 600, "black")
thunder = Spell("Thunder", 25, 600, "black")
blizzard = Spell("Blizzard", 25, 600, "black")
meteor = Spell("Meteor", 40, 1200, "black")
quake = Spell("Quake", 14, 140, "black")

# Create White Magic
cure = Spell("Cure", 25, 620, "white")
cura = Spell("Cura", 32, 1500, "white")
curaga = Spell("Curaga", 50, 6000, "white")

# Create some Items
potion = Item("Potion", "potion", "Heals 50 HP", 50)
hipotion = Item("Hi-Potion", "potion", "Heals 100 HP", 100)
superpotion = Item("Super Potion", "potion", "Heals 1000 HP", 1000)
elixer = Item("Elixer", "elixer", "Fully restores HP/MP of one party member", 9999)
hielixer = Item("MegaElixer", "elixer", "Fully restores party's HP/MP", 9999)

grenade = Item("Grenade", "attack", "Deals 500 damage", 500)

player_spells = [fire, thunder, blizzard, meteor, quake, cure, cura]
enemy_spells = [fire, meteor, curaga]
player_items = [{"item" : potion, "quantity" : 15}, {"item" : hipotion, "quantity" : 5},
                {"item" : superpotion, "quantity" : 15},{"item" : elixer, "quantity" : 5},
                {"item" : hielixer, "quantity" : 2},{"item" : grenade, "quantity" : 5}]

# Instantiate People:
player1 = Person("Naruto:", 3260, 132, 300, 34, player_spells, player_items)
player2 = Person("Sasuke:", 4160, 188, 311, 34, player_spells, player_items)
player3 = Person("Sakura:", 3089, 174, 288, 34, player_spells, player_items)

enemy1 = Person("Kabuto", 1250, 130, 560, 325, enemy_spells, [])
enemy2 = Person("Kaguya", 18200, 701, 525, 25, enemy_spells,[])
enemy3 = Person("Madara", 1250, 130, 560, 325, enemy_spells, [])

players = [player1, player2, player3]
enemies = [enemy1, enemy2, enemy3]

running = True
i = 0

print(bcolors.FAIL + bcolors.BOLD + "AN ENEMY ATTACK!" + bcolors.ENDC)

while running:
    print("====================")
    print("\n\n")
    print("NAME                  HP                                 MP")
    for player in players:
        player.get_stats()
    print("\n")
    for enemy in enemies:
        enemy.get_enemy_stats()

    for player in players:
        player.choose_action()
        choice = input("    Choose action: ")
        index = int(choice) - 1

        if index == 0:
            dmg = player.generate_damage()
            enemy = player.choose_target(enemies)
            enemies[enemy].take_damage(dmg)
            print("\n" + "You attacked " + enemies[enemy].name + " for", dmg, "points of damage.")
            if enemies[enemy].get_hp() == 0:
                print(enemies[enemy].name + " has died.")
                del enemies[enemy]
        elif index == 1:
            player.choose_magic()
            magic_choice = int(input("    Choose magic: ")) - 1
            if magic_choice == -1:
                continue
            '''
            magic_dmg = player.generate_spell_damage(magic_choice)
            spell = player.get_sepll_name(magic_choice)
            cost = player.get_spell_mp_cost(magic_choice)
            '''
            spell = player.magic[magic_choice]
            magic_dmg = spell.generate_damage()

            current_mp = player.get_mp()

            # if cost > current_mp:
            if spell.cost > current_mp:
                print(bcolors.FAIL + "\nNot enough MP\n" + bcolors.ENDC)
                continue

            # player.reduce_mp(cost)
            player.reduce_mp(spell.cost)

            if spell.type == "white":
                player.heal(magic_dmg)
                print(bcolors.OKBLUE + "\n" + spell.name + " heals for", str(magic_dmg), "HP" + bcolors.ENDC)
            elif spell.type == "black":
                enemy = player.choose_target(enemies)
                enemies[enemy].take_damage(magic_dmg)
                print(bcolors.OKGREEN + "\n" + spell.name + " deals", str(magic_dmg), "points of damage to " +
                      enemies[enemy].name + bcolors.ENDC)
                if enemies[enemy].get_hp() == 0:
                    print(enemies[enemy].name + " has died.")
                    del enemies[enemy]
            # enemy.take_damage(magic_dmg)
            # print(bcolors.OKGREEN + "\n" + spell + "deals", str(magic_dmg), "points of damage" + bcolors.ENDC)
            # print(bcolors.OKGREEN + "\n" + spell.name + "deals", str(magic_dmg), "points of damage" + bcolors.ENDC)

        elif index == 2:
            player.choose_item()
            item_choice = int(input("    Choose item: ")) - 1
            if item_choice == -1:
                continue

            item = player.items[item_choice]["item"]
            if player.items[item_choice]["quantity"] == 0:
                print(bcolors.FAIL + "\n" + "None left..." + bcolors.ENDC)
                continue
            player.items[item_choice]["quantity"] -= 1

            if item.type == "potion":
                player.heal(item.prop)
                print(bcolors.OKGREEN + "\n" + item.name + " heals for", str(item.prop), "HP" + bcolors.ENDC)
            elif item.type == "elixer":
                if item.name == "MegaElixer":
                    for i in players:
                        i.hp = i.maxhp
                        i.mp = i.maxmp
                else:
                    player.hp = player.maxhp
                    player.mp = player.maxmp
                print(bcolors.OKGREEN + "\n" + item.name + " fully restores HP/MP" + bcolors.ENDC)
            elif item.type == "attack":
                enemy = player.choose_target(enemies)
                enemies[enemy].take_damage(item.prop)
                print(bcolors.FAIL + "\n" + item.name + " deals", str(item.prop), "points of damage to " +
                      enemies[enemy].name + bcolors.ENDC)
                if enemies[enemy].get_hp() == 0:
                    print(enemies[enemy].name + " has died.")
                    del enemies[enemy]

    # Check if battle is over
    defeated_enemies = 0
    defeated_players = 0
    for player in players:
        if player.get_hp() == 0:
            defeated_players += 1
    for enemy in enemies:
        if enemy.get_hp() == 0:
            defeated_enemies += 1
    if defeated_enemies == 3:
        print(bcolors.OKGREEN + "YOU WIN!" + bcolors.ENDC)
        running = False
    elif defeated_players == 3:
        print(bcolors.FAIL + "YOURS ENEMIES HAVE DEFEATED YOU" + bcolors.ENDC)
        running = False

    print("\n")
    # Enemy attack phase
    for enemy in enemies:
        enemy_choice = random.randrange(0, 2)
        if enemy_choice == 0:
            target = random.randrange(0, len(players))
            enemy_dmg = enemy.generate_damage()
            players[target].take_damage(enemy_dmg)
            print(bcolors.FAIL + enemy.name + " attacks " + players[target].name + " for", enemy_dmg, "points of damage.")
        elif enemy_choice == 1:
            spell, magic_dmg = enemy.choose_enemy_spell()
            enemy.reduce_mp(spell.cost)
            if spell.type == "white":
                enemy.heal(magic_dmg)
                print(bcolors.FAIL + "\n" + spell.name + " heals " + enemy.name + " for", str(magic_dmg), "HP"
                      + bcolors.ENDC)
            elif spell.type == "black":
                target = random.randrange(0, len(players))
                players[target].take_damage(magic_dmg)
                print(bcolors.FAIL + enemy.name + "'s " + spell.name + " deals", str(magic_dmg),
                      "points of damage to " + players[target].name + bcolors.ENDC)
                if players[target]. players[target]
            # print(bcolors.FAIL + bcolors.BOLD + enemy.name + " uses " + spell.name + " and inflicts " +
            #      str(magic_dmg) + " points of damage." +bcolors.ENDC)
