package com.usky.ai.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class AlgScheduleFetchResponseDTO {
    @JsonProperty("BoardIp")
    private String boardIp;

    @JsonProperty("BoardId")
    private String boardId;

    @JsonProperty("Event")
    private String event = "/alg_schedule_fetch";

    @JsonProperty("Result")
    private Result result;

    @JsonProperty("Content")
    private List<Schedule> content;

    public boolean isSuccess() {
        return result != null && result.getCode() == 0;
    }

    private long time = System.currentTimeMillis();

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Result {
        @JsonProperty("Code")  //0成功/其他失败
        private int code;

        @JsonProperty("Content")
        private List<Schedule> content; // Content 属性在 Result 中

        @JsonProperty("Desc")
        private String desc;
    }

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Schedule {
        @JsonProperty("Id")
        private int id;

        @JsonProperty("Name")
        private String name;

        @JsonProperty("Summary")
        private String summary;

        @JsonProperty("Value")
        private String value;

        public List<String> getReadableTimePeriods() {
            List<String> timePeriods = new ArrayList<>();
            char[] binaryValue = value.toCharArray();
            String[] days = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日",};

            // 检查 Value 字段的长度是否正确
            if (binaryValue.length != 336) {
                //System.err.println("Value 字段长度不正确，预期长度为 336，实际长度为 " + binaryValue.length);
                return timePeriods; // 如果长度不足，返回空列表
            }

            for (int day = 0; day < 7; day++) {
                StringBuilder dayPeriods = new StringBuilder();
                boolean inPeriod = false;
                int startHour = -1;
                int startMinute = -1;

                for (int halfHour = 0; halfHour < 48; halfHour++) {
                    int hour = halfHour / 2;
                    int minute = (halfHour % 2) * 30;
                    int index = day * 48 + halfHour;

                    if (index < binaryValue.length && binaryValue[index] == '1') {
                        if (!inPeriod) {
                            inPeriod = true;
                            startHour = hour;
                            startMinute = minute;
                        }
                    } else {
                        if (inPeriod) {
                            inPeriod = false;
                            dayPeriods.append(String.format("%02d:%02d~%02d:%02d", startHour, startMinute, hour, minute)).append(", ");
                        }
                    }
                }

                if (inPeriod) {
                    int hour = 23;
                    int minute = 30;
                    dayPeriods.append(String.format("%02d:%02d~%02d:%02d", startHour, startMinute, hour, minute)).append(", ");
                }

                if (dayPeriods.length() > 0) {
                    timePeriods.add(days[day] + " " + dayPeriods.toString().trim().replaceAll(", $", ""));
                }
            }

            System.out.println("Readable time periods: " + timePeriods);
            return timePeriods;
        }
    }
}