package com.oo.eye.activity;

import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.app.baselibrary.base.common.BaseActivity;
import com.app.baselibrary.commonUtil.LogUtil;
import com.app.baselibrary.commonUtil.PreferencesUtils;
import com.oo.eye.EyeConfig;
import com.oo.eye.R;

import butterknife.BindView;
import butterknife.OnClick;

import static com.oo.eye.EyeConfig.EYE_DIRECTION_4;
import static com.oo.eye.EyeConfig.EYE_DIRECTION_8;

/**
 */

public class EyeSettingActivity extends BaseActivity {

    @BindView(R.id.group_decimal)
    RadioGroup mGroupDecimal;
    @BindView(R.id.group_distance)
    RadioGroup mGroupDistance;
    @BindView(R.id.group_direction)
    RadioGroup mGroupDirection;
    @BindView(R.id.group_wait)
    RadioGroup mGroupWait;
    @BindView(R.id.group_report)
    RadioGroup mGroupReport;
    @BindView(R.id.group_help)
    RadioGroup mGroupHelp;
    @BindView(R.id.about_lay_show)
    RelativeLayout mAboutLayShow;
    @BindView(R.id.checkbox_decimal)
    RadioButton mCheckboxDecimal;
    @BindView(R.id.checkbox_number5)
    RadioButton mCheckboxNumber5;
    @BindView(R.id.checkbox_distance5)
    RadioButton mCheckboxDistance5;
    @BindView(R.id.checkbox_distance2)
    RadioButton mCheckboxDistance2;
    @BindView(R.id.checkbox_direction4)
    RadioButton mCheckboxDirection4;
    @BindView(R.id.checkbox_direction8)
    RadioButton mCheckboxDirection8;
    @BindView(R.id.checkbox_wait_3)
    RadioButton mCheckboxWait3;
    @BindView(R.id.checkbox_wait_5)
    RadioButton mCheckboxWait5;
    @BindView(R.id.checkbox_wait_8)
    RadioButton mCheckboxWait8;
    @BindView(R.id.checkbox_wait_10)
    RadioButton mCheckboxWait10;
    @BindView(R.id.checkbox_wait_no)
    RadioButton mCheckboxWaitNo;
    @BindView(R.id.checkbox_report)
    RadioButton mCheckboxReport;
    @BindView(R.id.checkbox_report_print)
    RadioButton mCheckboxReportPrint;
    @BindView(R.id.checkbox_help)
    RadioButton mCheckboxHelp;
    @BindView(R.id.checkbox_close_help)
    RadioButton mCheckboxCloseHelp;

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

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


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

