package com.oo.eye.activity;

import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.app.baselibrary.base.common.BaseActivity;
import com.app.baselibrary.commonUtil.LogUtil;
import com.app.baselibrary.commonUtil.NibiruUtils;
import com.app.baselibrary.commonUtil.ToastUtil;
import com.oo.eye.EyeConfig;
import com.oo.eye.R;
import com.oo.eye.R2;
import com.oo.eye.bean.EyeChartsBean;
import com.oo.eye.bean.EyeHistroyBean;
import com.oo.eye.db.DbManager;
import com.oo.eye.mvp.PresenterFactory;
import com.oo.eye.mvp.contract.EyeMainContract;
import com.oo.seex.sys_state.widget.StateView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 视力测试
 */

public class EyeTestMainActivity extends BaseActivity implements EyeMainContract.View{

	private EyeMainContract.Presenter mPresenter;

	@BindView(R2.id.state_view)
	StateView mStateView;

	private boolean isRequestFinish = false;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_eye_test_main;
	}

	@Override
	protected void init() {
		mPresenter = PresenterFactory.createEyeMainPresenter(this);
		if(EyeConfig.getInstance().isNetData()){
			DbManager.getDaoMaster(this).newSession().deleteAll(EyeHistroyBean.class);
			DbManager.getDaoMaster(this).newSession().deleteAll(EyeChartsBean.class);
		}
		mPresenter.getEye(this);
	}

	protected void onResume() {
		super.onResume();
		NibiruUtils.switchVR(false);
		mStateView.registStateListener();
	}


	@Override
	protected void onPause() {
		super.onPause();
		mStateView.unRegistStateListener();
	}

	@OnClick(R2.id.lin_eye_test)
	void onClickTest(View v){
		startActivity(BeforeEyeTestActivity.class);
	}
	@OnClick(R2.id.tv_eye_test_histroy)
	void onClickHistroy(View v){
		EyeHistroyBean eyeTestBean = EyeConfig.getInstance().getCuurentBean();
		if(EyeConfig.getInstance().getCuurentBean() == null ||
				(eyeTestBean.getLeft_eye() == 0.0 && eyeTestBean.getRight_eye() == 0.0)){
			ToastUtil.showLongMessage("暂无数据，请先去测视");
			return;
		}
		startActivity(EyeTestHistroyActivity.class);
	}
	@OnClick(R2.id.lin_eye_test_setting)
	void onClickSetting(View v){
		startActivity(EyeSettingActivity.class);
	}
	@OnClick(R2.id.lin_eye_test_statistics)
	void onClickStatistics(View v){
		EyeHistroyBean eyeTestBean = EyeConfig.getInstance().getCuurentBean();
		if(EyeConfig.getInstance().getCuurentBean() == null ||
				(eyeTestBean.getLeft_eye() == 0.0 && eyeTestBean.getRight_eye() == 0.0)){
			ToastUtil.showLongMessage("暂无数据，请先去测视");
			return;
		}
		startActivity(EyeTestStatisticsActivity.class);
	}

	public void setPresenter(EyeMainContract.Presenter presenter){
		this.mPresenter = presenter;
	}

	public void getEyeSuccee(){
		isRequestFinish = true;
	}
	public void getEyeFail(String msg){
		isRequestFinish = false;
		ToastUtil.showLongMessage(msg);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		LogUtil.e("dispatchTouchEvent:"+ev.getAction());
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		LogUtil.e("keyCode:"+keyCode);
		return super.onKeyDown(keyCode, event);
	}
}
