// This shouldn't be hard coded; just unsure where to define it.
var ikaFightRoot = "tools/immortalnights/ikafight/";

function randomValue() {
  for (var c = 200; c < 400; c++) {
    ikaFightAPI_updateAttackerUnit(c, Math.floor(Math.random()*200), Math.floor(Math.random()*4), Math.floor(Math.random()*4));
  }  
  for (var c = 200; c < 400; c++) {
    ikaFightAPI_updateDefenderUnit(c, Math.floor(Math.random()*200), Math.floor(Math.random()*4), Math.floor(Math.random()*4));
  }
  
  //ikaFightAPI_updateTownDetails(Math.floor(Math.random()*12), Math.floor(Math.random()*25), Math.floor(Math.random()*25), Math.floor(Math.random()*25))
}

function ikaFight_setUpgradeDirectory(dir) {
  ikaFightRoot = dir;
}

function ikaFight_updateSelected(index, value, type) {
  var upgradeImage = document.getElementById(index + "_image");
  if (upgradeImage) {
    upgradeImage.src = ikaFight_getUpgradeImage(type, value);
  }
  
  var upgradeText = document.getElementById(index);
  if (upgradeText) {
    upgradeText.value = value;
  }
}

function ikaFightAPI_updateAttackerUnit(id, count, attUpg, defUpg) {
  var unitValue = document.getElementById("attackingQnty["+id+"]");
  if (unitValue) {
    unitValue.value = count;
    ikaFight_updateSelected("aau["+id+"]", attUpg, "attack");
    ikaFight_updateSelected("adu["+id+"]", defUpg, "defence");
  }
}

function ikaFightAPI_updateDefenderUnit(id, count, attUpg, defUpg) {
  var unitValue = document.getElementById("defendingQnty["+id+"]");
  if (unitValue) {
    unitValue.value = count;
    ikaFight_updateSelected("dau["+id+"]", attUpg, "attack");
    ikaFight_updateSelected("ddu["+id+"]", defUpg, "defence");
  }
}

function ikaFightAPI_updateTownDetails(spies, breaches, town, wall) {
  document.getElementById("spies").value = spies;
  //document.getElementById("breaches").value = breaches;
  document.getElementById("townLevel").value = town;
  document.getElementById("wallLevel").value = wall;
}

function ikaFight_getUpgradeImage(type, index) {
  if (type == "attack") {
    switch(index)
    {
      case 0:
        return ikaFightRoot + "gfx/noattack.gif";
        break;    
      case 1:
        return ikaFightRoot + "gfx/bronseattack.gif";
        break;   
      case 2:
        return ikaFightRoot + "gfx/silverattack.gif";
        break;   
      case 3:
        return ikaFightRoot + "gfx/goldattack.gif";
        break;
      default:
        return ikaFightRoot + "gfx/noattack.gif";
    }
  }
  else if (type == "defence") {
    switch(index)
    {
      case 0:
        return ikaFightRoot + "gfx/nodefence.gif";
        break;    
      case 1:
        return ikaFightRoot + "gfx/bronsedefence.gif";
        break;   
      case 2:
        return ikaFightRoot + "gfx/silverdefence.gif";
        break;   
      case 3:
        return ikaFightRoot + "gfx/golddefence.gif";
        break;
      default:
        return ikaFightRoot + "gfx/nodefence.gif";
    }
  }
}
