package com.oo.eye.activity;

import android.bluetooth.BluetoothAdapter;
import android.os.BatteryManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextClock;
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 com.oo.seex.sys_state.Config;
import com.oo.seex.sys_state.StateListener;
import com.oo.seex.sys_state.state.bean.BaseState;
import com.oo.seex.sys_state.state.bean.BatteryState;
import com.oo.seex.sys_state.state.bean.BluetoothState;
import com.oo.seex.sys_state.state.bean.WifiState;

import butterknife.BindView;
import butterknife.OnClick;

/**
 */

public class AfterEyeTestActivity extends BaseActivity implements StateListener, EyeTestContract.View{

	private EyeTestContract.Presenter mPresenter;

	@BindView(R2.id.system_time_tv)
	TextClock mSystemTimeTv;

	@BindView(R2.id.bluetooth_img)
	ImageView mBluetoothImg;

	@BindView(R2.id.wifi_img)
	ImageView mWifiImg;

	@BindView(R2.id.battery_img)
	ImageView mBatteryImg;

	@BindView(R2.id.battery_tv)
	TextView mBatteryTv;
	@BindView(R2.id.tv_after_lift)
	TextView mLiftTv;
	@BindView(R2.id.tv_after_name)
	TextView mNameTv;
	@BindView(R2.id.tv_after_result)
	TextView mResultTv;

	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+"";
			}
			mLiftTv.setText(getString(R.string.after_eye_test_prompt1, "" + text));
		}else{
			mLiftTv.setText(getString(R.string.after_eye_test_prompt2));
		}
		mNameTv.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));
		}
		mResultTv.setText(getString(R.string.after_eye_test_result,text));
	}
	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
	public void onSysStateChange(int flag, BaseState state) {
		switch (flag) {
			case Config.FLAG_BATTERY_STATE:
				BatteryState batteryState = (BatteryState) state;
				if (mBatteryTv != null)
					mBatteryTv.setText(batteryState.getLevel() + "%");
				if (batteryState.getStatus() == BatteryManager.BATTERY_STATUS_CHARGING) {
					mBatteryImg.setImageResource(R.mipmap.power_1);
				} else {
					if (batteryState.getLevel() < 20) {
						mBatteryImg.setImageResource(R.mipmap.power_4);

					} else if (batteryState.getLevel() < 80) {
						mBatteryImg.setImageResource(R.mipmap.power_3);

					} else {
						mBatteryImg.setImageResource(R.mipmap.power_2);
					}
				}
				break;
			case Config.FLAG_BLUETOOTH_STATE:
				BluetoothState bluetoothState = (BluetoothState) state;
				mBluetoothImg.setVisibility(View.VISIBLE);
				switch (bluetoothState.getState()) {
					case BluetoothAdapter.STATE_ON:
						mBluetoothImg.setImageResource(R.mipmap.bluetooth_1);
						break;
					case BluetoothAdapter.STATE_OFF:
						mBluetoothImg.setVisibility(View.GONE);
						break;
					case BluetoothAdapter.STATE_TURNING_OFF:
						mBluetoothImg.setImageResource(R.mipmap.bluetooth_2);
						break;
					case BluetoothAdapter.STATE_TURNING_ON:
						mBluetoothImg.setImageResource(R.mipmap.bluetooth_2);
						break;
				}
				break;
			case Config.FLAG_TIME_STATE:
				break;
			case Config.FLAG_WIFI_STATE:
				WifiState wifiState = (WifiState) state;
				mWifiImg.setImageResource(R.mipmap.wifi_4);
				mWifiImg.setVisibility(wifiState.isOpen() ? View.VISIBLE : View.GONE);

				if (wifiState.isOpen()) {
					if (!wifiState.isConnect()) {
						mWifiImg.setImageResource(R.mipmap.wifi_3);
					} else {
						switch (wifiState.getStrength()) {
							case 0:
							case 1:
							case 2:
								mWifiImg.setImageResource(R.mipmap.wifi_2);
								break;
							case 3:
							case 4:
								mWifiImg.setImageResource(R.mipmap.wifi_1);
								break;
						}
					}
				}
				break;
		}

	}
}
