package li.cil.oc.common.item

import java.util

import li.cil.oc.api.network._
import li.cil.oc.server.PacketSender
import li.cil.oc.util.Tooltip
import li.cil.oc.{Localization, Settings}
import net.minecraft.client.renderer.texture.IconRegister
import net.minecraft.entity.player.{EntityPlayer, EntityPlayerMP}
import net.minecraft.item.ItemStack
import net.minecraft.world.World
import net.minecraftforge.common.ForgeDirection

class Analyzer(val parent: Delegator) extends Delegate {
  val unlocalizedName = "Analyzer"

  override def tooltipLines(stack: ItemStack, player: EntityPlayer, tooltip: util.List[String], advanced: Boolean) {
    tooltip.addAll(Tooltip.get(unlocalizedName))
    super.tooltipLines(stack, player, tooltip, advanced)
  }

  override def onItemUse(stack: ItemStack, player: EntityPlayer, world: World, x: Int, y: Int, z: Int, side: Int, hitX: Float, hitY: Float, hitZ: Float) = {
    player match {
      case realPlayer: EntityPlayerMP =>
        world.getBlockTileEntity(x, y, z) match {
          case analyzable: Analyzable =>
            if (!world.isRemote) {
              analyzeNodes(analyzable.onAnalyze(realPlayer, side, hitX, hitY, hitZ), realPlayer)
            }
            true
          case host: SidedEnvironment =>
            if (!world.isRemote) {
              analyzeNodes(Array(host.sidedNode(ForgeDirection.getOrientation(side))), realPlayer)
            }
            true
          case host: Environment =>
            if (!world.isRemote) {
              analyzeNodes(Array(host.node), realPlayer)
            }
            true
          case _ => super.onItemUse(stack, realPlayer, world, x, y, z, side, hitX, hitY, hitZ)
        }
      case _ => false
    }
  }

  private def analyzeNodes(nodes: Array[Node], player: EntityPlayerMP) = if (nodes != null) for (node <- nodes if node != null) {
    node match {
      case connector: Connector =>
        if (connector.localBufferSize > 0) {
          player.sendChatToPlayer(Localization.Analyzer.StoredEnergy("%.2f/%.2f".format(connector.localBuffer, connector.localBufferSize)))
        }
        player.sendChatToPlayer(Localization.Analyzer.TotalEnergy("%.2f/%.2f".format(connector.globalBuffer, connector.globalBufferSize)))
      case _ =>
    }
    node match {
      case component: Component =>
        player.sendChatToPlayer(Localization.Analyzer.ComponentName(component.name))
      case _ =>
    }
    val address = node.address()
    if (address != null && !address.isEmpty) {
      player.sendChatToPlayer(Localization.Analyzer.Address(address))
      PacketSender.sendAnalyze(address, player)
    }
  }

  override def registerIcons(iconRegister: IconRegister) {
    super.registerIcons(iconRegister)

    icon = iconRegister.registerIcon(Settings.resourceDomain + ":analyzer")
  }
}
