import {EquipmentSlot, world,ItemStack } from "@minecraft/server";


/*
---------------------------------------------
  WARNING:
    THIS CODE IS SHIT
    WHEN SEEN THIS CODE WILL SLOWY BURN YOUR BRAINCELLS AND MY EGO
    I MADE IT I ASSUME!!!!
    -Enedyx
---------------------------------------------
*/

const armorSlots = [EquipmentSlot.Head, EquipmentSlot.Chest, EquipmentSlot.Legs, EquipmentSlot.Feet];

export const minecraftFood = [
    { id: "minecraft:apple", nutrition: 4 },
    { id: "minecraft:golden_apple", nutrition: 4 },
    { id: "minecraft:enchanted_golden_apple", nutrition: 4 },
    { id: "minecraft:carrot", nutrition: 3 },
    { id: "minecraft:golden_carrot", nutrition: 6 },
    { id: "minecraft:baked_potato", nutrition: 5 },
    { id: "minecraft:potato", nutrition: 1 },
    { id: "minecraft:poisonous_potato", nutrition: 2 },
    { id: "minecraft:bread", nutrition: 5 },
    { id: "minecraft:cooked_beef", nutrition: 8 },
    { id: "minecraft:beef", nutrition: 3 },
    { id: "minecraft:cooked_porkchop", nutrition: 8 },
    { id: "minecraft:porkchop", nutrition: 3 },
    { id: "minecraft:cooked_chicken", nutrition: 6 },
    { id: "minecraft:chicken", nutrition: 2 },
    { id: "minecraft:cooked_mutton", nutrition: 6 },
    { id: "minecraft:mutton", nutrition: 2 },
    { id: "minecraft:cooked_rabbit", nutrition: 5 },
    { id: "minecraft:rabbit", nutrition: 3 },
    { id: "minecraft:cod", nutrition: 2 },
    { id: "minecraft:cooked_cod", nutrition: 5 },
    { id: "minecraft:salmon", nutrition: 2 },
    { id: "minecraft:cooked_salmon", nutrition: 6 },
    { id: "minecraft:tropical_fish", nutrition: 1 },
    { id: "minecraft:pufferfish", nutrition: 1 },
    { id: "minecraft:sweet_berries", nutrition: 2 },
    { id: "minecraft:melon_slice", nutrition: 2 },
    { id: "minecraft:chorus_fruit", nutrition: 4 },
    { id: "minecraft:cookie", nutrition: 2 },
    { id: "minecraft:pumpkin_pie", nutrition: 8 },
    { id: "minecraft:beetroot", nutrition: 1 },
    { id: "minecraft:beetroot_soup", nutrition: 6 },
    { id: "minecraft:mushroom_stew", nutrition: 6 },
    { id: "minecraft:rabbit_stew", nutrition: 10 },
    { id: "minecraft:rotten_flesh", nutrition: 4 },
    { id: "minecraft:spider_eye", nutrition: 2 },
    { id: "minecraft:dried_kelp", nutrition: 1 },
    { id: "minecraft:honey_bottle", nutrition: 6 },
    { id: "minecraft:suspicious_stew", nutrition: 6 },
    { id: "minecraft:glow_berries", nutrition: 2 }
  ];

