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.bean.req.Prot10016ProductBean;
import cn.net.cpzslibs.prot.handset.bean.Prot10016ProList;
import cn.net.cpzslibs.prot.handset.bean.Prot10016ResBean;
import cn.net.cpzslibs.prot.utils.StrUtils;


/*
 * 手持机下载产品列表
 */
public class Prot10016ProlistHandset extends ProtBase {
	
	/**
	 * 是否成功
	 */
	
	private final short iTaskCode10016 = 10016;
	private Gson gson;
	private Prot10016ResBean productResult;
	private List<Prot10016ProList> products = new ArrayList<>() ;
	private boolean isEnd = false;
	
	public Prot10016ProlistHandset() {
		gson = new Gson();
	}
	public Prot10016ResBean getProductResult() {
		return productResult;
	}

	public void setProductResult(Prot10016ResBean productResult) {
		this.productResult = productResult;
	}

	public List<Prot10016ProList> getProducts() {
		return products;
	}

	public void setProducts(List<Prot10016ProList> products) {
		this.products = products;
	}
	
	public void dealProductList(SocketCreate create, Prot10016ProductBean productBean) throws IOException {
		recProt(create.getSocket(), create.getDos(), create.getDis(), productBean);
	}
	
	private void recProt(Socket socket, DataOutputStream dos, DataInputStream dis, Prot10016ProductBean productBean)
			throws IOException {
		if (socket.isConnected()) {
			if (!socket.isOutputShutdown()) {
				// 发送登录信息
				debugLog(iTaskCode10016, "开始请求产品列表");
				sendProt(dos, productBean);
			}
		}
		if (socket.isConnected()) {
			if (!socket.isInputShutdown()) {
				while (!isEnd) {
					//服务器可能分多次包返回数据
					recJsonData(dis);
				}
			}
		}
	}
	
	/**
	 * 发送JSon数据到服务器
	 * 
	 * @param dos
	 * @param iUserType
	 * @param loginBean
	 * @throws IOException
	 */
	private void sendProt(DataOutputStream dos, Prot10016ProductBean productBean) throws IOException {
		String bodyContent = gson.toJson(productBean).toString();
		short bodySize = countBodySize(bodyContent);
		sendHeader(dos, iTaskCode10016, bodySize);// 84
		dos.write(bodyContent.getBytes("UTF-8"));
		sendCheckCoded(dos, bodyContent.getBytes("UTF-8"));
	}
	
	/**
	 * 读取JSon数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recJsonData(DataInputStream dis) throws IOException {
		recHeader(dis);
		int jsonLength = getiRecBodySize() - 2;
		String jsonData = getUTF8(dis, jsonLength);
		debugLog(iTaskCode10016, jsonData);
		if (!StrUtils.isEmpty(jsonData)) {
			productResult = gson.fromJson(jsonData, Prot10016ResBean.class);
			
			if (productResult.getProducts() != null) {
				products.addAll(productResult.getProducts());
			}
			if (productResult.getCurrPackNo() == 0) {
				//所有协议包已经请求完
				productResult.setProducts(products);
				isEnd = true;
			}
			if (productResult.getResult() == 1) {
				//请求时遇到错误信息
				isEnd = true;
			}
		} else {
			isEnd = true;
		}

		recCheckCode(dis);
		
	}
	
}
