package cn.net.caoyao.cpzshandset.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.HomeBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetVersionConstant;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.CachePut;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseActivity;
import cn.net.caoyao.cpzshandset.ui.adapter.ListTextAdapter;
import cn.net.caoyao.cpzshandset.ui.adapter.ListTextAdapter.IOnItemSelectListener;
import cn.net.caoyao.cpzshandset.ui.adapter.SpinnerTextPopWindow;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog.DSure;

public class ModifyHandsetVersionActivity extends BaseActivity implements OnClickListener {
	private int currentHandsetVersion = Config.CURRENT_HANDSET_VERSION_MODE;
	private int currentScannerMode = Config.CURRENT_SCANNER_MODE;
	private CachePut cacheP;
	private Context context;
	private View lin_modify_handset_version,lin_modify_scanner_mode,image_modify_handset_version,image_modify_scanner_mode,rel_modify_handset_version,rel_modify_scanner_mode;
	private TextView tv_modify_handset_version,tv_modify_scanner_mode,tips_info;
	private List<String> handsetVersions = new ArrayList<>();
	private Map<String, Integer> handsetOptionMap = new HashMap<>();
	private SpinnerTextPopWindow handsetSpinerPopWindow;
	private List<String> scannerModes = new ArrayList<>();
	private Map<String, Integer> scannerModesnMap = new HashMap<>();
	private SpinnerTextPopWindow scannerModesSpinerPopWindow;
	private TipsDialog tipsDialog;
	
	@Override
	public int loadView() {
		cacheP = new CachePut();
		context = this;
		return R.layout.activity_modify_handset_version;
	}

	@Override
	public void initTitleView() {
		View image_title_back_common = findViewById(R.id.image_title_back_common);
		image_title_back_common.setVisibility(View.VISIBLE);
		image_title_back_common.setOnClickListener(this);
		TextView tv_title_common = (TextView) findViewById(R.id.tv_title_common);
		tv_title_common.setText("修改手持机模式");
		Button btn_title_more_option = (Button) findViewById(R.id.btn_title_more_option);
		btn_title_more_option.setVisibility(View.VISIBLE);
		btn_title_more_option.setText("完成");
		btn_title_more_option.setOnClickListener(this);

	}

	@Override
	public void initView() {
		rel_modify_scanner_mode = findViewById(R.id.rel_modify_scanner_mode);
		rel_modify_handset_version = findViewById(R.id.rel_modify_handset_version);
		lin_modify_handset_version = findViewById(R.id.lin_modify_handset_version);
		lin_modify_scanner_mode = findViewById(R.id.lin_modify_scanner_mode);
		image_modify_handset_version = findViewById(R.id.image_modify_handset_version);
		image_modify_scanner_mode = findViewById(R.id.image_modify_scanner_mode);
		tv_modify_handset_version = (TextView) findViewById(R.id.tv_modify_handset_version);
		tv_modify_scanner_mode = (TextView) findViewById(R.id.tv_modify_scanner_mode);
		tips_info = (TextView) findViewById(R.id.tips_info);
		
		if (!Config.canSwitchHandsetVersionMode) {
			lin_modify_handset_version.setVisibility(View.GONE);
		} else {
			currentHandsetVersion = cacheG.getCacheIntegerG(context, Config.handsetVersionConfigSp, Config.currentHandsetVersion);
			tv_modify_handset_version.setText(getHandsetVersionStr(currentHandsetVersion));
		}
		if (!Config.canSwitchScannerMode) {
			lin_modify_scanner_mode.setVisibility(View.GONE);
		} else {
			currentScannerMode= cacheG.getCacheIntegerG(context, Config.handsetVersionConfigSp, Config.currentScannerMode);
			tv_modify_scanner_mode.setText(getScannerMode(currentScannerMode));
		}
		
		String ipTips = Config.isDebuger ? "测试版" :"正式版";
		String tipsStr = "提示信息："+" \n\n" +
				"版本信息：" + ipTips  +"   port：" + Config.port +" \n\n" +
		"当前使用的手持机版本：" + getHandsetVersionStr(currentHandsetVersion) +"\n\n" +
				"当前使用的扫码方式：" + getScannerMode(currentScannerMode);
		tips_info.setText(tipsStr);
	}

