package cn.net.caoyao.cpzshandset.service.presenter.impl.shipment.train;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import cn.net.caoyao.cpzshandset.bean.ConfigBean;
import cn.net.caoyao.cpzshandset.bean.TrainInvoiceBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetVersionConstant;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.caoyao.cpzshandset.service.bll.InvoiceBll;
import cn.net.caoyao.cpzshandset.service.bll.TrainInvoiceBll;
import cn.net.caoyao.cpzshandset.service.bll.config.ConfigBll;
import cn.net.caoyao.cpzshandset.service.presenter.OptionHisListStyleConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.OptionHisListStyleConstactImpl;
import cn.net.caoyao.cpzshandset.service.thread.ThreadPoolManager;
import cn.net.caoyao.cpzshandset.service.thread.ThreadReqTools;
import cn.net.caoyao.cpzshandset.service.thread.impl.yth.YthSubmitInvoiceRunable;
import cn.net.caoyao.cpzshandset.ui.activity.ShipmentScannerActivity;
import cn.net.caoyao.cpzshandset.ui.activity.TrainInvoiceConfigActivity;
import cn.net.caoyao.cpzshandset.ui.adapter.TrainInvoiceListAdapter;
import cn.net.caoyao.cpzshandset.ui.dialog.OperationDialog;
import cn.net.caoyao.cpzshandset.ui.dialog.OperationDialog.DClick;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog.DSure;
import cn.net.caoyao.cpzshandset.ui.weight.DialogUtils;
import cn.net.handsetlib.utils.ReadDeviceIDUtil;

