package com.example.blu.toys.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.clj.fastble.callback.BleWriteCallback;
import com.clj.fastble.data.BleDevice;
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.LocalDateUtils;
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 org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class SetTimeActivity extends BaseActivity {


	@BindView(R.id.wheel_hour)
	WheelView wheelHour;
	@BindView(R.id.wheel_minute)
	WheelView wheelMinute;
	@BindView(R.id.wheel_second)
	WheelView wheelSecond;
	@BindView(R.id.wheel_am_pm)
	WheelView wheelAmPm;

	@BindView(R.id.wheel_hour_dw)
	WheelView wheelHour_dw;
	@BindView(R.id.wheel_minute_dw)
	WheelView wheelMinute_dw;
	@BindView(R.id.wheel_second_dw)
	WheelView wheelSecond_dw;
	@BindView(R.id.back_but)
	TypefaceTextView backBut;
	@BindView(R.id.center)
	LinearLayout center;
	@BindView(R.id.start_but)
	RelativeLayout startBut;
	@BindView(R.id.will)
	TypefaceTextView will;

	private ArrayList<String> hourList = new ArrayList<>();
	private ArrayList<String> minuteList = new ArrayList<>();
	private ArrayList<String> secondList = new ArrayList<>();
	private ArrayList<String> amPmList = new ArrayList<>();

	private int amOrPm = 0;


	String selectHour;
	String selectMin;
	String selectSec;

	String selectHour_dw;
	String selectMin_dw;
	String selectSec_dw;

	int hour = 0;
	int min = 0;
	int second = 0;


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

	@Override
	public void init(Bundle savedInstanceState) {

		wheelHour.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				if (StringUtils.isEmpty(item)) {
					//不能动
					int indexHour = hourList.indexOf(selectHour);
					wheelHour.setSeletion(indexHour);
					return;
				}
				Date time = new Date();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(time);
				int nowHour = calendar.get(Calendar.HOUR);//时（12小时制）

				if (item.startsWith("0")) {
					item = item.substring(1);
				}

				//获取选择的时间
				Integer selectHourTime = Integer.valueOf(item);
				if (selectHourTime < nowHour) {
					int indexHour = hourList.indexOf(selectHour);
					wheelHour.setSeletion(indexHour);
					ToastUtils.show("选择的时间不能小于当前时间");
					return;
				}

//				selectHour = item;
//				//下面的小时 显示
//				int dowHour = selectHourTime - nowHour;
//				if (dowHour < 10) {
//					selectHour_dw = "0" + dowHour;
//				} else {
//					selectHour_dw = String.valueOf(dowHour);
//				}
//				wheelHour_dw.setSeletion(hourList.indexOf(selectHour_dw));

			}
		});


		wheelMinute.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				if (StringUtils.isEmpty(item)) {
					int indexMinute = minuteList.indexOf(selectMin);
					wheelMinute.setSeletion(indexMinute);
					return;
				}

				Calendar calendar = Calendar.getInstance();
				int nowHour = calendar.get(Calendar.HOUR);//时（12小时制）
				int nowMin = calendar.get(Calendar.MINUTE);


				String currentSelectHour;
				if (selectHour.startsWith("0")) {
					currentSelectHour = item.substring(1);
				} else {
					currentSelectHour = selectHour;
				}

				Integer selectHourTime = Integer.valueOf(currentSelectHour);

				if (selectHourTime < nowHour) {
					ToastUtils.show("选择的时间不能小于当前时间");
					return;
				}


				if (selectHourTime > nowHour) {
					selectMin = item;
				}


				if (selectHourTime == nowHour) {
					if (item.startsWith("0")) {
						item = item.substring(1);
					}
					Integer selectMinTime = Integer.valueOf(item);
					if (selectMinTime < nowMin) {
						int indexMinute = minuteList.indexOf(selectMin);
						wheelMinute.setSeletion(indexMinute);
						ToastUtils.show("选择的时间不能小于当前时间");
					} else {
						selectMin = item;
					}
				}


				if (selectMin.startsWith("0")) {
					item = item.substring(1);
				}

				Integer selectMinTime = Integer.valueOf(item);

				//设置下面的时间
