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

import java.io.IOException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import android.os.Handler;
import android.os.Message;

import cn.net.caoyao.cpzshandset.bean.SingleLabelOptionBean;
import cn.net.caoyao.cpzshandset.bean.UploadBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
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.ReturnedGoodsBll;
import cn.net.caoyao.cpzshandset.service.bll.SingleLabelBll;
import cn.net.caoyao.cpzshandset.service.bll.UploadStatusBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.bean.res.ProtStatusResBean;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20049ReqBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20049SubmitLabelOption;

public class LiqueurSFYSubmitReturnedGoodsThread implements Runnable {
	private long deviceId;
	private ReturnedGoodsBll bll;
	private SingleLabelBll labelBll;
	private UploadStatusBll uploadStatusBll;
	private Handler mHandler;
	private int option_type = HandsetOperation.OPERATION_RETURNED_GOODS_CONSTANT.getOption();
	private int option_id;
	private String account;

	public LiqueurSFYSubmitReturnedGoodsThread(Handler mHandler,long deviceId,ReturnedGoodsBll bll,SingleLabelBll labelBll,UploadStatusBll uploadStatusBll,int option_id,String account) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		this.bll = bll;
		this.labelBll = labelBll;
		this.uploadStatusBll = uploadStatusBll;
		this.option_id = option_id;
		this.account = account;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			LogUtil.d(TConfig.Echo, "登录");
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				submitReturnedGoods(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 submitReturnedGoods(SocketCreate create) throws IOException{
		Prot20049SubmitLabelOption labelOption = new Prot20049SubmitLabelOption();
		labelOption.submitLabelOptionInfo(create, getProt20049ReqBean());
		ProtStatusResBean resBean = labelOption.getResBean();
		if (resBean != null ) {
			if (resBean.getResult() == 0) {
				//成功
				bll.updateReturnedGoodsSubmitStatus(Config.SUBMIT_STATUS_NOT_SUBMIT, 0,  option_id);
				uploadStatusBll.insertUploadStatus(getUploadBean());
				labelBll.deleteLabelById(option_type, option_id);
				
			} else {
				//失败
				bll.updateReturnedGoodsSubmitStatus(Config.SUBMIT_STATUS_SUBMIT_FAIL, resBean.getErrcode(), option_id);
			}
		}
		Message msg = mHandler.obtainMessage(RequestConstant.PROT_SUBMIT_COUNTER_LABEL_20043_COMPLETE);
		msg.obj = resBean;
		mHandler.sendMessage(msg);
//		mHandler.sendEmptyMessage(RequestConstant.PROT_SUBMIT_COUNTER_LABEL_20043_COMPLETE);
		
		
		
	}


	private Prot20049ReqBean getProt20049ReqBean() {
		Prot20049ReqBean p = new Prot20049ReqBean();
		p.setLabels(getLabels());
		p.setOpt(3);//1-出库 2-销售 3-退货
		p.setAccount(account);
		LogUtil.e(TConfig.Echo, "退货：" + p.toString());
		return p;
	}


	private UploadBean getUploadBean() {
		UploadBean b = new UploadBean();
		b.setLabel_count(labelBll.queryLabelCountById(option_type, option_id));
		b.setOption_type(option_type);
		return b;
	}


	private List<String> getLabels() {
		List<String> singles = new ArrayList<>();
		List<SingleLabelOptionBean> singleOptions = labelBll.queryAllLabelById(option_type, option_id);
		for (SingleLabelOptionBean sLBean : singleOptions) {
			singles.add(sLBean.getLabel_id());
		}
		return singles;
	}


}