public class YthTrainShipmentListPresenter extends OptionHisListStyleConstactImpl{
	private final OptionHisListStyleConstact.View hisView;
	private Context context;
	private List<TrainInvoiceBean> trainInvoices = new ArrayList<TrainInvoiceBean>();
	private TrainInvoiceBll trainBll;
	private ConfigBll configBll;
	private InvoiceBll invoiceBll;
	private TrainInvoiceListAdapter adapter;
	private OperationDialog operationDialog;
	private TipsDialog tipsDialog;
	private ProgressDialog bProgressDialog = null; 
	private int currentHansetVersion = HandsetVersionConstant.HANDSET_YTH_VERSION_MODE;
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case RequestConstant.PROT_SUBMIT_INVOICE_10051_FAIL:
				if (!checkViewAvailable()) return;
					dismissProgress();
					onResume();
				break;
			case RequestConstant.PROT_SUBMIT_INVOICE_10051_SUCCESS:
				if (!checkViewAvailable()) return;
					dismissProgress();
					onResume();
				break;
			case RequestConstant.REQUEST_PROT01_LOGIN_FAIL:
				if (!checkViewAvailable()) return;
					dismissProgress();
					hisView.showMsgToast("登录失败！" +"\n错误码：" + msg.arg1 +"\n原因："+ThreadReqTools.getErrorMsg(msg.arg1),Toast.LENGTH_SHORT);
				break;
			case RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT:
				if (!checkViewAvailable()) return;
					dismissProgress();
					hisView.showMsgToast("连接超时", Toast.LENGTH_SHORT);
				break;
			case RequestConstant.REQUEST_FAIL:
				if (!checkViewAvailable()) return;
				dismissProgress();
				hisView.showMsgToast("请求失败!", Toast.LENGTH_SHORT);
				break;
			}

		}
	};
	public YthTrainShipmentListPresenter(OptionHisListStyleConstact.View hisView, Context context) {
		super(hisView, context);
		this.hisView = hisView;
		this.context = context;
		start();
	}
	
	@Override
	public void start() {
		if (!checkViewAvailable()) return;
		hisView.setPageTitle("火车发货");
		init();
		adapter = new TrainInvoiceListAdapter(trainInvoices);
		if (checkViewAvailable()) {
			hisView.setAdapter(adapter);
		}
		
	}

	public void dismissProgress() {
		if (bProgressDialog != null) {
			try {
				DialogUtils.dismissProgressDialog(bProgressDialog);
			} catch (Exception e) {
			}
		}
	}

	/**
	 * 提示正在更新数据
	 */
	protected void showProgressDialog(String titleStr, String tipsMsg) {
		bProgressDialog = DialogUtils.showProgressDialog(bProgressDialog, titleStr,
				tipsMsg, context);
	}
	private void init() {
		trainBll = new TrainInvoiceBll(context);
		configBll = new ConfigBll(context);
		invoiceBll = new InvoiceBll(context);
	}

	@Override
	public boolean checkViewAvailable() {
		boolean b = false;
		if (hisView != null && context != null) {
			b = true;
		}
		return b;
	}

	@Override
	public Handler getHandler() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onItemClickListener(int position) {
		if (!checkViewAvailable()) return;
		if (trainInvoices.size() > 0) {
			Intent intent = new Intent(context, ShipmentScannerActivity.class);
			intent.putExtra("shipmentMode", 1);
			intent.putExtra("transId", trainInvoices.get(position).getId());
			context.startActivity(intent);
		}
		
	}

	@Override
	public void onItemLongClickListener(int position) {
		if (!checkViewAvailable()) return;
		if (trainInvoices.size() > 0) {
			showOptionDialog(position);
		}
		
	}
	
	private void showOptionDialog(final int position) {
		if (operationDialog == null) {
			operationDialog = new OperationDialog(context);
		}
		operationDialog.show();
		operationDialog.setDClick(position, new DClick() {
			
			@Override
			public void modifyClick() {
				//修改
				if (!checkViewAvailable()) return;
				Intent intent = new Intent(context, TrainInvoiceConfigActivity.class);
				intent.putExtra("isCreate", false);
				intent.putExtra("currentHisId", trainInvoices.get(position).getId());
				context.startActivity(intent);
			}
			
			@Override
			public void delClick() {
				// 删除
				if (!checkViewAvailable()) return;
				String msg = "本条数据还未上传\n\n是否确定删除火车发货数据？\n\n确定删除后\n\n该数据对应的所有信息都会被删除" ;
				showWarnTipDialog(msg,position);
			}

			@Override
			public void searckClick() {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
	
	protected void showWarnTipDialog(String msg,final int position) {
		if (tipsDialog == null) {
			tipsDialog = new TipsDialog(context);
		}
		tipsDialog.show();
		tipsDialog.setDelWarnView("警告", msg, position, false, new DSure() {
			
			@Override
			public void sureClick() {
				trainBll.deleteTrainInvoiceById(trainInvoices.get(position));
				onResume();
				hisView.showMsgToast("删除成功！", Toast.LENGTH_SHORT);
			}
		});
		
	}

	@Override
	public void backBtnClick() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void moreOptionClick() {
		if (!checkViewAvailable()) return;
		Intent intent = new Intent(context, TrainInvoiceConfigActivity.class);
		intent.putExtra("isCreate", true);
		context.startActivity(intent);
		
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResume() {
		if (!checkViewAvailable()) return;
		trainInvoices = trainBll.queryAllTrainInvoices();
		adapter.refreshData(trainInvoices);
	}

	@Override
	public void uploadAllHisList() {
		if (!checkViewAvailable()) return;
		ConfigBean configBean = configBll.queryConfigByHandsetMode(currentHansetVersion);
		if (configBean == null) {
			hisView.showMsgToast("当前设备还未配置过手持机id，请先配置手持机的设备id", Toast.LENGTH_SHORT);
			return;
		}
		if (trainInvoices == null || trainInvoices.size() == 0) {
			hisView.showMsgToast("抱歉，当前还没有未上传发货列表，不可上传", Toast.LENGTH_SHORT);
			return;
		}
		
		showProgressDialog("提示", "正在上传发货信息，请稍后……");
		long deviceId = Long.parseLong(ReadDeviceIDUtil.readScanDecrypt(configBean.getDeviceId()));
//		LogUtil.d(TConfig.Echo, "devicedId:" + deviceId);
		ThreadPoolManager.getThreadPool().execute(new YthSubmitInvoiceRunable(mHandler, deviceId, trainBll, null, invoiceBll, 1));
	}

}
