package com.yztiot.streetlamp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import java.util.Calendar;

public class TimingReceiver extends BroadcastReceiver {
    private boolean isSerialOpen = false;
    private SerialManage serialManage;

    @Override
    public void onReceive(Context context, Intent intent) {
        Calendar now = Calendar.getInstance();
        int currentHour = now.get(Calendar.HOUR_OF_DAY);
        int currentMinute = now.get(Calendar.MINUTE);

        SharedPreferences prefs = context.getSharedPreferences(MainActivity.PREFS_NAME, Context.MODE_PRIVATE);
        int startHour = prefs.getInt(MainActivity.START_HOUR_KEY, -1);
        int startMinute = prefs.getInt(MainActivity.START_MINUTE_KEY, -1);
        int endHour = prefs.getInt(MainActivity.END_HOUR_KEY, -1);
        int endMinute = prefs.getInt(MainActivity.END_MINUTE_KEY, -1);
        String frame = prefs.getString(MainActivity.FRAME_KEY,"");

        if ("com.yztiot.streetlamp.START_ALARM".equals(intent.getAction())) {
            if (startHour == currentHour && startMinute == currentMinute) {
                Log.d("TimingCheck", "开始时间到达: " + currentHour + ":" + currentMinute+"frame: "+frame);
                send(frame);
            }
        } else if ("com.yztiot.streetlamp.END_ALARM".equals(intent.getAction())) {
            if (endHour == currentHour && endMinute == currentMinute) {
                Log.d("TimingCheck", "结束时间到达: " + currentHour + ":" + currentMinute+"frame: "+frame);
                send("55010100020059");
            }
        }

        Log.d("TimingReceiver", "TimingReceiver triggered at: " + currentHour + ":" + currentMinute);
    }

    private void send(String data) {
        SerialManage.getInstance().init(new SerialInter() {
            @Override
            public void connectMsg(String path, boolean isSucc) {
                Log.d("SerialManage", "串口连接成功");
            }

            @Override
            public void readData(String path, byte[] bytes, int size) {
                String receivedText = new String(bytes, 0, size);
            }
        });

        // 在初始化后发送数据
        byte[] bytesToSend = hexStringToByteArray(data);
        String hexString = bytesToHex(bytesToSend);
        SerialManage.getInstance().send(hexString);
    }
    private String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }
    private byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}