package cn.net.cpzslibs.prot.handset;

import com.google.gson.Gson;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import cn.net.cpzslibs.prot.ProtBase;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.Port10047BreedPorList;
import cn.net.cpzslibs.prot.handset.bean.Port10047BreedResBean;
import cn.net.cpzslibs.prot.handset.bean.Port10047EventProList;
import cn.net.cpzslibs.prot.handset.bean.Port10047EventResBean;
import cn.net.cpzslibs.prot.handset.bean.Port10047MachProList;
import cn.net.cpzslibs.prot.handset.bean.Port10047MachResBean;
import cn.net.cpzslibs.prot.handset.bean.Port10047PlantBreedProList;
import cn.net.cpzslibs.prot.handset.bean.Port10047PlantBreedResBean;
import cn.net.cpzslibs.prot.handset.bean.Port10047PutstuffProList;
import cn.net.cpzslibs.prot.handset.bean.Port10047PutstuffResBean;
import cn.net.cpzslibs.prot.handset.bean.Port10047SendBean;
import cn.net.cpzslibs.prot.handset.bean.Port10090PlantMachResBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10090PlantMachProList;
import cn.net.cpzslibs.prot.utils.StrUtils;

public class Prot10090GetWuWeiProducts extends ProtBase {
	private final short iTaskCode10090 = 10090;

	private Gson gson;

	private Port10047BreedResBean breedResult;// 养殖计划所有json数据
	private List<Port10047BreedPorList> breeds = new ArrayList<>();// 养殖计划的集合

	private Port10047MachResBean machsResult;// 加工计划所有的json
	private List<Port10047MachProList> machs = new ArrayList<>();// 加工计划的集合

	private Port10047PutstuffResBean putstuffResult;// 投入品所有json
	private List<Port10047PutstuffProList> putstuffs = new ArrayList<>();// 投入品集合

	private Port10047EventResBean eventResult;// 事件（操作种类）所有json
	private List<Port10047EventProList> events = new ArrayList<>();// 事件（操作种类）集合

	private Port10047PlantBreedResBean plantBreedResult;// 种植生产计划所有的json
	private List<Port10047PlantBreedProList> plantsBreed = new ArrayList<>();

	private Port10090PlantMachResBean plantMachResult;// 种植加工计划的所有json
	private List<Prot10090PlantMachProList> plantsMach = new ArrayList<>();

	private boolean isEnd = false;// true：所有协议包已经请求完 false：服务器返回的数据还未接收完全
	private boolean isMachEnd = false;// true：所有协议包已经请求完 false：服务器返回的数据还未接收完全
	private boolean isPutStuffEnd = false;// true：所有协议包已经请求完
											// false：服务器返回的数据还未接收完全
	private boolean isEventEnd = false;// true：所有协议包已经请求完 false：服务器返回的数据还未接收完全

	private boolean isPlantBreedEnd = false;// true：所有协议包已经请求完
											// false：服务器返回的数据还未接收完全
	private boolean isPlantMachEnd = false;// true：所有协议包已经请求完
											// false：服务器返回的数据还未接收完全

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

	// 返回产品的集合
	public Port10047BreedResBean getBreedResult() {
		return this.breedResult;
	}

	public Port10047MachResBean getMachResult() {
		return this.machsResult;
	}

	public Port10047PutstuffResBean getPutStuffResult() {
		return this.putstuffResult;
	}

	public Port10047EventResBean getEventResult() {
		return this.eventResult;
	}
	
	public Port10047PlantBreedResBean getPlantBreedResult() {
		return plantBreedResult;
	}

	public Port10090PlantMachResBean getPlantMachResult() {
		return plantMachResult;
	}

	// 请求养殖计划
	public void dealBreedList(SocketCreate create, Port10047SendBean productBean)
			throws IOException {
		recProt(create.getSocket(), create.getDos(), create.getDis(),
				productBean);
	}

