package cn.net.caoyao.cpzshandset.service.presenter.impl.home.yth;

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

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.service.presenter.HomeConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.HomeConstactImpl;
import cn.net.caoyao.cpzshandset.ui.activity.JianCeActivity;
import cn.net.caoyao.cpzshandset.ui.activity.OptionHistoryListStyleActivity;
import cn.net.caoyao.cpzshandset.ui.activity.SettingActivity;
import cn.net.caoyao.cpzshandset.ui.adapter.HomeAdapter;

public class YthHomePresenter extends HomeConstactImpl {
	private final HomeConstact.View homeView;
	private Context context;
	private List<HandsetOperation> homeBeans = new ArrayList<HandsetOperation>();
	
	public YthHomePresenter(Context context,HomeConstact.View homeView){
		this.context = context;
		this.homeView = homeView;
		start();
	}

	@Override
	public void start() {
		initHomeBeans();
		if (!checkViewAvailable()) return;
		homeView.setAdapter(new HomeAdapter(homeBeans));
		homeView.setHomeTitle(context.getResources().getString(R.string.home_yth_title_str));
//		LogUtil.e(TConfig.Echo, getPY("1345687")  +"  拼音：" + getPY("煤球") +"  测试2：" + getPY("张没加")+"  测试3：" + getPY("测试3")+"  测试4：" + getPY("测试45HJ测试") );
		/*String str = "重庆九禾测土配肥有限责任公司";
		Py4j py4j = new Py4j();
		LogUtil.d(TConfig.Echo, str + "  拼音：" + py4j.getPreLetterPY(str));
		py4j = null;*/
	}
	
	/*private String getPY(String contentStr) {
		if (StringUtils.isEmpty(contentStr)) return "";
		char[] c = contentStr.toCharArray();
		if (c == null) return "";
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < c.length; i++) {
			String s = PinyinUtil.toPinyin(context, c[i]);//.substring(0, 1).toUpperCase()
			if (StringUtils.isEmpty(s)) {
				sb.append(c[i]);
			}else {
				sb.append(s.substring(0, 1).toUpperCase());	
			}
			
		}
		
		return sb.toString();
	}*/

	private void initHomeBeans() {
//		HomeBean activateBean = new HomeBean(HandsetOperationConstant.OPERATION_ACTIVATE_CONSTANT,R.drawable.home_activate_icon,context.getResources().getString(R.string.home_activate_str));
//		homeBeans.add(activateBean);
		homeBeans.add(HandsetOperation.OPERATION_TRAIN_SHIPMENT_CONSTANT);
		homeBeans.add(HandsetOperation.OPERATION_INSPECT_CONSTANT);
		homeBeans.add(HandsetOperation.OPERATION_SETTING_CONSTANT);
	}

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

	@Override
	public void onItemClickListener(int position) {
		if (!checkViewAvailable() || homeBeans == null || homeBeans.size() < position)return;
		switchCurrentOption(homeBeans.get(position));
	}


	private void switchCurrentOption(HandsetOperation homeBean) {
		if (homeBean == null || homeBean.getOption() < 0) return;
		switch (homeBean) {
		case OPERATION_SETTING_CONSTANT:
			//设置
			if (!checkViewAvailable()) return;
//			homeView.showMsgToast("设置", Toast.LENGTH_SHORT);
			Intent settingIntent = new Intent(context,SettingActivity.class);
			context.startActivity(settingIntent);
			break;
		case OPERATION_UPLOAD_STATUS_CONSTANT:
			//提交状态
			if (!checkViewAvailable()) return;
			homeView.showMsgToast("提交状态", Toast.LENGTH_SHORT);
			break;
		case OPERATION_INSPECT_CONSTANT:
			//检测
			if (!checkViewAvailable()) return;
			Intent intent = new Intent(context, JianCeActivity.class);
			context.startActivity(intent);
			break;
		case OPERATION_QUERY_CONSTANT:
			//查询
			// TODO 通用版暂时无该功能
			break;
		case OPERATION_UPLOAD_CONSTANT:
			//上传
			// TODO 通用版暂时无该功能
			break;
			default :
				switchPage(homeBean);
				break;

		}
		
	}

	private void switchPage(HandsetOperation homeBean) {
		Intent activateIntent = new Intent(context, OptionHistoryListStyleActivity.class);
		activateIntent.putExtra("optionMode", homeBean);
		context.startActivity(activateIntent);		
	}

}
