package com.oo.eye.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

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.ButterKnife;
import butterknife.OnClick;

/**
 * 视力测试
 */

public class EyeTestMainActivity extends BaseActivity implements EyeMainContract.View {

    @BindView(R.id.test_setting)
    LinearLayout mTestSetting;
    @BindView(R.id.lin_eye_test)
    LinearLayout mLinEyeTest;
    @BindView(R.id.tv_eye_test_histroy)
    TextView mTvEyeTestHistroy;
    @BindView(R.id.lin_eye_test_histroy)
    LinearLayout mLinEyeTestHistroy;
    @BindView(R.id.lin_eye_test_statistics)
    LinearLayout mLinEyeTestStatistics;
    @BindView(R.id.lin_eye_test_setting)
    LinearLayout mLinEyeTestSetting;
    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();
    }

    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);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.test_setting, R.id.lin_eye_test, R.id.lin_eye_test_histroy, R.id.lin_eye_test_statistics, R.id.lin_eye_test_setting})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.test_setting:
                startActivity(EyeSettingActivity.class);
                break;
            case R.id.lin_eye_test://单人测试
                startActivity(SinglePeopleTestActivity.class);
                break;
            case R.id.lin_eye_test_histroy://多人测试
                startActivity(MorePeopleTestActivity.class);
                break;
            case R.id.lin_eye_test_statistics:
                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);
                break;
            case R.id.lin_eye_test_setting://测试报表
                startActivity(TestResurtListActivity.class);
                break;
        }
    }
}
