import {world,GameMode} from "@minecraft/server"
import {artifactsList} from "./artifacts.js"

/*
holy shit i understand this monstrosity
nvm
*/
export function findMarried(player){
  let inv = player.getComponent("inventory").container
  for(let j = 9; j < 27; j+=9)
    {
        var item = inv.getItem(j)
        if(item!=undefined)
        if(item.typeId == 'edx:soulbound_ring' && item?.getDynamicProperty('id')!=undefined)
        {
          const married = world.getEntity(item?.getDynamicProperty('id')) 
          return married
        }
    }
  return undefined;
}
export function soulboundRing(player)
{

  var marriedIdList =[]
  var marriedPlayer =0
    let inv = player.getComponent("inventory").container
    if(!player.hasTag('soulbound_ring')) return;
    else
    {
        for(let j = 9; j < 27; j+=9)
        {
            var item = inv.getItem(j)
            if(item!=undefined)
            if(item.typeId == 'edx:soulbound_ring' && item?.getDynamicProperty('id')!=undefined)
            {
              marriedPlayer = world.getEntity(item?.getDynamicProperty('id')) 
              if(marriedPlayer!=undefined)
                if(marriedIdList.length == 0 && marriedPlayer.hasTag('soulbound_ring'))
                {
                  marriedIdList.push(item?.getDynamicProperty('id'))
                  if(marriedPlayer){
                  }
                  soulboundEffects(player,marriedPlayer)
                  soulboundArtifact(player,marriedPlayer)
                }
                  
                else if(!player.matches({ gameMode: GameMode.Creative }) && marriedPlayer.id != marriedIdList[0] && marriedIdList.length != 0)
                {
                  soulboundCheat(player,marriedIdList,marriedPlayer,j)
                }
            }
        }
    }
    
}
export function soulboundArtifact(player,marriedPlayer)
{
  if(marriedPlayer!=undefined)
  for (const tag of marriedPlayer.getTags()){
    if(artifactsList.includes(tag) && tag!='soulbound_ring')
    {
      player.addTag(tag)
    }
  }
}


export function soulboundCheat(player,marriedList,otherplayer,slotIdx)
{
  let inv = player.getComponent("inventory").container
  var item = inv.getItem(j)
  world.gameRules.showDeathMessages = false
  player.dimension.spawnParticle('edx:souls_spotter',player.location);
  inv.setItem(slotIdx, undefined)
  player.kill()
  player.dimension.spawnItem(item,player.location)
  world.sendMessage(`${player.name} cheated on ${marriedList[0]} with ${otherplayer}.`);
  world.gameRules.showDeathMessages = true
}

export function killMarriedplayer(marriedPlayer,deadplayer)
{
  if(marriedPlayer.getComponent("health").currentValue ==0) return;
  world.gameRules.showDeathMessages = false
  deadplayer.dimension.spawnParticle('edx:souls_spotter',marriedPlayer.location);
  marriedPlayer.kill()
  randomDeathMessage(marriedPlayer,deadplayer)
  world.gameRules.showDeathMessages = true
}
export function randomDeathMessage(marriedPlayer,deadplayer){
  const random = Math.random()
      var and = {rawtext :[{ 
    translate: "message.soulbound_ring.and",
  }]};
        var followed = {rawtext :[{ 
    translate: "message.soulbound_ring.followed", 
  }]};
          var message1= {rawtext :[
      {text: marriedPlayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.and"},
      {text:" "},
      {text: deadplayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.1"}
]};
            var message2 = {rawtext :[      
      {text: marriedPlayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.2"},
      {text:" "},
      {text: deadplayer.name}
    ]};
              var message3 = {rawtext :[      
      {text: marriedPlayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.3"},
      {text:" "},
      {text: deadplayer.name}
    ]};
              var message4 = {rawtext :[      
      {text: marriedPlayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.4"},
      {text:" "},
      {text: deadplayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.5"},
    ]};
              var message5 = {rawtext :[      
      {text: marriedPlayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.followed"},
      {text:" "},
      {text: deadplayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.6"},
    ]};
              var message6 = {rawtext :[      
      {text: marriedPlayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.followed"},
      {text:" "},
      {text: deadplayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.7"},
    ]};
              var message7 = {rawtext :[      
      {text: marriedPlayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.and"},
      {text:" "},
      {text: deadplayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.8"}
      ,]};
              var message8 = {rawtext :[
      {translate: "message.soulbound_ring.9"},
      {text:" "}, 
      {text: marriedPlayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.and"},
      {text:" "},
      {text: deadplayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.10"}]};
              var message9 = {rawtext :[
      {translate: "message.soulbound_ring.11"},
      {text:" "}, 
      {text: marriedPlayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.and"},
      {text:" "},
      {text: deadplayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.12"}]};
              var message10 = {rawtext :[
      {text: marriedPlayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.and"},
      {text:" "},
      {text: deadplayer.name},
      {text:" "},
      {translate: "message.soulbound_ring.13"}]};
  if(random<0.1) 
    world.sendMessage(message1); 
  else if(random>=0.1 && random<0.2)
    world.sendMessage(message2);
  else if(random>=0.2 && random<0.3)
    world.sendMessage(message3);
  else if(random>=0.3 && random<0.4)
    world.sendMessage(message4);
  else if(random>=0.4 && random<0.5)
    world.sendMessage(message5); 
  else if(random>=0.5 && random<0.6)
    world.sendMessage(message6);
  else if(random>=0.6 && random<0.7)
    world.sendMessage(message7);
  else if(random>=0.7 && random<0.8)
    world.sendMessage(message8);
  else if(random>=0.8 && random<0.9)
    world.sendMessage(message9);
  else if(random>=0.9)
    world.sendMessage(message10);
}
export function soulboundEffects(player,marriedPlayer)
{
  if(marriedPlayer!=undefined)
  for (const effect of marriedPlayer?.getEffects())
  {
    if(effect.duration>300)
      player.addEffect(effect.typeId, 300,{ amplifier: effect.amplifier });
  }
}