package com.fieldexpert.fbapi4j.http;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.fieldexpert.fbapi4j.FogBugz;
import com.fieldexpert.fbapi4j.common.Attachment;

class MultiPartPostRequest extends HttpRequest {

	private static final String MULTIPART_FORM_DATA = "multipart/form-data; boundary=";

	/**
	 * See: http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
	 */
	private static String createBoundary() {
		return "-----------------------------" + Long.toString(System.currentTimeMillis());
	}

	private String boundary;
	private List<Attachment> attachments;

	public MultiPartPostRequest(URL url, Map<String, String> parameters, List<Attachment> attachments) {
		super(Http.POST, url, parameters);
		this.attachments = attachments;
		this.boundary = createBoundary();
	}

	@Override
	String getContentType() {
		return MULTIPART_FORM_DATA + boundary;
	}

	@Override
	void write(OutputStream out) throws IOException {
		MultiPartOutputStream mout = new MultiPartOutputStream(out, boundary);

		for (Entry<String, String> param : parameters.entrySet()) {
			mout.write(param.getKey(), param.getValue());
		}

		for (Attachment attachment : attachments) {
			mout.write(FogBugz.FILE, attachment.getFilename(), attachment.getType(), attachment.getContent());
		}

		mout.writeEnd();
	}
}

@@ -494,23 +494,16 @@
 private 
-static 
 String c
@@ -1224,16 +1224,33 @@
 );%0A%09%09%7D%0A%0A
+%09%09int files = 0;%0A
 %09%09for (A
@@ -1313,16 +1313,28 @@
 ugz.FILE
+ + (++files)
 , attach
@@ -1396,28 +1396,72 @@
 tContent());
+%09
 %0A%09%09%7D
+%0A%09%09mout.write(FogBugz.N_FILE_COUNT, files);
 %0A%0A%09%09mout.wri

Fix the file upload capabilities