package cn.net.cpzslibs.prot.handset;

import com.google.gson.Gson;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;

import cn.net.cpzslibs.prot.ProtBase;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.Prot10049SendBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10049UpPictrueBean;
import cn.net.cpzslibs.prot.utils.StrUtils;

public class Prot10049UpPicture extends ProtBase {

	private final short iTaskCode10049 = 10049;

	private Gson gson;

	private Prot10049UpPictrueBean resBean;

	private Prot10049SendBean sendBean;
	

	private File file;

	int index = 0;// 多少个包
	int tag = 0;
	

	public Prot10049UpPicture() {
		gson = new Gson();
	}

	public Prot10049UpPictrueBean getResBean() {
		return resBean;
	}

	public void setResBean(Prot10049UpPictrueBean resBean) {
		this.resBean = resBean;
	}

	public void dealPicture(SocketCreate create, String strPath)
			throws IOException {
		recProt(create.getSocket(), create.getDos(), create.getDis(), strPath);

	}

	private void recProt(Socket socket, DataOutputStream dos,
			DataInputStream dis, String strPath) throws IOException {

		file = new File(strPath);
		long fileSize = file.length(); // 计算长度

		int b = Integer.parseInt(String.valueOf(fileSize % 32));
		if (b == 0) {
			index = Integer.parseInt(String.valueOf(fileSize / 32));
		} else {
			index = Integer.parseInt(String.valueOf(fileSize / 32)) + 1;
		}

		String name = strPath.substring(strPath.lastIndexOf("\\"));// 名字
		
		FileInputStream fis = new FileInputStream(file);
		
		int length = 0;
		byte[] len = new byte[32];
		
		System.out.println(file+"fileSize"+fileSize+"index"+index);

		/*
		 * 分包读取写入
		 */
		while ((length = fis.read(len, 0, len.length)) > 0) { // 从此输入流中将最多//
			tag = tag + 1;
			System.out.println();
		/*	if (tag == index) {
				sendBean = new Prot10049SendBean(name, Integer.parseInt(String
						.valueOf(fileSize)), 0);
			} else {
				sendBean = new Prot10049SendBean(name, Integer.parseInt(String
						.valueOf(fileSize)), tag);
			}*/
			// len.length//
			// 个字节的数据读入一个//
			// byte 数组中

			if (socket.isConnected()) {
				if (!socket.isOutputShutdown()) {
					// 发送登录信息
					debugLog(iTaskCode10049, "开始请求产品列表");

					sendProt(dos, sendBean, length, len);

				}
			}
			if (socket.isConnected()) {
				if (!socket.isInputShutdown()) {
					recJson(dis);
				}
			}
		}
	}

	/**
	 * 发送JSon数据到服务器
	 */
	private void sendProt(DataOutputStream dos, Prot10049SendBean labelBean,
			int length, byte[] len) throws IOException {
		
		String bodyContent = gson.toJson(labelBean).toString();
		System.out.println(bodyContent);
		short bodySize = (short) (countBodySize(bodyContent) + 32);

		sendHeader(dos, iTaskCode10049, bodySize);// 84
		dos.write(bodyContent.getBytes("UTF-8"));

		dos.write(len, 0, length);
		sendCheckCoded(dos, bodyContent.getBytes("UTF-8"));
	}

	/**
	 * 读取JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recJson(DataInputStream dis) throws IOException {
		debugLog(iTaskCode10049, "获取接收的数据");
		recHeader(dis);
		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);
		debugLog(iTaskCode10049, jsonData);
		if (!StrUtils.isEmpty(jsonData)) {
			resBean = gson.fromJson(jsonData, Prot10049UpPictrueBean.class);
		}
		recCheckCode(dis);
	}

}
