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

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

import android.content.Context;
import android.content.Intent;

import cn.net.caoyao.cpzshandset.R;

import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.service.bll.UploadStatusBll;
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.activity.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.activity.UploadStatusActivity;
import cn.net.caoyao.cpzshandset.ui.adapter.HomeAdapter;

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

	@Override
	public void start() {
		initHomeEumnBeans();
		if (!checkViewAvailable()) return;
		//清除七天前的提交记录数据
		clearStatusData();
		homeView.setAdapter(new HomeAdapter(homeBeans));
		homeView.setHomeTitle(context.getResources().getString(R.string.home_counter_title_str));
	}

	private void clearStatusData() {
		UploadStatusBll uploadStatusBll = new UploadStatusBll(context);
		uploadStatusBll.delete7DaysBefore();
	}

	private void initHomeEumnBeans() {
		homeBeans.add(HandsetOperation.OPERATION_COUNTER_SALE_CONSTANT);
		homeBeans.add(HandsetOperation.OPERATION_RETURNED_GOODS_CONSTANT);
		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 option) {
//		if (homeBean == null || homeBean.getOperationId() < 0) return;
		switch (option) {
		case OPERATION_RETURNED_GOODS_CONSTANT:
		case OPERATION_COUNTER_SALE_CONSTANT:
			//拆包
			if (!checkViewAvailable()) return;
			Intent unpackIntent = new Intent(context, SingleLabelScannerActivity.class);
			unpackIntent.putExtra("optionMode", option);
			context.startActivity(unpackIntent);
			break;
		case OPERATION_SETTING_CONSTANT:
			//设置  
			if (!checkViewAvailable()) return;
			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;
//			homeView.showMsgToast("检测", Toast.LENGTH_SHORT);
			Intent intent = new Intent(context, JianCeActivity.class);
			context.startActivity(intent);
			break;
			default :
				switchPage(option);
				break;

		}
		
	}

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

}