	private void recProt(Socket socket, DataOutputStream dos,
			DataInputStream dis, Port10047SendBean productBean)
			throws IOException {

		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {

				// 发送登录信息
				debugLog(iTaskCode10090, "开始请求产品列表");
				sendProt(dos, productBean);
			}
		}

		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				while (!isEnd) {
					// 服务器可能分多次包返回数据
					recJson(dis);
				}
			}
		}

	}

	/**
	 * 发送JSon数据到服务器
	 * 
	 * @param dos
	 * @param iUserType
	 * @param loginBean
	 * @throws IOException
	 */
	private void sendProt(DataOutputStream dos, Port10047SendBean productBean)
			throws IOException {

		String bodyContent = gson.toJson(productBean).toString();

		short bodySize = countBodySize(bodyContent);

		sendHeader(dos, iTaskCode10090, bodySize);// 84

		dos.write(bodyContent.getBytes("UTF-8"));

		sendCheckCoded(dos, bodyContent.getBytes("UTF-8"));

	}

	static int cTest = 0;

	/**
	 * 读取JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recJson(DataInputStream dis) throws IOException {
		debugLog(iTaskCode10090, (cTest++) + "获取接收的数据");
		recHeader(dis);

		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);

		debugLog(iTaskCode10090, jsonData);
		if (!StrUtils.isEmpty(jsonData)) {
			breedResult = gson.fromJson(jsonData, Port10047BreedResBean.class);

			if (breedResult.getPlans() != null) {
				breeds.addAll(breedResult.getPlans());
			}
			if (breedResult.getIndex() == 0) {// 如果是0则不再读取数据
				// 所有协议包已经请求完
				breedResult.setPlans(breeds);
				isEnd = true;
			}
			if (breedResult.getResult() == 1) {
				// 请求时遇到错误信息
				isEnd = true;
			}
		} else {
			isEnd = true;
		}

		recCheckCode(dis);
		/*
		 * for (int i = 0; i < breeds.size(); i++) {
		 * System.out.println(breeds.get(i).getName());
		 * System.out.println(breeds.get(i).getId());
		 * 
		 * }
		 */
	}

	/*
	 * 请求加工计划
	 */

	public void dealMachList(SocketCreate create, Port10047SendBean productBean)
			throws IOException {
		recProtMach(create.getSocket(), create.getDos(), create.getDis(),
				productBean);
	}

	private void recProtMach(Socket socket, DataOutputStream dos,
			DataInputStream dis, Port10047SendBean productBean)
			throws IOException {

		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {

				// 发送登录信息
				debugLog(iTaskCode10090, "开始请求产品列表");
				sendMachProt(dos, productBean);
			}
		}

		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				while (!isMachEnd) {
					// 服务器可能分多次包返回数据
					recJsonMach(dis);
				}
			}
		}

	}

	/**
	 * 发送JSon数据到服务器
	 * 
	 * @param dos
	 * @param iUserType
	 * @param loginBean
	 * @throws IOException
	 */
	private void sendMachProt(DataOutputStream dos,
			Port10047SendBean productBean) throws IOException {

		String bodyContent = gson.toJson(productBean).toString();

		short bodySize = countBodySize(bodyContent);

		sendHeader(dos, iTaskCode10090, bodySize);// 84

		dos.write(bodyContent.getBytes("UTF-8"));

		sendCheckCoded(dos, bodyContent.getBytes("UTF-8"));

	}

	/**
	 * 读取加工计划JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recJsonMach(DataInputStream dis) throws IOException {
		// debugLog(iTaskCode10090, (cTest++) + "获取接收的数据");
		recHeader(dis);

		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);

		debugLog(iTaskCode10090, jsonData);
		if (!StrUtils.isEmpty(jsonData)) {
			machsResult = gson.fromJson(jsonData, Port10047MachResBean.class);

			if (machsResult.getPlans() != null) {
				machs.addAll(machsResult.getPlans());
			}
			if (machsResult.getIndex() == 0) {// 如果是0则不再读取数据
				// 所有协议包已经请求完
				machsResult.setPlans(machs);
				isMachEnd = true;
			}
			if (machsResult.getResult() == 1) {
				// 请求时遇到错误信息
				isMachEnd = true;
			}
		} else {
			isMachEnd = true;
		}

		recCheckCode(dis);

		/*
		 * for (int i = 0; i < machs.size(); i++) {
		 * System.out.println(machs.get(i).getName());
		 * System.out.println(machs.get(i).getId()); }
		 */
	}

	/*
	 * 请求投入品
	 */

	public void dealPutStuffList(SocketCreate create,
                                 Port10047SendBean productBean) throws IOException {
		recProtPutStuff(create.getSocket(), create.getDos(), create.getDis(),
				productBean);
	}

	private void recProtPutStuff(Socket socket, DataOutputStream dos,
			DataInputStream dis, Port10047SendBean productBean)
			throws IOException {

		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {

				// 发送登录信息
				debugLog(iTaskCode10090, "开始请求产品列表");
				sendProt(dos, productBean);
			}
		}

		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				while (!isPutStuffEnd) {
					// 服务器可能分多次包返回数据
					recJsonPutStuff(dis);
				}
			}
		}

	}

	/**
	 * 读取投入品JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recJsonPutStuff(DataInputStream dis) throws IOException {
		// debugLog(iTaskCode10090, (cTest++) + "获取接收的数据");
		recHeader(dis);

		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);

		debugLog(iTaskCode10090, jsonData);
		if (!StrUtils.isEmpty(jsonData)) {
			putstuffResult = gson.fromJson(jsonData,
					Port10047PutstuffResBean.class);

			if (putstuffResult.getPut_stuffs() != null) {
				putstuffs.addAll(putstuffResult.getPut_stuffs());
			}
			if (putstuffResult.getIndex() == 0) {// 如果是0则不再读取数据
				// 所有协议包已经请求完
				putstuffResult.setPut_stuffs(putstuffs);
				isPutStuffEnd = true;
			}
			if (putstuffResult.getResult() == 1) {
				// 请求时遇到错误信息
				isPutStuffEnd = true;
			}
		} else {
			isPutStuffEnd = true;
		}

		recCheckCode(dis);
		/*
		 * for (int i = 0; i < putstuffs.size(); i++) {
		 * System.out.println(putstuffs.get(i).getName());
		 * System.out.println(putstuffs.get(i).getType());
		 * 
		 * }
		 */
	}

	/*
	 * 请求投入品
	 */

	public void dealEventList(SocketCreate create, Port10047SendBean productBean)
			throws IOException {
		recProtEvent(create.getSocket(), create.getDos(), create.getDis(),
				productBean);
	}

	private void recProtEvent(Socket socket, DataOutputStream dos,
			DataInputStream dis, Port10047SendBean productBean)
			throws IOException {

		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {

				// 发送登录信息
				debugLog(iTaskCode10090, "开始请求产品列表");
				sendProt(dos, productBean);
			}
		}

		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				while (!isEventEnd) {
					// 服务器可能分多次包返回数据
					recJsonEvent(dis);
				}
			}
		}

	}

	/**
	 * 读取投入品JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recJsonEvent(DataInputStream dis) throws IOException {
		// debugLog(iTaskCode10090, (cTest++) + "获取接收的数据");
		recHeader(dis);

		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);

		debugLog(iTaskCode10090, jsonData);
		if (!StrUtils.isEmpty(jsonData)) {
			eventResult = gson.fromJson(jsonData, Port10047EventResBean.class);

			if (eventResult.getEvents() != null) {
				events.addAll(eventResult.getEvents());
			}
			if (eventResult.getIndex() == 0) {// 如果是0则不再读取数据
				// 所有协议包已经请求完
				eventResult.setEvents(events);
				isEventEnd = true;
			}
			if (eventResult.getResult() == 1) {
				// 请求时遇到错误信息
				isEventEnd = true;
			}
		} else {
			isEventEnd = true;
		}

		recCheckCode(dis);
		for (int i = 0; i < events.size(); i++) {
			System.out.println("i=getName==" + events.get(i).getName());
			System.out.println("i=getType==" + events.get(i).getType());

		}
	}

	/*
	 * 处理种植计划
	 */
	public void dealPlantBreed(SocketCreate create,
                               Port10047SendBean productBean) throws IOException {
		recProtPlantBreed(create.getSocket(), create.getDos(), create.getDis(),
				productBean);
	}
	

	private void recProtPlantBreed(Socket socket, DataOutputStream dos,
			DataInputStream dis, Port10047SendBean productBean)
			throws IOException {

		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {

				// 发送登录信息
				debugLog(iTaskCode10090, "开始请求产品列表");
				sendProt(dos, productBean);
			}
		}

		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				while (!isPlantBreedEnd) {
					// 服务器可能分多次包返回数据
					recJsonPlantBreed(dis);
				}
			}
		}

	}

	/**
	 * 读取种植生产计划JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recJsonPlantBreed(DataInputStream dis) throws IOException {
		//debugLog(iTaskCode10090, (cTest++) + "获取接收的数据");
		recHeader(dis);

		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);

		debugLog(iTaskCode10090, jsonData);
		if (!StrUtils.isEmpty(jsonData)) {
			plantBreedResult = gson.fromJson(jsonData,
					Port10047PlantBreedResBean.class);

			if (plantBreedResult.getPlans() != null) {
				plantsBreed.addAll(plantBreedResult.getPlans());
			}
			if (plantBreedResult.getIndex() == 0) {// 如果是0则不再读取数据
				// 所有协议包已经请求完
				plantBreedResult.setPlans(plantsBreed);
				isPlantBreedEnd = true;
			}
			if (plantBreedResult.getResult() == 1) {
				// 请求时遇到错误信息
				isPlantBreedEnd = true;
			}
		} else {
			isPlantBreedEnd = true;
		}

		recCheckCode(dis);
		/*for (int i = 0; i < plantsBreed.size(); i++) {
			System.out.println("i=getName==" + plantsBreed.get(i).getName());
			System.out.println("i=getName==" + plantsBreed.get(i).getId());

		}*/

	}
	/*
	 * 处理种植加工计划
	 */
	public void dealPlantMach(SocketCreate create,
                              Port10047SendBean productBean) throws IOException {
		recProtPlantMach(create.getSocket(), create.getDos(), create.getDis(),
				productBean);
	} 
	
	private void recProtPlantMach(Socket socket, DataOutputStream dos,
			DataInputStream dis, Port10047SendBean productBean)
			throws IOException {

		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {

				// 发送登录信息
				debugLog(iTaskCode10090, "开始请求产品列表");
				sendProt(dos, productBean);
			}
		}

		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				while (!isPlantMachEnd) {
					// 服务器可能分多次包返回数据
					recJsonPlantMach(dis);
				}
			}
		}

	}
	/**
	 * 读取种植加工计划JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recJsonPlantMach(DataInputStream dis) throws IOException {
	//	debugLog(iTaskCode10090, (cTest++) + "获取接收的数据");
		recHeader(dis);

		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);

		debugLog(iTaskCode10090, jsonData);
		if (!StrUtils.isEmpty(jsonData)) {
			plantMachResult = gson.fromJson(jsonData,
					Port10090PlantMachResBean.class);

			if (plantMachResult.getPlans() != null) {
				plantsMach.addAll(plantMachResult.getPlans());
			}
			if (plantMachResult.getIndex() == 0) {// 如果是0则不再读取数据
				// 所有协议包已经请求完
				plantMachResult.setPlans(plantsMach);
				isPlantMachEnd = true;
			}
			if (plantMachResult.getResult() == 1) {
				// 请求时遇到错误信息
				isPlantMachEnd = true;
			}
		} else {
			isPlantMachEnd = true;
		}

		recCheckCode(dis);
	/*	for (int i = 0; i < plantsMach.size(); i++) {
			System.out.println("i=getName==" + plantsMach.get(i).getName());
			System.out.println("i=getName==" + plantsMach.get(i).getId());

		}*/

	}

}