	/**
	 * 获取手持机版本的名称
	 * @param currentHandsetVersion2
	 * @return
	 */
	private String getHandsetVersionStr(int currentHandsetVersion2) {
		String str = "通用版";
		switch (currentHandsetVersion2) {
		case HandsetVersionConstant.HANDSET_UNIVERSAL_VERSION_MODE:
			// 通用版
			str = "中草药";
			break;
		case HandsetVersionConstant.HANDSET_RICE_VERSION_MODE:
			// 大米
			str = "大米";
			break;
		case HandsetVersionConstant.HANDSET_HOLOTHURIAN_VERSION_MODE:
			// 海参
			str = "海参";
			break;
		case HandsetVersionConstant.HANDSET_WINE_TERMINAL_VERSION_MODE:
			// 酒终端
			str = "酒终端";
			break;
		case HandsetVersionConstant.HANDSET_WINE_DEALER_VERSION_MODE:
			// 酒经销商
			str = "酒经销商";
			break;
		case HandsetVersionConstant.HANDSET_FIRE_EXTINGUISHER_VERSION_MODE:
			// 灭火器
			str = "灭火器";
			break;
		case HandsetVersionConstant.HANDSET_AGRICULTURAL_PRODUCTS_VERSION_MODE:
			// 农产品
			str = "农产品";
			break;
		case HandsetVersionConstant.HANDSET_SEED_VERSION_MODE:
			// 种子
			str = "种子";
			break;
		case HandsetVersionConstant.HANDSET_YTH_VERSION_MODE:
			// 云天化
			str = "云天化";
			break;
		case HandsetVersionConstant.HANDSET_YTH_IMAGE_VERSION_MODE:
			//云天化图片版
			str = "云天化图片版激活";
			break;
		case HandsetVersionConstant.HANDSET_CHINATEX_VERSION_MODE:
			//中纺
			str = "中纺";
			break;
		case HandsetVersionConstant.HANDSET_YTH_LOGIN_SHIPMENT_VERSION_MODE:
			str = "云天化登录版";
			break;
		case HandsetVersionConstant.HANDSET_SHEEP_EXCHANGE_CARGO:
			str = "羊老大调货";
			break;
		case HandsetVersionConstant.HANDSET_YTH_SCANNER_SHIPMENT_VERSION_MODE:
			str = "云天化扫码版物流";
			break;
		case HandsetVersionConstant.HANDSET_LIQUEUR_VERSION_MODE:
			str = "白酒追溯";
			break;
		case HandsetVersionConstant.HANDSET_MEDICINE_XY_VERSION_MODE:
			str = "上药终端";
			break;
		case HandsetVersionConstant.HANDSET_COUNTER_VERSION_MODE:
			str = "专柜终端";
			break;
		case HandsetVersionConstant.HANDSET_UNIVERSAL_EGG_VERSION_MODE:
			str = "通用版-egg";
			break;
		case HandsetVersionConstant.HANDSET_UNIVERSAL_VERSION_MODE_FARMERS:
			str = "通用版-Farmers";
			break;	
		case HandsetVersionConstant.HANDSET_YTH_SCANNER_SHIPMENT_PUNIT_VERSION_MODE:
			str = "云天化-扫码Plus";
			break;		
		case HandsetVersionConstant.HANDSET_YUN_CAR_VERSION_MODE:
			str = "汽配-仅销售";
			break;
		case HandsetVersionConstant.HANDSET_KIA_PRODUCT_LINE_VERSION_MODE:
			str = "起亚";
			break;		
			
		}
		
		return str;
	}

