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

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 SheepExHomePresenter extends HomeConstactImpl {
	private final HomeConstact.View homeView;
	private Context context;
	private List<HandsetOperation> homeBeans = new ArrayList<HandsetOperation>();
	
	public SheepExHomePresenter(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_sheep_ex_title_str));
	}
	

	private void initHomeBeans() {
		homeBeans.add(HandsetOperation.OPERATION_EXCHANGE_CARGO_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);		
	}

}
