package com.oo.eye.activity;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.app.baselibrary.base.common.BaseActivity;
import com.app.baselibrary.commonUtil.BDttsUtils;
import com.app.baselibrary.commonUtil.LogUtil;
import com.app.baselibrary.commonUtil.ToastUtil;
import com.oo.eye.EyeConfig;
import com.oo.eye.R;
import com.oo.eye.bean.StudentBean;
import com.oo.eye.db.DbManager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 视力测试
 */

public class EyeTestMainActivity extends BaseActivity {

    @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 SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    private boolean isRequestFinish = false;

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

    @Override
    protected void init() {
        if (BDttsUtils.getInstance()!=null) {
            BDttsUtils.getInstance().speak("");
        }
    }

    protected void onResume() {
        super.onResume();
    }


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


    public void getEyeSuccee() {
        isRequestFinish = true;
    }

    public void getEyeFail(String msg) {
        isRequestFinish = false;
        ToastUtil.showLongMessage(msg);
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            LogUtil.e("---查看按键对应的数字---" + event.getKeyCode());
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_DEL://返回键
                    exitBy2Click();
                    return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }
    /**
     * 双击退出函数
     */

    private static Boolean isExit = false;
    private Timer tExit;
    private void exitBy2Click() {

        if (isExit == false) {
            isExit = true; // 准备退出
            ToastUtil.showMessage("再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            finish();
            System.exit(0);
        }
    }
//
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        LogUtil.e("dispatchTouchEvent:" + ev.getAction());
//        return super.dispatchTouchEvent(ev);
//    }

    @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:
                long count = DbManager.getDaoSession(EyeTestMainActivity.this).getStudentBeanDao().count();
                ArrayList<StudentBean> list = new ArrayList<>();
                StudentBean student = new StudentBean();
                student.setRealname("游客"+(count+1));
                student.setClassname(mSimpleDateFormat.format(new Date()));
                list.add(student);
                EyeConfig.getInstance().setStudents(list);
                Intent intent = new Intent(EyeTestMainActivity.this,EyeTestActivity.class);
                intent.putExtra("isTemporary",true);
                startActivity(intent);
                break;
            case R.id.lin_eye_test_setting://测试报表
                startActivity(TestResurtListActivity.class);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (tExit != null) {
            tExit.cancel();
            tExit = null;
        }
    }
}