export function deceitwebArmorEffects(player){
    for (const armorSlot of armorSlots) 
      {
        const equipment = player.getComponent('equippable');
        const armor = equipment.getEquipment(armorSlot);
        if(armor!=undefined)
        {
          var tags = armor.getTags()
          if(!tags.includes("edx:deceitweb_armor") || tags == undefined)//si pas armure
          {
            if(armorSlot == EquipmentSlot.Head && player.hasTag('deceitweb_head'))
            {
              player.removeTag("deceitweb_head");
              player.runCommand(`scoreboard players remove @s max_health 2`)
            }
            else if(armorSlot == EquipmentSlot.Chest && player.hasTag('deceitweb_chest'))
            {
              player.removeTag("deceitweb_chest");
              player.runCommand(`scoreboard players remove @s max_health 2`)
            }
            else if(armorSlot == EquipmentSlot.Legs && player.hasTag('deceitweb_legs'))
            {
              player.removeTag("deceitweb_legs");
              player.runCommand(`scoreboard players remove @s max_health 2`)
            }
            else if(armorSlot == EquipmentSlot.Feet && player.hasTag('deceitweb_feet'))
            {
              player.removeTag("deceitweb_feet");
              player.runCommand(`scoreboard players remove @s max_health 2`)
            }
          }
          else if(tags.includes("edx:deceitweb_armor")) //si armure
          {
            if(armorSlot == EquipmentSlot.Head && !player.hasTag('deceitweb_head'))
              {
                player.addTag("deceitweb_head");
                player.runCommand(`scoreboard players add @s max_health 2`)
              }
              else if(armorSlot == EquipmentSlot.Chest && !player.hasTag('deceitweb_chest'))
              {
                player.addTag("deceitweb_chest");
                player.runCommand(`scoreboard players add @s max_health 2`)
              }
              else if(armorSlot == EquipmentSlot.Legs && !player.hasTag('deceitweb_legs'))
              {
                player.addTag("deceitweb_legs");
                player.runCommand(`scoreboard players add @s max_health 2`)
              }
              else if(armorSlot == EquipmentSlot.Feet && !player.hasTag('deceitweb_feet'))
              {
                player.addTag("deceitweb_feet");
                player.runCommand(`scoreboard players add @s max_health 2`)
              }
          }
          if(!tags.includes("edx:deceitweb_armor_growing") || tags == undefined)
            {
              if(armorSlot == EquipmentSlot.Head && player.hasTag('deceitweb_head_growing'))
              {
                player.removeTag("deceitweb_head_growing");
                player.runCommand(`scoreboard players remove @s max_health 1`)
              }
              else if(armorSlot == EquipmentSlot.Chest && player.hasTag('deceitweb_chest_growing'))
              {
                player.removeTag("deceitweb_chest_growing");
                player.runCommand(`scoreboard players remove @s max_health 1`)
              }
              else if(armorSlot == EquipmentSlot.Legs && player.hasTag('deceitweb_legs_growing'))
              {
                player.removeTag("deceitweb_legs_growing");
                player.runCommand(`scoreboard players remove @s max_health 1`)
              }
              else if(armorSlot == EquipmentSlot.Feet && player.hasTag('deceitweb_feet_growing'))
              {
                player.removeTag("deceitweb_feet_growing");
                player.runCommand(`scoreboard players remove @s max_health 1`)
              }
            }
            else if(tags.includes("edx:deceitweb_armor_growing"))
            {
              if(armorSlot == EquipmentSlot.Head && !player.hasTag('deceitweb_head_growing'))
                {
                  player.addTag("deceitweb_head_growing");
                  player.runCommand(`scoreboard players add @s max_health 1`)
                }
                else if(armorSlot == EquipmentSlot.Chest && !player.hasTag('deceitweb_chest_growing'))
                {
                  player.addTag("deceitweb_chest_growing");
                  player.runCommand(`scoreboard players add @s max_health 1`)
                }
                else if(armorSlot == EquipmentSlot.Legs && !player.hasTag('deceitweb_legs_growing'))
                {
                  player.addTag("deceitweb_legs_growing");
                  player.runCommand(`scoreboard players add @s max_health 1`)
                }
                else if(armorSlot == EquipmentSlot.Feet && !player.hasTag('deceitweb_feet_growing'))
                {
                  player.addTag("deceitweb_feet_growing");
                  player.runCommand(`scoreboard players add @s max_health 1`)
                }
            }
        }
        else
        {
          if(armorSlot == EquipmentSlot.Head && player.hasTag('deceitweb_head'))
            {
              player.removeTag("deceitweb_head");
              player.runCommand(`scoreboard players remove @s max_health 2`)
            }
            else if(armorSlot == EquipmentSlot.Chest && player.hasTag('deceitweb_chest'))
            {
              player.removeTag("deceitweb_chest");
              player.runCommand(`scoreboard players remove @s max_health 2`)
            }
            else if(armorSlot == EquipmentSlot.Legs && player.hasTag('deceitweb_legs'))
            {
              player.removeTag("deceitweb_legs");
              player.runCommand(`scoreboard players remove @s max_health 2`)
            }
            else if(armorSlot == EquipmentSlot.Feet && player.hasTag('deceitweb_feet'))
            {
              player.removeTag("deceitweb_feet");
              player.runCommand(`scoreboard players remove @s max_health 2`)
            }
  
  
            if(armorSlot == EquipmentSlot.Head && player.hasTag('deceitweb_head_growing'))
              {
                player.removeTag("deceitweb_head_growing");
                player.runCommand(`scoreboard players remove @s max_health 1`)
              }
              else if(armorSlot == EquipmentSlot.Chest && player.hasTag('deceitweb_chest_growing'))
              {
                player.removeTag("deceitweb_chest_growing");
                player.runCommand(`scoreboard players remove @s max_health 1`)
              }
              else if(armorSlot == EquipmentSlot.Legs && player.hasTag('deceitweb_legs_growing'))
              {
                player.removeTag("deceitweb_legs_growing");
                player.runCommand(`scoreboard players remove @s max_health 1`)
              }
              else if(armorSlot == EquipmentSlot.Feet && player.hasTag('deceitweb_feet_growing'))
              {
                player.removeTag("deceitweb_feet_growing");
                player.runCommand(`scoreboard players remove @s max_health 1`)
              }
        }
        
      } 
  }
  
