package com.oo.eye.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.app.baselibrary.base.common.BaseActivity;
import com.app.baselibrary.commonUtil.CalculateUtils;
import com.app.baselibrary.commonUtil.NibiruUtils;
import com.app.baselibrary.commonUtil.PreferencesUtils;
import com.app.baselibrary.commonUtil.SnUtils;
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.Eye;
import com.oo.eye.bean.EyeHistroyBean;
import com.oo.eye.manager.DaemonSender;
import com.oo.eye.mvp.PresenterFactory;
import com.oo.eye.mvp.contract.EyeTestContract;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 */

public class AfterEyeTestActivity extends BaseActivity implements EyeTestContract.View {

    @BindView(R.id.tv_after_title)
    TextView mTvAfterTitle;
    @BindView(R.id.tv_after_name)
    TextView mTvAfterName;
    @BindView(R.id.tv_after_time)
    TextView mTvAfterTime;
    @BindView(R.id.tv_after_save)
    TextView mTvAfterSave;
    @BindView(R.id.tv_after_give_up)
    TextView mTvAfterGiveUp;
    @BindView(R.id.tv_after_result_left)
    TextView mTvAfterResultLeft;
    @BindView(R.id.tv_after_result_right)
    TextView mTvAfterResultRight;
    private EyeTestContract.Presenter mPresenter;

    private EyeHistroyBean mEyeTestBean;
    private double mLeftEye;
    private double mRightEye;
    private int mLeftLine;
    private int mRightLine;

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

    @Override
    protected void init() {
        PresenterFactory.createEyeTestPresenter(this);
        DaemonSender.closeEyeTest(this);
        mEyeTestBean = EyeConfig.getInstance().getCuurentBean();
        if (mEyeTestBean == null) {
            mEyeTestBean = new EyeHistroyBean();
            mEyeTestBean.setType(2);
        }
        mLeftEye = getIntent().getDoubleExtra("leftEye", 0);
        mRightEye = getIntent().getDoubleExtra("rightEye", 0);
        mLeftLine = getIntent().getIntExtra("leftLine", 0);
        mRightLine = getIntent().getIntExtra("rightLine", 0);
        if (mEyeTestBean != null && mEyeTestBean.getLeft_eye() > 0 && mEyeTestBean.getRight_eye() > 0) {
            double left = CalculateUtils.sub(mLeftEye, mEyeTestBean.getLeft_eye(), 1);
            double right = CalculateUtils.sub(mRightEye, mEyeTestBean.getRight_eye(), 1);
            int count = (int) CalculateUtils.add(CalculateUtils.mul(left, 10, 1),
                    CalculateUtils.mul(right, 10, 1), 1);
            String text = "少于1";
            if (count > 0) {
                text = count + "";
            }
        }
        mTvAfterName.setText(getString(R.string.after_eye_test_name, EyeConfig.getInstance().getName())
                + "(用户" + SnUtils.getSN() + ")");
        int selectId = PreferencesUtils.getInt(EyeConfig.EYE_SETTING_KEY, 0);
        String text = String.valueOf(mLeftEye) + " | " + String.valueOf(mRightEye);
        if (selectId == 1) {
            text = String.valueOf(Eye.transformSmall(mLeftEye)) + " | "
                    + String.valueOf(Eye.transformSmall(mRightEye));
        }
        mTvAfterResultLeft.setText(mLeftEye+"");
        mTvAfterResultRight.setText(mRightEye+"");
    }

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


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

    @OnClick(R2.id.tv_after_save)
    void onClickSave(View view) {
        mPresenter.postEyeTestData(this, SnUtils.getSN(),
                mLeftEye, mRightEye, mLeftLine, mRightLine);
    }

    @OnClick(R2.id.tv_after_give_up)
    void onClickGiveUp(View view) {
        finish();
    }

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

    public void postEyeTestDataSuccee() {
        mEyeTestBean.setLeft_eye(mLeftEye);
        mEyeTestBean.setRight_eye(mRightEye);
        mEyeTestBean.setLeft_line(mLeftLine);
        mEyeTestBean.setRight_line(mRightLine);
        EyeConfig.getInstance().setCuurentBean(mEyeTestBean);
        if (EyeConfig.getInstance().getInitBean() == null) {
            EyeConfig.getInstance().setInitBean(mEyeTestBean);
        }
        ToastUtil.showLongMessage("数据保存成功");
        finish();
    }

    public void postEyeTestDataFail(String msg) {
        ToastUtil.showLongMessage("数据保存失败,请重试");
    }

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