	/**
	 * 获取扫码模式
	 * @param currentScannerMode2
	 * @return
	 */
	private String  getScannerMode(int currentScannerMode2) {
		String str = "手持机扫码";
		switch (currentScannerMode2) {
		case 0:
			str = "手持机扫码";
			break;

		case 1:
			str = "手机扫码";
			break;
			
		case 2:
			
			str = "东大集成";
			break;	
		}
		return str;
	}

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

	}

	@Override
	public void setListener() {
		image_modify_handset_version.setOnClickListener(this);
		image_modify_scanner_mode.setOnClickListener(this);

	}

	@Override
	public void initData() {
		initHandsetVersionData();
		initHandsetSpinnerView();
		initScannerModeData();
		initScannerModeSpinnerView();
	}
	
	private void initScannerModeData() {
		HomeBean handsetMode = new HomeBean(0, 0, "手持机扫码");
		HomeBean phoneMode = new HomeBean(1, 0, "手机扫码");
		HomeBean ddjcMode = new HomeBean(2, 0, "东大集成");
		
		scannerModes.add(handsetMode.getOpreationName());
		scannerModesnMap.put(handsetMode.getOpreationName(), handsetMode.getOperationId());
		scannerModes.add(phoneMode.getOpreationName());
		scannerModesnMap.put(phoneMode.getOpreationName(), phoneMode.getOperationId());
		scannerModes.add(ddjcMode.getOpreationName());
		scannerModesnMap.put(ddjcMode.getOpreationName(), ddjcMode.getOperationId());
		
	}
	
	private void initHandsetVersionData() {
		
		HomeBean common = new HomeBean(HandsetVersionConstant.HANDSET_UNIVERSAL_VERSION_MODE, 0, "通用版");
		HomeBean yth = new HomeBean(HandsetVersionConstant.HANDSET_YTH_VERSION_MODE,0,"云天化");
		HomeBean ythImage = new HomeBean(HandsetVersionConstant.HANDSET_YTH_IMAGE_VERSION_MODE, 0, "云天化图片版激活");
		HomeBean seeds = new HomeBean(HandsetVersionConstant.HANDSET_SEED_VERSION_MODE, 0, "种子");
		HomeBean chinatex = new HomeBean(HandsetVersionConstant.HANDSET_CHINATEX_VERSION_MODE, 0, "中纺");
		HomeBean ythLoginShipment = new HomeBean(HandsetVersionConstant.HANDSET_YTH_LOGIN_SHIPMENT_VERSION_MODE, 0, "云天化登录版发货");
		HomeBean sheepEx = new HomeBean(HandsetVersionConstant.HANDSET_SHEEP_EXCHANGE_CARGO, 0, "羊老大调货");
		HomeBean ythScannerShipment = new HomeBean(HandsetVersionConstant.HANDSET_YTH_SCANNER_SHIPMENT_VERSION_MODE, 0, "云天化扫码版物流");
		HomeBean liqueur = new HomeBean(HandsetVersionConstant.HANDSET_LIQUEUR_VERSION_MODE, 0, "白酒追溯");
		HomeBean medicine = new HomeBean(HandsetVersionConstant.HANDSET_MEDICINE_XY_VERSION_MODE,0,"上药终端");
		HomeBean counter = new HomeBean(HandsetVersionConstant.HANDSET_COUNTER_VERSION_MODE, 0, "专柜终端");
		HomeBean common_egg = new HomeBean(HandsetVersionConstant.HANDSET_UNIVERSAL_EGG_VERSION_MODE,0,"通用版-egg");
		HomeBean common_farmers = new HomeBean(HandsetVersionConstant.HANDSET_UNIVERSAL_VERSION_MODE_FARMERS,0,"通用版-farmers");
		HomeBean yth_Punit = new HomeBean(HandsetVersionConstant.HANDSET_YTH_SCANNER_SHIPMENT_PUNIT_VERSION_MODE,0,"云天化-扫码Plus");
		HomeBean yunCar = new HomeBean(HandsetVersionConstant.HANDSET_YUN_CAR_VERSION_MODE,0,"汽配—仅出库");
		HomeBean KIA = new HomeBean(HandsetVersionConstant.HANDSET_KIA_PRODUCT_LINE_VERSION_MODE,0,"起亚");
		
		handsetVersions.add(common.getOpreationName());
		handsetOptionMap.put(common.getOpreationName(), common.getOperationId());
		handsetVersions.add(yth.getOpreationName());
		handsetOptionMap.put(yth.getOpreationName(), yth.getOperationId());
		handsetVersions.add(ythImage.getOpreationName());
		handsetOptionMap.put(ythImage.getOpreationName(), ythImage.getOperationId());
		handsetVersions.add(seeds.getOpreationName());
		handsetOptionMap.put(seeds.getOpreationName(), seeds.getOperationId());
		handsetVersions.add(chinatex.getOpreationName());
		handsetOptionMap.put(chinatex.getOpreationName(), chinatex.getOperationId());
		handsetVersions.add(ythLoginShipment.getOpreationName());
		handsetOptionMap.put(ythLoginShipment.getOpreationName(), ythLoginShipment.getOperationId());
		handsetVersions.add(sheepEx.getOpreationName());
		handsetOptionMap.put(sheepEx.getOpreationName(), sheepEx.getOperationId());
		handsetVersions.add(ythScannerShipment.getOpreationName());
		handsetOptionMap.put(ythScannerShipment.getOpreationName(), ythScannerShipment.getOperationId());
		handsetVersions.add(liqueur.getOpreationName());
		handsetOptionMap.put(liqueur.getOpreationName(), liqueur.getOperationId());
		handsetVersions.add(medicine.getOpreationName());
		handsetOptionMap.put(medicine.getOpreationName(), medicine.getOperationId());
		handsetVersions.add(counter.getOpreationName());
		handsetOptionMap.put(counter.getOpreationName(), counter.getOperationId());
		handsetVersions.add(common_egg.getOpreationName());
		handsetOptionMap.put(common_egg.getOpreationName(), common_egg.getOperationId());
		handsetVersions.add(common_farmers.getOpreationName());
		handsetOptionMap.put(common_farmers.getOpreationName(), common_farmers.getOperationId());
		handsetVersions.add(yth_Punit.getOpreationName());
		handsetOptionMap.put(yth_Punit.getOpreationName(), yth_Punit.getOperationId());
		handsetVersions.add(yunCar.getOpreationName());
		handsetOptionMap.put(yunCar.getOpreationName(), yunCar.getOperationId());
		
		handsetVersions.add(KIA.getOpreationName());
		handsetOptionMap.put(KIA.getOpreationName(), KIA.getOperationId());
		
	}

	private void showScannerModeSpinnerWindow(){
		scannerModesSpinerPopWindow.setWidth(rel_modify_scanner_mode.getWidth());
		scannerModesSpinerPopWindow.showAsDropDown(tv_modify_scanner_mode);
    }
	private void showHandsetSpinnerWindow(){
		handsetSpinerPopWindow.setWidth(rel_modify_handset_version.getWidth());
		handsetSpinerPopWindow.showAsDropDown(tv_modify_handset_version);
    }
	
	/**
	 * 初始化可选手持机的信息
	 */
	private void initScannerModeSpinnerView() {
		ListTextAdapter mAdapter = new ListTextAdapter(scannerModes);
      //初始化PopWindow
		scannerModesSpinerPopWindow = new SpinnerTextPopWindow(this);
		scannerModesSpinerPopWindow.setAdatper(mAdapter);
		scannerModesSpinerPopWindow.setItemListener(new IOnItemSelectListener() {
			@Override
			public void onItemClick(int pos) {
//				startStation = startStations.get(pos);
				currentScannerMode = scannerModesnMap.get(scannerModes.get(pos));
				tv_modify_scanner_mode.setText(getScannerMode(currentScannerMode));
			}
		});		
	
	}
	/**
	 * 初始化可选手持机的信息
	 */
	private void initHandsetSpinnerView() {
		ListTextAdapter mAdapter = new ListTextAdapter(handsetVersions);
      //初始化PopWindow
		handsetSpinerPopWindow = new SpinnerTextPopWindow(this);
		handsetSpinerPopWindow.setAdatper(mAdapter);
		handsetSpinerPopWindow.setItemListener(new IOnItemSelectListener() {
			@Override
			public void onItemClick(int pos) {
//				startStation = startStations.get(pos);
				currentHandsetVersion = handsetOptionMap.get(handsetVersions.get(pos));
				tv_modify_handset_version.setText(getHandsetVersionStr(currentHandsetVersion));
				LogUtil.d(TConfig.Echo, "pos:" + pos + " currentHandsetVersion:" + currentHandsetVersion  +"  name:" + getHandsetVersionStr(currentHandsetVersion));
			}
		});		
	
	}
	
	private void showTipsDialog() {
		if (tipsDialog == null) {
			tipsDialog = new TipsDialog(context);
		}
		tipsDialog.show();
		String tipsContent = "当前修改的操作为：" + "\n\n"
		+"手持机版本：" + getHandsetVersionStr(currentHandsetVersion) +"\n\n"
		+"扫码方式：" +getScannerMode(currentScannerMode) +"\n\n"
		+"是否确认修改？";
		tipsDialog.setWarnView("警告", tipsContent, false, new DSure() {

			@Override
			public void sureClick() {
				cacheP.putCacheInterG(context, Config.handsetVersionConfigSp, Config.currentHandsetVersion, currentHandsetVersion);
				cacheP.putCacheInterG(context, Config.handsetVersionConfigSp, Config.currentScannerMode, currentScannerMode);
				Intent intent = new Intent(context, SplashActivity.class);
		        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
		        startActivity(intent);
		        // 杀掉进程 会重启程序 然后读取缓存中的配置来改变模式
		        android.os.Process.killProcess(android.os.Process.myPid());
		        System.exit(0);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.image_modify_scanner_mode:
			//更换扫码模式
			showScannerModeSpinnerWindow();
			break;
		case R.id.image_modify_handset_version:
			//更换手持机版本
			showHandsetSpinnerWindow();
			break;
		case R.id.image_title_back_common:
			finish();
			break;

		case R.id.btn_title_more_option:
			showTipsDialog();
			break;
		}
		
	}

}