export function* deceitwebArmorEatAndGrow(){
    for (const player of world.getPlayers()){
      yield
      for (const armorSlot of armorSlots) 
        {
          yield
          const equipment = player.getComponent('equippable');
          const armor = equipment.getEquipment(armorSlot);
          if(armor!=undefined)
          {
            var tags = armor.getTags()
            if(tags.includes("edx:deceitweb_armor"))
            {
              
              if(Math.random()<0.2){
                var bool = remove_food(player,3)
                if (!bool)
                {
                  player.runCommand(`damage @s 3 none entity @s`)
                }
                else
                  player.dimension.playSound('item.deceitweb_armor.eat',player.location)
              }
            }
            if(tags.includes("edx:deceitweb_armor_growing"))
              {
                
                if(Math.random()<0.2){
                  var bool = remove_food(player,2)
                  if (!bool)
                  {
                    player.runCommand(`damage @s 3 none entity @s`)
                  }
                  else if(Math.random()<0.3)
                  {
                    player.dimension.playSound('item.deceitweb_armor.transform',player.location)
                    if(tags.includes("edx:deceitweb_helmet_2"))
                    {
                      equipment.setEquipment(armorSlot, new ItemStack("edx:deceitweb_helmet"));
                    }
                    else if(tags.includes("edx:deceitweb_helmet_1"))
                    {
                      equipment.setEquipment(armorSlot, new ItemStack("edx:deceitweb_helmet_stage2"));
                    }
                    
                    if(tags.includes("edx:deceitweb_boots_2"))
                    {
                      equipment.setEquipment(armorSlot, new ItemStack("edx:deceitweb_boots"));
                    }
                    else if(tags.includes("edx:deceitweb_boots_1"))
                    {
                      equipment.setEquipment(armorSlot, new ItemStack("edx:deceitweb_boots_stage2"));
                    }
                    
                    if(tags.includes("edx:deceitweb_chestplate_2"))
                    {
                      equipment.setEquipment(armorSlot, new ItemStack("edx:deceitweb_chestplate"));
                    }
                    else if(tags.includes("edx:deceitweb_chestplate_1"))
                    {
                      equipment.setEquipment(armorSlot, new ItemStack("edx:deceitweb_chestplate_stage2"));
                    }
                    
                    if(tags.includes("edx:deceitweb_leggings_2"))
                    {
                      equipment.setEquipment(armorSlot, new ItemStack("edx:deceitweb_leggings"));
                    }
                    else if(tags.includes("edx:deceitweb_leggings_1"))
                    {
                      equipment.setEquipment(armorSlot, new ItemStack("edx:deceitweb_leggings_stage2"));
                    }
                  }
                  else
                    player.dimension.playSound('item.deceitweb_armor.eat',player.location)
                }
              }
          }
          
        } 
    }
  }

export function remove_food(player,nutritionMax)
{
  var nutritionAmount =0
  let inv = player.getComponent("inventory").container
  for (var i=0;i<36;i++){
    var item=inv.getItem(i);
    if(item!=undefined)
    {
      var vanillaFood = minecraftFood.find(({ id }) => id === item.typeId )
      if(item?.getComponent("minecraft:food")!=undefined)//bouffe moddée
      {
        var itemNutrition = item.getComponent("minecraft:food").nutrition
        if(item.amount<=1)
          inv.setItem(i, undefined);
        else
          inv.setItem(i, new ItemStack(item.typeId, item.amount-1));
        if(itemNutrition+nutritionAmount>=nutritionMax)
          return true
        else
        {
          nutritionAmount+=itemNutrition
          
        }
        i=0
      }
      else if(vanillaFood!=undefined)//bouffe vanilla
      {
        var itemNutrition = vanillaFood.nutrition
        if(item.amount<=1)
          inv.setItem(i, undefined);
        else
          inv.setItem(i, new ItemStack(item.typeId, item.amount-1));
        if(itemNutrition+nutritionAmount>=nutritionMax)
          return true
        else
        {
          nutritionAmount+=itemNutrition
          
        }
        i=0
          
      }
      if(nutritionAmount >=nutritionMax)
      {
        return true
      }
    }
    
  } 
  return false
}