package com.example.blu.toys.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.clj.fastble.callback.BleWriteCallback;
import com.clj.fastble.exception.BleException;
import com.example.blu.toys.R;
import com.example.blu.toys.ble.BlePlay;
import com.example.blu.toys.ble.agreement.TrafficLightBean;
import com.example.blu.toys.utils.SpUtils;
import com.example.blu.toys.view.TypefaceTextView;
import com.example.blu.toys.view.WheelView;
import com.hjq.toast.ToastUtils;

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

public class PlayActivity extends BaseActivity {


	@BindView(R.id.task_time)
	TypefaceTextView typefaceTextView;//倒计时

	private int mDay = 0;
	private int mHour = 0;
	private int mMin = 0;
	private int mSecond = 00;// 天 ,小时,分钟,秒
	private boolean isRun = true;


	private Handler timeHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {
				computeTime();
				typefaceTextView.setText(mHour + ":" + mHour + ":" + mSecond);
			}
		}
	};


	/**
	 * 开启倒计时
	 */
	private void startRun() {
		new Thread(() -> {
			while (isRun) {
				try {
					Thread.sleep(1000); // sleep 1000ms
					Message message = Message.obtain();
					message.what = 1;
					timeHandler.sendMessage(message);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	/**
	 * 倒计时计算
	 */
	private void computeTime() {
		mSecond--;
		if (mSecond < 0) {
			mMin--;
			mSecond = 59;
			if (mMin < 0) {
				mMin = 59;
				mHour--;
				if (mHour < 0) {
					// 倒计时结束
					mHour = 23;
					mDay--;
				}
			}
		}
	}


	@Override
	public int getLayout() {
		return R.layout.activity_play;
	}

	@Override
	public void init(Bundle savedInstanceState) {
		Intent intent = getIntent();
		mHour = intent.getIntExtra("hour", 0);
		mMin = intent.getIntExtra("min", 0);
		mSecond = intent.getIntExtra("second", 0);
		startRun();
	}

	@Override
	public void initData() {

	}


	@OnClick({R.id.cancel_but, R.id.pause_but, R.id.resume_but})
	public void onViewClicked(View view) {
		SpUtils spUtils = SpUtils.getSpUtils(PlayActivity.this);
		int close = spUtils.getSPValue("close", 1);
		int open = spUtils.getSPValue("open", 10);
		int alarm = spUtils.getSPValue("alarm", 1);
		int green = spUtils.getSPValue("green", 1);
		int brightness = spUtils.getSPValue("brightness", 50);

		switch (view.getId()) {
			case R.id.cancel_but://取消 关机



				TrafficLightBean trafficLightBean = new TrafficLightBean(
						1, 0,
						0, 0, 0, 0, 0, 0);
				byte[] bytes = trafficLightBean.toByte();
				BlePlay.getInstance().sendStard(bytes, new BleWriteCallback() {
					@Override
					public void onWriteSuccess(int current, int total, byte[] justWrite) {
						ToastUtils.show("关机-发送成功");
						timeHandler.removeMessages(1);

					}

					@Override
					public void onWriteFailure(BleException exception) {

					}
				});


			case R.id.pause_but://暂停
				int time = mHour * 360 + mMin * 60 + mSecond;
				TrafficLightBean ttt = new TrafficLightBean(2, 0,
						time, brightness, open, close, green, alarm);
				byte[] bytes12 = ttt.toByte();
				BlePlay.getInstance().sendPause(bytes12, new BleWriteCallback() {
					@Override
					public void onWriteSuccess(int current, int total, byte[] justWrite) {
						ToastUtils.show("暂停-发送成功");
						timeHandler.removeMessages(1);
					}

					@Override
					public void onWriteFailure(BleException exception) {

					}
				});

			case R.id.resume_but://开始
				int hh = mHour * 360 + mMin * 60 + mSecond;
				TrafficLightBean tt = new TrafficLightBean(2, 1,
						hh, brightness, open, close, green, alarm);
				byte[] byte11 = tt.toByte();
				BlePlay.getInstance().sendStard(byte11, new BleWriteCallback() {
					@Override
					public void onWriteSuccess(int current, int total, byte[] justWrite) {
						startRun();
						ToastUtils.show("开始-发送成功");
					}

					@Override
					public void onWriteFailure(BleException exception) {

					}
				});

				break;
		}
	}
}