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

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import android.os.Handler;
import android.os.Message;
import cn.net.caoyao.cpzshandset.bean.StationManagerBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.caoyao.cpzshandset.service.bll.StationManagerBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.bean.res.Prot10050ResBean;
import cn.net.cpzslibs.prot.bean.res.Prot10050ResStationBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20011ReqStationManager;

public class YthReqStationRunable implements Runnable {
	private Handler mHandler;
	private long deveicedId; 
	private StationManagerBll stationBll;
	public YthReqStationRunable(Handler mHandler, long deveicedId,StationManagerBll stationBll) {
		this.mHandler = mHandler;
		this.deveicedId = deveicedId;
		this.stationBll = stationBll;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, String.valueOf(deveicedId));
		try {
			create.createLoginCpzsHandset(deveicedId);
			if (create.isResult()) {
				Prot20011ReqStationManager stationReq = new Prot20011ReqStationManager();
				stationReq.dealStationList(create);
				Prot10050ResBean resBean = stationReq.getStations();
				if (resBean.getResult() == 0) {
					//0：请求成功     1：遇到错误信息，返回错误码
					if (resBean.getAc() != null && resBean.getAc().size()>0) {
						insertStationInfo(resBean.getAc());
					}else {
						mHandler.sendEmptyMessage(RequestConstant.PROT_REQ_STATION_10050_NO_DATA);
					}
				} else {
					Message failMsg = mHandler.obtainMessage();
					failMsg.what = RequestConstant.PROT_REQ_STATION_10050_FAIL;
					failMsg.obj = resBean;
					mHandler.sendMessage(failMsg);
				}
				
			} else {
//				mHandler.sendEmptyMessage(ThreadConstant.)
				Message loginFailMsg = mHandler.obtainMessage();
				loginFailMsg.obj = create.getErrMsg();
				loginFailMsg.arg1 = create.getiErrCode();
				loginFailMsg.what = RequestConstant.REQUEST_PROT01_LOGIN_FAIL;
				mHandler.sendMessage(loginFailMsg);
			}
		}catch (SocketTimeoutException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
		} catch (IOException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} finally{
			new Prot02Logout().dealLogout(create);
		}
	}

	private void insertStationInfo(List<Prot10050ResStationBean> ac) {
		List<StationManagerBean> stations = new ArrayList<StationManagerBean>();
		for (Prot10050ResStationBean ps : ac) {
			StationManagerBean sm = new StationManagerBean();
			sm.setId(ps.getId());
			sm.setLine(ps.getLine());
			sm.setRegion(ps.getRegion());
			sm.setRegionid(ps.getRegion_id());
			sm.setStationdec(ps.getStation_dec());
			sm.setStationname(ps.getStation_name());
			sm.setStatus(0);
			sm.setTransmode(ps.getTrans_mode());
			stations.add(sm);
		}
		stationBll.insertStation(stations);
		mHandler.sendEmptyMessage(RequestConstant.PROT_REQ_STATION_10050_SUCCESS);
	}

}
