package cn.net.caoyao.cpzshandset.service.presenter.impl.setting.seeds;

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.widget.Toast;
import cn.net.caoyao.cpzshandset.bean.ConfigBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetSettingOption;
import cn.net.caoyao.cpzshandset.common.config.HandsetVersionConstant;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.caoyao.cpzshandset.service.bll.CertificateBll;
import cn.net.caoyao.cpzshandset.service.bll.LandBll;
import cn.net.caoyao.cpzshandset.service.bll.ProductBll;
import cn.net.caoyao.cpzshandset.service.bll.config.ConfigBll;
import cn.net.caoyao.cpzshandset.service.presenter.SettingConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.SettingConstactImpl;
import cn.net.caoyao.cpzshandset.service.thread.ThreadPoolManager;
import cn.net.caoyao.cpzshandset.service.thread.impl.seeds.SeedsUpdateProductListThread;
import cn.net.caoyao.cpzshandset.ui.activity.DeviceIdConfigActivity;
import cn.net.caoyao.cpzshandset.ui.activity.ModifyHandsetVersionActivity;
import cn.net.caoyao.cpzshandset.ui.adapter.SettingAdapter;
import cn.net.caoyao.cpzshandset.ui.weight.DialogUtils;
import cn.net.handsetlib.utils.ReadDeviceIDUtil;

public class SeedsSettingPresenter extends SettingConstactImpl {
	private final SettingConstact.View sView;
	private Context context;
	private List<HandsetSettingOption> beans = new ArrayList<HandsetSettingOption>();
	private ConfigBll configBll;
	private ProgressDialog bProgressDialog = null;
	private int currentHandsetVersion = HandsetVersionConstant.HANDSET_SEED_VERSION_MODE;
	private ProductBll productBll;
	private LandBll landBll;
	private CertificateBll certificateBll;

	public SeedsSettingPresenter(SettingConstact.View sView, Context context) {
		this.sView = sView;
		this.context = context;
		start();
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
		configBll = new ConfigBll(context);
		initSettingData();
		SettingAdapter adapter = new SettingAdapter(beans);
		sView.setAdapter(adapter);
	}

	private void initSettingData() {
		beans.add(HandsetSettingOption.SETTING_CONFIG_DEVICE_ID_MODE);
		beans.add(HandsetSettingOption.SETTING_UPDATE_PRODUCT_LIST_MODE);
		if (Config.canSwitchHandsetVersionMode) {
			beans.add(HandsetSettingOption.SETTING_HANDSET_MODE_SWITCH_MODE);
		}
	}

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

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

	}

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

	@Override
	public void onItemClickListener(int position) {
		// TODO Auto-generated method stub
		selectOption(beans.get(position));
	}

	private void selectOption(HandsetSettingOption setOption) {
		switch (setOption) {
		case SETTING_HANDSET_MODE_SWITCH_MODE:
			// 修改手持机版本模式
			Intent modifyVersionIntent = new Intent(context,
					ModifyHandsetVersionActivity.class);
			context.startActivity(modifyVersionIntent);
			break;
		case SETTING_CONFIG_DEVICE_ID_MODE:
			// 配置设备ID
			Intent intent = new Intent(context, DeviceIdConfigActivity.class);
			context.startActivity(intent);
			break;
		case SETTING_UPDATE_PRODUCT_LIST_MODE:
			// 根据模式查找配置表中的配置信息
			ConfigBean configBean = configBll
					.queryConfigByHandsetMode(currentHandsetVersion);
			if (configBean != null) {
				showProgressDialog("提示", "正在请求产品信息，请稍后……");
				long deviceId = Long.parseLong(ReadDeviceIDUtil
						.readScanDecrypt(configBean.getDeviceId()));
				ThreadPoolManager.getThreadPool().execute(
						new SeedsUpdateProductListThread(mHandler, deviceId,
								getProductBll(), getLandBll(),
								getCertificateBll()));

			} else {
				sView.showMsgToast("当前设备还未配置过手持机id，请先配置手持机的设备id",
						Toast.LENGTH_SHORT);
			}

			break;
		default:
			break;
		}
	}

	/**
	 * 提示正在更新数据
	 */
	protected void showProgressDialog(String titleStr, String tipsMsg) {
		bProgressDialog = DialogUtils.showProgressDialog(bProgressDialog,
				titleStr, tipsMsg, context);
	}
	/**
	 * 取消对话框
	 */
	public void dismissProgress() {
		if (bProgressDialog != null) {
			try {
				DialogUtils.dismissProgressDialog(bProgressDialog);
			} catch (Exception e) {
			}
		}
	}

	private ProductBll getProductBll() {
		if (productBll == null) {
			productBll = new ProductBll(context);
		}
		return productBll;
	}

	private LandBll getLandBll() {
		if (landBll == null) {
			landBll = new LandBll(context);
		}
		return landBll;
	}

	private CertificateBll getCertificateBll() {
		if (certificateBll == null) {
			certificateBll = new CertificateBll(context);

		}
		return certificateBll;
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case RequestConstant.REQUEST_PRODUCT_LIST_SUCCESS:
				//请求产品信息成功
				if (!checkViewAvailable())return;
				dismissProgress();
				sView.showMsgToast("请求产品信息成功！", Toast.LENGTH_SHORT);
				break;

			default:
				break;
			}
		};
	};
}
