package com.oo.eye.mvp.presenter;

import android.content.Context;

import com.oo.eye.EyeConfig;
import com.oo.eye.mvp.contract.EyeMainContract;

/**
 * Created by xucon on 2017/12/27.
 */

public class EyeMainPresenter implements EyeMainContract.Presenter {

	private EyeMainContract.View mView;

	public EyeMainPresenter(EyeMainContract.View view){
		this.mView = view;
	}

	public void getEye(Context context){
		if(EyeConfig.getInstance().isNetData()){
//			getEyeNet();
		}else {
			getEyeDB(context);
		}
	}

	private void getEyeNet(){
//		EyeDataManager.getInstance().getEyeInit(SnUtils.getSN())
//			.subscribe(new RxCallback<NewResponseImpl<InitDataBean>>() {
//			@Override
//			public void onSuccess(NewResponseImpl<InitDataBean> response) {
//					InitDataBean initDataBean = response.getResultData();
//				if(initDataBean != null){
//					if(initDataBean.getInitializedEye() != null){
//						EyeHistroyBean initBean = initDataBean.getInitializedEye();
//						if(initBean != null && initBean.getLeft_eye() != 0 && initBean.getRight_eye() != 0){
//							EyeConfig.getInstance().setInitBean(initBean);
//						}
//					}
//					if(initDataBean.getCurrentEye() != null){
//						EyeHistroyBean currentBean = initDataBean.getCurrentEye();
//						if(currentBean != null && currentBean.getLeft_eye() != 0 && currentBean.getRight_eye() != 0){
//							EyeConfig.getInstance().setInitBean(currentBean);
//						}
//					}
//				}
//				if (mView != null && mView.isActive())
//				mView.getEyeSuccee();
//			}
//
//			@Override
//			public void onError(int errorCode, String errorMessage) {
//				mView.getEyeFail(errorMessage);
//			}
//		});
	}

	private void getEyeDB(Context context){
		try {
//			QueryBuilder<EyeHistroyBean> builder = DbManager.getDaoMaster(context).newSession().getEyeHistroyBeanDao().queryBuilder();
//			List<EyeHistroyBean> descBeans = builder.orderDesc(EyeHistroyBeanDao.Properties.Created_time).build().list();
//			if (descBeans != null && descBeans.size() > 0) {
//				EyeConfig.getInstance().setCuurentBean(descBeans.get(0));
//				EyeConfig.getInstance().setInitBean(descBeans.get(descBeans.size() - 1));
//			}
//			EyeConfig.getInstance().setNetData(false);
//			if (mView != null && mView.isActive()) {
//				mView.getEyeSuccee();
//			}
		}catch (Exception e){
			e.printStackTrace();
			if (mView != null && mView.isActive())
			mView.getEyeFail("数据库加载失败");
		}
	}
}