    @Override
    protected void init() {
        int selectId = PreferencesUtils.getInt(EyeConfig.EYE_SETTING_KEY, EyeConfig.EYE_SETTING_5);
        switch (selectId) {
            case EyeConfig.EYE_SETTING_1:
                mCheckboxDecimal.setChecked(true);
                break;
            case EyeConfig.EYE_SETTING_5:
                mCheckboxNumber5.setChecked(true);
                break;
        }
        //几分之
        mGroupDecimal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.checkbox_decimal:
                        PreferencesUtils.putInt(EyeConfig.EYE_SETTING_KEY, EyeConfig.EYE_SETTING_1);
                        break;
                    case R.id.checkbox_number5:
                        PreferencesUtils.putInt(EyeConfig.EYE_SETTING_KEY, EyeConfig.EYE_SETTING_5);
                        break;
                }
            }
        });
        //测试距离
        int distance = PreferencesUtils.getInt(EyeConfig.EYE_DISTANCE_SETTING_KEY, EyeConfig.EYE_DISTANCE_5);
        switch (distance) {
            case EyeConfig.EYE_DISTANCE_2:
                mCheckboxDistance2.setChecked(true);
                break;
            case EyeConfig.EYE_DISTANCE_5:
                mCheckboxDistance5.setChecked(true);
                break;

        }
        mGroupDistance.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.checkbox_distance2:
                        PreferencesUtils.putInt(EyeConfig.EYE_DISTANCE_SETTING_KEY, EyeConfig.EYE_DISTANCE_2);
                        break;
                    case R.id.checkbox_distance5:
                        PreferencesUtils.putInt(EyeConfig.EYE_DISTANCE_SETTING_KEY, EyeConfig.EYE_DISTANCE_5);
                        break;
                }
            }
        });
        //C字视标
        int direction = PreferencesUtils.getInt(EyeConfig.EYE_DIRECTION_SETTING_KEY, EYE_DIRECTION_4);
        switch (direction) {
            case EYE_DIRECTION_4:
                mCheckboxDirection4.setChecked(true);
                break;
            case EYE_DIRECTION_8:
                mCheckboxDirection8.setChecked(true);
                break;

        }
        mGroupDirection.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.checkbox_direction4:
                        PreferencesUtils.putInt(EyeConfig.EYE_DIRECTION_SETTING_KEY, EYE_DIRECTION_4);
                        break;
                    case R.id.checkbox_direction8:
                        PreferencesUtils.putInt(EyeConfig.EYE_DIRECTION_SETTING_KEY, EYE_DIRECTION_8);
                        break;
                }
            }
        });
        //测视等待时长
        int wait = PreferencesUtils.getInt(EyeConfig.EYE_WAIT_SETTING_KEY, 0);
        switch (wait) {
            case 3:
                mCheckboxWait3.setChecked(true);
                break;
            case 5:
                mCheckboxWait5.setChecked(true);
                break;
            case 8:
                mCheckboxWait8.setChecked(true);
                break;
            case 10:
                mCheckboxWait10.setChecked(true);
                break;
            case 0:
                mCheckboxWaitNo.setChecked(true);
                break;

        }
        mGroupWait.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.checkbox_wait_3:
                        PreferencesUtils.putInt(EyeConfig.EYE_WAIT_SETTING_KEY, 3);
                        break;
                    case R.id.checkbox_wait_5:
                        PreferencesUtils.putInt(EyeConfig.EYE_WAIT_SETTING_KEY, 5);
                        break;
                    case R.id.checkbox_wait_8:
                        PreferencesUtils.putInt(EyeConfig.EYE_WAIT_SETTING_KEY, 8);
                        break;
                    case R.id.checkbox_wait_10:
                        PreferencesUtils.putInt(EyeConfig.EYE_WAIT_SETTING_KEY, 10);
                        break;
                    case R.id.checkbox_wait_no:
                        PreferencesUtils.putInt(EyeConfig.EYE_WAIT_SETTING_KEY, 0);
                        break;
                }
            }
        });
        //检视报告
        int report = PreferencesUtils.getInt(EyeConfig.EYE_REPORT_SETTING_KEY, 0);
        switch (report) {
            case 0:
                mCheckboxReport.setChecked(true);
                break;
            case 1:
                mCheckboxReportPrint.setChecked(true);
                break;
        }
        mGroupReport.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.checkbox_report:
                        PreferencesUtils.putInt(EyeConfig.EYE_REPORT_SETTING_KEY, 0);
                        break;
                    case R.id.checkbox_report_print:
                        PreferencesUtils.putInt(EyeConfig.EYE_REPORT_SETTING_KEY, 1);
                        break;
                }
            }
        });
        //测视帮助
        boolean help = PreferencesUtils.getBoolean(EyeConfig.EYE_HELP_SETTING_KEY, true);
        if (help) {
            mCheckboxHelp.setChecked(true);
        } else {
            mCheckboxCloseHelp.setChecked(true);
        }
        mGroupHelp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.checkbox_help:
                        PreferencesUtils.putBoolean(EyeConfig.EYE_HELP_SETTING_KEY, true);
                        break;
                    case R.id.checkbox_close_help:
                        PreferencesUtils.putBoolean(EyeConfig.EYE_HELP_SETTING_KEY, false);
                        break;
                }
            }
        });
    }

    @OnClick({R.id.back_iv, R.id.about_lay, R.id.close_lay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.about_lay:
                mAboutLayShow.setVisibility(View.VISIBLE);
                break;
            case R.id.close_lay:
                mAboutLayShow.setVisibility(View.INVISIBLE);
                break;
        }
    }

    private long mLastOnKeyTime = -1;

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            //两次点击大于1秒
            long currentTime = System.currentTimeMillis();
            if (mLastOnKeyTime > 0 && currentTime - mLastOnKeyTime < 500) {
                return true;
            }
            mLastOnKeyTime = currentTime;
            LogUtil.e("---查看按键对应的数字---" + event.getKeyCode());
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_DEL://返回键
                    finish();
                    return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }
}
