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

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.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.util.CachePut;
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.LoginActivity;
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;
import cn.net.cpzslibs.prot.handset.bean.res.ResponsibleUserInfoBean;

public class LiqueurSFYHomePresenter extends HomeConstactImpl {
	private final HomeConstact.View homeView;
	private Context context;
	private List<HandsetOperation> homeBeans = new ArrayList<HandsetOperation>();
	private ResponsibleUserInfoBean uBean;
	private  HomeAdapter adapter;
	public LiqueurSFYHomePresenter(Context context,HomeConstact.View homeView,Intent intent){
		this.context = context;
		this.homeView = homeView;
		CachePut cacheP = new CachePut();
		uBean = (ResponsibleUserInfoBean) intent.getSerializableExtra("userInfo");
		cacheP.putCacheStringG(context, Config.configSPName, Config.liqueurSFYLoginAccount, uBean.getAccount());
		start();
//		cacheP.putCacheStringG(context, Config.configSPName, Config.liqueurSFYLoginAccount, "15109282870");
	}
	
	@Override
	public void onNewIntent(Intent intent) {
		CachePut cacheP = new CachePut();
		uBean = (ResponsibleUserInfoBean) intent.getSerializableExtra("userInfo");
		cacheP.putCacheStringG(context, Config.configSPName, Config.liqueurSFYLoginAccount, uBean.getAccount());
		homeBeans = new ArrayList<>();
		initHomeEumnBeans();
		if (adapter != null) {
			adapter.refreshData(homeBeans);
		} else {
			adapter = new HomeAdapter(homeBeans);
			homeView.setAdapter(adapter);
		}
	}

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

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

	private void initHomeEumnBeans() {
		if (uBean.getUserType() == 1) {
			//仓库
			homeBeans.add(HandsetOperation.OPERATION_ACTIVATE_WITH_RELATION_CONSTANT);
			homeBeans.add(HandsetOperation.OPERATION_OUT_WAREHOUSE_CONSTANT);
		} 
		homeBeans.add(HandsetOperation.OPERATION_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:
			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);
			statusIntent.putExtra("userInfo", uBean);
			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);		
	}
	@Override
	public void logoutClick() {
		if(!checkViewAvailable()) return;
		homeView.showMsgToast("退出登录", Toast.LENGTH_SHORT);
		Intent intent = new Intent(context, LoginActivity.class);
		context.startActivity(intent);
		homeView.finishPage();
	}


}
