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;
//		amPmList.add("AM");
//		amPmList.add("PM");

	@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> sixList = new ArrayList<>();
	private ArrayList<String> selectTime = new ArrayList<>();


	String selectHour;
	String selectMin;
	String selectSec;


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


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


	//设置底部的时间
	private void setDwTime() {

		Date time = new Date();
		int currHour = LocalDateUtils.getHour(time);
		int currMinute = LocalDateUtils.getSecond(time);
		int currSecond = LocalDateUtils.getMinute(time);


		String hour = wheelHour.getSeletedItem();
		String minute = wheelMinute.getSeletedItem();
		String second = wheelSecond.getSeletedItem();


		int selectHourTime;
		int selectMinTime;
		int selectSecTime;

		String selectHour_dw;
		String selectMin_dw;
		String selectSec_dw;

		//小时
		if (hour.startsWith("0")) {
			hour = hour.substring(1);
		}
		selectHourTime = Integer.parseInt(hour);

		int dowHour = selectHourTime - currHour;

		if (dowHour < 10) {
			selectHour_dw = "0" + dowHour;
		} else {
			selectHour_dw = String.valueOf(dowHour);
		}
		wheelHour_dw.setSeletion(selectTime.indexOf(selectHour_dw));


		//分钟
		if (minute.startsWith("0")) {
			minute = minute.substring(1);
		}
		selectMinTime = Integer.parseInt(minute);

		int dowMin = selectMinTime - currMinute;
		if (dowMin < 10) {
			selectMin_dw = "0" + dowMin;
		} else {
			selectMin_dw = String.valueOf(dowMin);
		}

		wheelMinute_dw.setSeletion(sixList.indexOf(selectMin_dw));


		//秒
		if (second.startsWith("0")) {
			second = second.substring(1);
		}
		selectSecTime = Integer.parseInt(second);

		int dowSec = selectSecTime - currSecond;
		if (dowSec < 10) {
			selectSec_dw = "0" + dowSec;
		} else {
			selectSec_dw = String.valueOf(dowSec);
		}

		wheelSecond_dw.setSeletion(sixList.indexOf(selectSec_dw));

	}


	//设置顶部的时间
	private void setUpTime() {
		String hour = wheelHour.getSeletedItem();
		String minute = wheelMinute.getSeletedItem();
		String second = wheelSecond.getSeletedItem();

		String dwHour = wheelHour_dw.getSeletedItem();
		String dwMinute = wheelMinute_dw.getSeletedItem();
		String dwSecond = wheelSecond_dw.getSeletedItem();

		int selectHourTime;
		int selectMinTime;
		int selectSecTime;

		int dwHourTime;
		int dwMinTime;
		int dwSecTime;


		if (hour.startsWith("0")) {
			hour = hour.substring(1);
		}
		selectHourTime = Integer.parseInt(hour);

		if (dwHour.startsWith("0")) {
			dwHour = dwHour.substring(1);
		}
		dwHourTime = Integer.parseInt(dwHour);

		selectHourTime = selectHourTime + dwHourTime;
		if (selectHourTime < 10) {
			hour = "0" + selectHourTime;
		} else {
			hour = String.valueOf(selectHourTime);
		}

		wheelHour.setSeletion(hourList.indexOf(hour));


		//分钟
		if (minute.startsWith("0")) {
			minute = minute.substring(1);
		}
		selectMinTime = Integer.parseInt(minute);

		if (dwMinute.startsWith("0")) {
			dwMinute = dwMinute.substring(1);
		}
		dwMinTime = Integer.parseInt(dwMinute);


		selectMinTime = selectMinTime + dwMinTime;
		if (selectMinTime < 10) {
			minute = "0" + selectMinTime;
		} else {
			minute = String.valueOf(selectMinTime);
		}

		wheelMinute.setSeletion(sixList.indexOf(minute));



		//秒
		if (second.startsWith("0")) {
			second = second.substring(1);
		}
		selectSecTime = Integer.parseInt(second);

		if (dwSecond.startsWith("0")) {
			dwSecond = dwSecond.substring(1);
		}
		dwSecTime = Integer.parseInt(dwSecond);


		selectSecTime = selectSecTime + dwSecTime;
		if (selectSecTime < 10) {
			second = "0" + selectSecTime;
		} else {
			second = String.valueOf(selectSecTime);
		}

		wheelSecond.setSeletion(sixList.indexOf(second));


	}

	@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;
				}
				setDwTime();

			}
		});


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

			}
		});


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

				setDwTime();

			}
		});


		wheelHour_dw.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				setUpTime();
			}
		});


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


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

	}

	@Override
	public void initData() {
		Date time = new Date();
		int hour = LocalDateUtils.getHour(time);
		int minute = LocalDateUtils.getSecond(time);
		int second = LocalDateUtils.getMinute(time);


		hourList.clear();
		sixList.clear();

		hourList.add("00");

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

		for (int i = 1; i <= 16; i++) {
			if (i < 10) {
				selectTime.add("0" + i);
			} else {
				selectTime.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(sixList);// 设置数据源
		if (minute < 10) {
			selectMin = "0" + minute;
		} else {
			selectMin = String.valueOf(minute);
		}
		int indexMinute = sixList.indexOf(selectMin);
		wheelMinute.setSeletion(indexMinute);// 默认选中第三项

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

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


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

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

		///秒////
		wheelSecond_dw.setOffset(3);
		wheelSecond_dw.setItems(sixList);
		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;
				}
				String selectHour_dw = wheelHour_dw.getSeletedItem();
				String selectMin_dw = wheelMinute_dw.getSeletedItem();
				String selectSec_dw = wheelSecond_dw.getSeletedItem();

				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 * 3600 + 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;
		}
	}
}