package common.packet;

import java.io.IOException;

import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;

public class SPacketLoading implements Packet<IClientPlayer> {
	private String message;
	private String task;
	private int total = -1;
	private int progress = -2;

	public SPacketLoading() {
	}

	public SPacketLoading(String message) {
		this.message = message;
	}

	public SPacketLoading(String task, int total) {
		this.task = task;
		this.total = total;
	}

	public SPacketLoading(int progress) {
		this.progress = progress;
	}

	public void readPacketData(PacketBuffer buf) throws IOException {
		this.message = buf.readStringFromBuffer(128);
		this.message = this.message != null && this.message.isEmpty() ? null : this.message;
		if(this.message == null) {
			this.task = buf.readStringFromBuffer(128);
			this.task = this.task != null && this.task.isEmpty() ? null : this.task;
			this.total = buf.readInt();
			this.progress = buf.readInt();
		}
	}

	public void writePacketData(PacketBuffer buf) throws IOException {
		buf.writeString(this.message == null ? "" : this.message);
		if(this.message == null) {
			buf.writeString(this.task == null ? "" : this.task);
			buf.writeInt(this.total);
			buf.writeInt(this.progress);
		}
	}

	public void processPacket(IClientPlayer handler) {
		handler.handleLoading(this);
	}

	public String getMessage() {
		return this.message;
	}

	public String getTask() {
		return this.task;
	}

	public int getTotal() {
		return this.total;
	}

	public int getProgress() {
		return this.progress;
	}
}
