package net.minecraft.src;

import java.util.Random;

public class PClo_TileEntityRadio extends TileEntity {

	public String channel = mod_PClogic.default_radio_channel;
	public int type = 0; // 0=tx, 1=rx
	public boolean active = false;
	

	@Override
	public void writeToNBT(NBTTagCompound nbttagcompound) {
		super.writeToNBT(nbttagcompound);
		nbttagcompound.setString("channel", channel);
		nbttagcompound.setInteger("type", type);
		nbttagcompound.setBoolean("active", active);
	}

	@Override
	public void readFromNBT(NBTTagCompound nbttagcompound) {
		super.readFromNBT(nbttagcompound);
		channel = nbttagcompound.getString("channel");
		type = nbttagcompound.getInteger("type");
		active = nbttagcompound.getBoolean("active");
		
		if(type == 1){
			PClo_RadioManager.registerReceiver(new PC_CoordI(xCoord, yCoord, zCoord), channel);
		}else{
			PClo_RadioManager.setTransmitterState(new PC_CoordI(xCoord, yCoord, zCoord), channel, active);
		}
	}



	@Override
	public void updateEntity() {
		
		
		
	}

	/**
	 * Notify block change.
	 */
	public void updateBlock() {
		worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, getBlockType().blockID);
		worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord - 1, zCoord, getBlockType().blockID);
		worldObj.markBlocksDirty(xCoord, yCoord, zCoord, xCoord, yCoord, zCoord);
	}

	/**
	 * forge method - receives update ticks
	 * 
	 * @return true
	 */
	public boolean canUpdate() {
		return true;
	}

	/**
	 * Set device type
	 * @param typeindex 0=gold TX, 1=iron RX
	 */
	public void setType(int typeindex) {
		type = typeindex;
	}

	/**
	 * @return is this device transmitter
	 */
	public boolean isTransmitter() {
		return type == 0;
	}
	
	/**
	 * @return is this device receiver
	 */
	public boolean isReceiver() {
		return type == 1;
	}

	/**
	 * @return is the radio device active
	 */
	public boolean isActive() {
		return active;
	}
	
	/**
	 * Set "active" flag and send update to radio manager
	 * @param act is active
	 */
	public void setStateWithNotify(boolean act) {
		if(isTransmitter() && active != act){
			PClo_RadioManager.setTransmitterState(new PC_CoordI(xCoord, yCoord, zCoord), channel, act);
		}
		active = act;
	}

	/**
	 * @return radio channel assigned to this entity
	 */
	public String getChannel() {
		return channel;
	}
}
