/*******************************************************************************
 * OulipoMachine licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the License.  
 *
 * You may obtain a copy of the License at
 *   
 *       http://www.apache.org/licenses/LICENSE-2.0
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. See the NOTICE file distributed with this work for 
 * additional information regarding copyright ownership. 
 *******************************************************************************/
package org.oulipo.streams.opcodes;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public final class InsertTextOp extends Op<InsertTextOp.Data> {

	public static class Data {

		public final String text;

		public final long to;

		public Data(long to, String text) {
			this.to = to;
			this.text = text;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Data other = (Data) obj;
			if (text == null) {
				if (other.text != null)
					return false;
			} else if (!text.equals(other.text))
				return false;
			if (to != other.to)
				return false;
			return true;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((text == null) ? 0 : text.hashCode());
			result = prime * result + (int) (to ^ (to >>> 32));
			return result;
		}
	}

	public static InsertTextOp read(byte[] message) throws IOException {
		return read(new DataInputStream(new ByteArrayInputStream(message)));
	}

	public static InsertTextOp read(DataInputStream dis) throws IOException {
		// if (Op.INSERT_TEXT != dis.readByte()) {
		// throw new IOException("Incorrect op type");
		// }
		long to = dis.readLong();
		// int width = (int) dis.readLong();
		// byte[] text = new byte[width];
		return new InsertTextOp(new Data(to, dis.readUTF()));
	}

	public InsertTextOp(Data data) {
		super(Op.INSERT_TEXT, data);
	}

	public InsertTextOp(long to, String text) {
		this(new Data(to, text));
	}

	@Override
	public byte[] toBytes() throws IOException {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try (DataOutputStream dos = new DataOutputStream(os)) {
			dos.writeByte(Op.INSERT_TEXT);
			dos.writeLong(getData().to);
			dos.writeUTF(getData().text);
			// dos.writeLong(getData().text.length());
			// dos.write(getData().text.getBytes());
		}
		os.flush();
		return os.toByteArray();

	}
}

@@ -1926,16 +1926,115 @@
 ult;%0A%09%09%7D
+%0A%0A%09%09@Override%0A%09%09public String toString() %7B%0A%09%09%09return %22Data %5Btext=%22 + text + %22, to=%22 + to + %22%5D%22;%0A%09%09%7D
 %0A%09%7D%0A%0A%09pu
@@ -2256,208 +2256,32 @@
 %7B%0A%09%09
-// if (Op.INSERT_TEXT != dis.readByte()) %7B%0A%09%09// throw new IOException(%22Incorrect op type%22);%0A%09%09// %7D%0A%09%09long to = dis.readLong();%0A%09%09// int width = (int) dis.readLong();%0A%09%09// byte%5B%5D text = new byte%5Bwidth%5D
+long to = dis.readLong()
 ;%0A%09%09
@@ -2760,98 +2760,8 @@
 t);%0A
-%09%09%09// dos.writeLong(getData().text.length());%0A%09%09%09// dos.write(getData().text.getBytes());%0A
 %09%09%7D%0A

Write string value out as UTF on insert op