//				int dowMin = selectMinTime - nowMin;
//				if (dowMin < 10) {
//					selectMin_dw = "0" + dowMin;
//				} else {
//					selectMin_dw = String.valueOf(dowMin);
//				}
//
//				wheelMinute_dw.setSeletion(minuteList.indexOf(selectMin_dw));
			}
		});


		//秒的选择
		wheelSecond.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				if (StringUtils.isEmpty(item)) {
					int indexSecond = secondList.indexOf(selectSec);
					wheelSecond.setSeletion(indexSecond);
					return;
				}

				Date time = new Date();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(time);
				int nowHour = calendar.get(Calendar.HOUR);//时（12小时制）
				int nowMinute = calendar.get(Calendar.MINUTE);//分
				int nowSecond = calendar.get(Calendar.SECOND);

				String currentHour;
				if (selectHour.startsWith("0")) {
					currentHour = item.substring(1);
				} else {
					currentHour = selectHour;
				}
				Integer currentSelectHour = Integer.valueOf(currentHour);

				String currentMin;

				if (selectMin.startsWith("0")) {
					currentMin = item.substring(1);
				} else {
					currentMin = selectMin;
				}
				Integer currentSelectMin = Integer.valueOf(currentMin);


				//当小时大于当前的时间
				if (currentSelectHour >= nowHour) {


					if (currentSelectMin > nowMinute) {
						selectSec = item;
					}


					if (currentSelectMin == nowMinute) {

						String sec;
						if (item.startsWith("0")) {
							sec = item.substring(1);
						} else {
							sec = item;
						}
						Integer selectSecTime = Integer.valueOf(sec);

						//计算秒
						if (selectSecTime < nowSecond) {
							int indexSec = secondList.indexOf(selectMin);
							wheelSecond.setSeletion(indexSec);
							ToastUtils.show("选择的时间不能小于当前时间");
						} else {
							selectMin = item;
						}
//
//						int dowSec = selectSecTime - nowSecond;
//						if (dowSec < 10) {
//							selectSec_dw = "0" + dowSec;
//						} else {
//							selectSec_dw = String.valueOf(dowSec);
//						}
//
//						wheelSecond_dw.setSeletion(secondList.indexOf(selectSec_dw));
					}

				} else {
					ToastUtils.show("选择的时间不能小于当前时间");
				}

			}
		});


		wheelHour_dw.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				selectHour_dw = hourList.get(selectedIndex);

			}
		});


		wheelMinute_dw.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				selectMin_dw = minuteList.get(selectedIndex);
			}
		});


		wheelSecond_dw.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				selectSec_dw = secondList.get(selectedIndex);
			}
		});


	}

	@Override
	public void initData() {
		Date time = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		int hour = calendar.get(Calendar.HOUR);//时（12小时制）
		int minute = calendar.get(Calendar.MINUTE);//分
		int second = calendar.get(Calendar.SECOND);

		int currTime = LocalDateUtils.getHour(time);
		if (currTime > 12) {
			amOrPm = 1;
		} else {
			amOrPm = 0;
		}

		hourList.clear();
		minuteList.clear();
		secondList.clear();

		amPmList.add("AM");
		amPmList.add("PM");

		hourList.add("00");

		for (int i = 1; i <= 12; i++) {
			if (i < 10) {
				hourList.add("0" + i);
			} else {
				hourList.add(String.valueOf(i));

			}
		}
		for (int i = 0; i <= 59; i++) {
			if (i < 10) {
				minuteList.add("0" + i);
				secondList.add("0" + i);
			} else {
				minuteList.add(String.valueOf(i));
				secondList.add(String.valueOf(i));
			}


		}

		///时////
		wheelHour.setOffset(3);
		wheelHour.setItems(hourList);
		if (hour < 10) {
			selectHour = "0" + hour;
		} else {
			selectHour = String.valueOf(hour);
		}
		int indexHour = hourList.indexOf(selectHour);
		wheelHour.setSeletion(indexHour);

		///分////
		wheelMinute.setOffset(3);// 对话框中当前项上面和下面的项数
		wheelMinute.setItems(minuteList);// 设置数据源
		if (minute < 10) {
			selectMin = "0" + minute;
		} else {
			selectMin = String.valueOf(minute);
		}
		int indexMinute = minuteList.indexOf(selectMin);
		wheelMinute.setSeletion(indexMinute);// 默认选中第三项


		///秒////
		wheelSecond.setOffset(3);
		wheelSecond.setItems(secondList);// 设置数据源
		if (second < 10) {
			selectSec = "0" + second;
		} else {
			selectSec = String.valueOf(second);
		}

		int indexSecond = secondList.indexOf(selectSec);
		wheelSecond.setSeletion(indexSecond);

		wheelAmPm.setOffset(2);// 对话框中当前项上面和下面的项数
		wheelAmPm.setItems(amPmList);// 设置数据源
		wheelAmPm.setSeletion(amOrPm);// 默认选中第三项


		/////////下////////
		///时////
		wheelHour_dw.setOffset(3);
		wheelHour_dw.setItems(hourList);
		wheelHour_dw.setSeletion(0);

		///分////
		wheelMinute_dw.setOffset(3);// 对话框中当前项上面和下面的项数
		wheelMinute_dw.setItems(minuteList);// 设置数据源
		wheelMinute_dw.setSeletion(0);// 默认选中第三项

		///秒////
		wheelSecond_dw.setOffset(3);
		wheelSecond_dw.setItems(secondList);
		wheelSecond_dw.setSeletion(0);

	}


	@Subscribe(threadMode = ThreadMode.MAIN)
	public void setBleDevice(BleDevice ble) {
		LogUtils.e("连接 成功-----开机");
	}

	@OnClick({R.id.back_but, R.id.start_but})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.back_but:
				BlePlay.getInstance().closeBle();
				finish();
				break;
			case R.id.start_but:
				boolean bleOpen = BlePlay.getInstance().isBleOpen();
				if (!bleOpen) {
					ToastUtils.show("请打开蓝牙");
					return;
				}
				if (TextUtils.isEmpty(selectHour_dw)) {
					hour = 0;
				} else if (selectHour_dw.startsWith("0")) {
					selectHour_dw = selectHour_dw.substring(1);
					hour = Integer.valueOf(selectHour_dw);
				}

				if (TextUtils.isEmpty(selectMin_dw)) {
					min = 0;
				} else if (selectMin_dw.startsWith("0")) {
					selectMin_dw = selectMin_dw.substring(1);
					min = Integer.valueOf(selectMin_dw);
				}

				if (TextUtils.isEmpty(selectSec_dw)) {
					second = 0;
				} else if (selectSec_dw.startsWith("0")) {
					selectSec_dw = selectSec_dw.substring(1);
					second = Integer.valueOf(selectSec_dw);
				}


				//转换成秒 0000000000000000
				int time = hour * 360 + min * 60 + second;
				SpUtils spUtils = SpUtils.getSpUtils(SetTimeActivity.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);

				TrafficLightBean trafficLightBean = new TrafficLightBean(
						2, 1,
						time, brightness, open, close, green, alarm);

				byte[] bytes = trafficLightBean.toByte();
				BlePlay.getInstance().sendStard(bytes, new BleWriteCallback() {
					@Override
					public void onWriteSuccess(int current, int total, byte[] justWrite) {
						Intent intent = new Intent(SetTimeActivity.this, PlayActivity.class);
						intent.putExtra("hour", hour);
						intent.putExtra("min", min);
						intent.putExtra("second", second);
						startActivity(intent);
					}

					@Override
					public void onWriteFailure(BleException exception) {

					}
				});
				break;
		}
	}
}