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

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

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.service.bll.ShipmentUploadStatusBll;
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.LoginActivity;
import cn.net.caoyao.cpzshandset.ui.activity.OptionHistoryListStyleActivity;
import cn.net.caoyao.cpzshandset.ui.activity.SettingActivity;
import cn.net.caoyao.cpzshandset.ui.activity.UploadStatusActivity;
import cn.net.caoyao.cpzshandset.ui.adapter.HomeAdapter;
import cn.net.cpzslibs.prot.handset.bean.res.ResponsibleUserInfoBean;

public class YthPunitHomePresenter extends HomeConstactImpl {
	private final HomeConstact.View homeView;
	private Context context;
	private List<HandsetOperation> homeBeans = new ArrayList<HandsetOperation>();
	private ResponsibleUserInfoBean uInfoBean;
	
	public YthPunitHomePresenter(Context context,HomeConstact.View homeView,Intent intent){
		this.context = context;
		this.homeView = homeView;
		uInfoBean = (ResponsibleUserInfoBean) intent.getSerializableExtra("userInfo");
		start();
	}

	@Override
	public void start() {
		initHomeBeans();
		if (!checkViewAvailable()) return;
		//清除七天前的提交记录数据
		clearStatusData();
		homeView.setLogoutVisibility(View.VISIBLE);
		homeView.setAdapter(new HomeAdapter(homeBeans));
		homeView.setHomeTitle(context.getResources().getString(R.string.home_yth_login_title_str));
	}
	
	private void clearStatusData() {
		ShipmentUploadStatusBll uploadStatusBll = new ShipmentUploadStatusBll(context);
		uploadStatusBll.delete7DaysBefore();
	}

	private void initHomeBeans() {
		homeBeans.add(HandsetOperation.OPERATION_TRUCK_SHIPMENT_CONSTANT);
		HandsetOperation.OPERATION_UPLOAD_STATUS_CONSTANT.setOptionName("上传记录");
		homeBeans.add(HandsetOperation.OPERATION_UPLOAD_STATUS_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);
			Intent statusIntent = new Intent(context, UploadStatusActivity.class);
			context.startActivity(statusIntent);
			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);
		activateIntent.putExtra("userInfo", uInfoBean);
		context.startActivity(activateIntent);		
	}

	@Override
	public void logoutClick() {
		if(!checkViewAvailable()) return;
		homeView.showMsgToast("退出登录", Toast.LENGTH_SHORT);
		Intent intent = new Intent(context, LoginActivity.class);
		context.startActivity(intent);
		homeView.finishPage();
	}

}
