package cn.net.caoyao.cpzshandset.service.thread.impl.ythImage;

import java.io.IOException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import android.os.Handler;
import android.os.Message;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.service.bll.ProductBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20001ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20001ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20006Product;

public class YthImageUpdateProductListThread implements Runnable {
	private long deviceId;
	private ProductBll bll;
	private Handler mHandler;

	public YthImageUpdateProductListThread(Handler mHandler,long deviceId,ProductBll bll) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		this.bll = bll;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			LogUtil.d(TConfig.Echo, "登录");
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				requestProductList(create);
			} else {
				Message msg = new Message();
				msg.what = RequestConstant.REQUEST_PROT01_LOGIN_FAIL;
				msg.arg1 = create.getiErrCode();
				msg.obj = create.getErrMsg();
				mHandler.sendMessage(msg);
			}
			
		} catch (BindException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (ConnectException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (IOException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		}finally {
			new Prot02Logout().dealLogout(create);
		}
	}

	private void requestProductList(SocketCreate create) throws IOException{
//		List<Product> beforePList = bll.queryAllProductInfo();
		
//		Prot10080Product product = new Prot10080Product();
		Prot20006Product product = new Prot20006Product();
		product.reqProductList(create, new Prot20001ReqBean(String.valueOf(deviceId)));
		Prot20001ResBean resBean = product.getResBean();
		if (resBean == null) {
			Message msg = mHandler.obtainMessage();
			msg.what = RequestConstant.REQUEST_PRODUCT_LIST_IS_EMPTY;
			msg.obj = "产品列表为空";
			msg.arg1 = 0;
			mHandler.sendMessage(msg);
			return;
		}
		if (resBean.getResult() == 1) {
			//请求产品列表失败
			Message msg = mHandler.obtainMessage();
			msg.what = RequestConstant.REQUEST_PRODUCT_LIST_FAIL;
			msg.obj = resBean;
			mHandler.sendMessage(msg);
			return;
		}
		if (resBean.getList() == null) {
			//产品列表为空
			Message msg = mHandler.obtainMessage();
			msg.what = RequestConstant.REQUEST_PRODUCT_LIST_IS_EMPTY;
			msg.obj = "产品列表为空";
			msg.arg1 = 0;
			mHandler.sendMessage(msg);
			return;
		}
		
		bll.deleteAllProduct();
		
		bll.insertProductList(resBean.getList());
		mHandler.sendEmptyMessage(RequestConstant.REQUEST_PRODUCT_LIST_SUCCESS);
		/*if (beforePList != null) {
			for (Product p : beforePList) {
				if (!StringUtils.isEmpty(p.getImage())) {
					Product cP = bll.queryProductById(p.getId());
					if (cP != null && !StringUtils.isEmpty(cP.getImage())) {
						if (!cP.getImage().equals(p.getImage())) {
							bitmapService.clearCache(p.getImage());
						} 
					} else {
						bitmapService.clearCache(p.getImage());
					}
				}
			}
		}*/
//		mHandler.sendEmptyMessage(RequestConstant.REQUEST_PRODUCT_LIST_SUCCESS);
			
		/*Prot17GetProducts getProducts = new Prot17GetProducts();
//		LogUtil.d(TConfig.Echo, "开始请求产品列表信息");
		getProducts.dealProductList_V2(create, new CallBack_V2() {

			@Override
			public void okData(List<Product> products, int iTotalCount, int iCurrentRecCount) {
				if (products != null) {
					bll.deleteAllProduct();
					bll.insertProductList(products);
//					LogUtil.d(TConfig.Echo, "请求产品列表成功：" + bll.queryAllProductInfo().toString());
					mHandler.sendEmptyMessage(RequestConstant.REQUEST_PRODUCT_LIST_SUCCESS);
				} else {
					Message msg = mHandler.obtainMessage();
					msg.what = RequestConstant.REQUEST_PRODUCT_LIST_FAIL;
					msg.obj = "产品列表为空";
					msg.arg1 = 0;
					mHandler.sendMessage(msg);
				}
			}

			@Override
			public void errData(String err, int iRecErrCode) {
				Message msg = mHandler.obtainMessage();
				msg.what = RequestConstant.REQUEST_PRODUCT_LIST_FAIL;
				msg.obj = err;
				msg.arg1 = iRecErrCode;
				mHandler.sendMessage(msg);
				LogUtil.e(TConfig.Echo, "err：" + err + " iRecErrCode:" + iRecErrCode);

			}
		});		*/
	}


}
