package mcjty.rftools.blocks.logic.wireless;

import mcjty.lib.tileentity.LogicTileEntity;
import mcjty.theoneprobe.api.IProbeHitData;
import mcjty.theoneprobe.api.IProbeInfo;
import mcjty.theoneprobe.api.ProbeMode;
import mcp.mobius.waila.api.IWailaConfigHandler;
import mcp.mobius.waila.api.IWailaDataAccessor;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Optional;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.List;

public abstract class RedstoneChannelTileEntity extends LogicTileEntity {

    protected int channel = -1;

    public int getChannel(boolean initialize) {
        if(initialize && channel == -1) {
            RedstoneChannels redstoneChannels = RedstoneChannels.getChannels(world);
            setChannel(redstoneChannels.newChannel());
            redstoneChannels.save();
        }
        return channel;
    }

    public void setChannel(int channel) {
        this.channel = channel;
        markDirtyClient();
    }

    @Override
    public void readRestorableFromNBT(NBTTagCompound tagCompound) {
        super.readRestorableFromNBT(tagCompound);
        channel = tagCompound.getInteger("channel");
    }

    @Override
    public void writeRestorableToNBT(NBTTagCompound tagCompound) {
        super.writeRestorableToNBT(tagCompound);
        tagCompound.setInteger("channel", channel);
    }

    @Override
    @Optional.Method(modid = "theoneprobe")
    public void addProbeInfo(ProbeMode mode, IProbeInfo probeInfo, EntityPlayer player, World world, IBlockState blockState, IProbeHitData data) {
        super.addProbeInfo(mode, probeInfo, player, world, blockState, data);
        probeInfo.text(TextFormatting.GREEN + "Channel: " + getChannel(false));
    }

    @SideOnly(Side.CLIENT)
    @Override
    @Optional.Method(modid = "waila")
    public void addWailaBody(ItemStack itemStack, List<String> currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) {
        super.addWailaBody(itemStack, currenttip, accessor, config);
        currenttip.add(TextFormatting.GREEN + "Channel: " + getChannel(false));
    }


}

@@ -1918,16 +1918,289 @@
  data);%0A
+        int channel = getChannel(false);%0A        if(channel == -1) %7B%0A            probeInfo.text(TextFormatting.YELLOW + %22No channel set! Right-click with another%22);%0A            probeInfo.text(TextFormatting.YELLOW + %22transmitter or receiver to pair%22);%0A        %7D else %7B%0A    
         
@@ -2251,35 +2251,35 @@
 el: %22 + 
-getC
+c
 hannel
-(false));
+);%0A        %7D
 %0A    %7D%0A%0A
@@ -2560,16 +2560,289 @@
 onfig);%0A
+        int channel = getChannel(false);%0A        if(channel == -1) %7B%0A            currenttip.add(TextFormatting.YELLOW + %22No channel set! Right-click with another%22);%0A            currenttip.add(TextFormatting.YELLOW + %22transmitter or receiver to pair%22);%0A        %7D else %7B%0A    
         
@@ -2897,27 +2897,27 @@
 %22 + 
-getC
+c
 hannel
-(false));
+);%0A        %7D
 %0A   

Fix #1851: clarify when a transmitter/receiver has no channel