using System; using System.Collections; using System.Collections.Generic; using Kill.Bluetooth.Protocol; using Kill.Managers; using UnityEngine; using UnityEngine.UI; namespace Kill.UI.Pages { public class HomePageSchedule : MonoBehaviour { ScheduleTask[] tasks; public Image scheduleIcon; /// /// 定时任务图标 0:未设置 1:已设置 /// public Sprite[] scheduleIconSprites; public Text scheduleText; // 多语言Key private const string LANG_STANDBY = "100094"; // Standby 待机 private const string LANG_SCAN = "100095"; // Scan 扫描 private const string LANG_ARMED = "100096"; // Armed 消杀 private const string LANG_NO_SCHEDULE = "100101"; // No Schedule 无定时任务 public void Init(ScheduleTask[] tasks) { this.tasks = tasks; UpdateScheduleDisplay(); } /// /// 更新定时任务显示 /// private void UpdateScheduleDisplay() { // 获取今天的星期几(0=周日, 1=周一, ... 6=周六) DayOfWeek today = DateTime.Now.DayOfWeek; byte todayFlag = GetDayFlag(today); // 查找今天启用的任务 ScheduleTask? todayTask = FindTodayTask(todayFlag); if (todayTask.HasValue) { // 有今日任务,显示最近的一个 DisplayTask(todayTask.Value); SetIconScheduled(true); } else { // 没有今日任务 DisplayNoSchedule(); SetIconScheduled(false); } } /// /// 查找今天启用的任务(返回最近的一个未结束任务) /// private ScheduleTask? FindTodayTask(byte todayFlag) { if (tasks == null || tasks.Length == 0) return null; ScheduleTask? nearestTask = null; int nearestTimeDiff = int.MaxValue; int currentTime = DateTime.Now.Hour * 60 + DateTime.Now.Minute; foreach (var task in tasks) { // 检查任务是否启用 if (!task.Enabled) continue; // 检查是否是今天的任务: // 1. Repeat == 0 表示仅一次,认为是当天的任务 // 2. Repeat & todayFlag != 0 表示今天有重复 bool isTodayTask = task.Repeat == 0 || (task.Repeat & todayFlag) != 0; if (!isTodayTask) continue; int taskTime = task.StartHour * 60 + task.StartMinute; int endTime = task.EndHour * 60 + task.EndMinute; // 跳过已结束的任务 if (currentTime >= endTime) continue; int timeDiff = taskTime - currentTime; // 找距离当前时间最近的任务(包括进行中的和未来未开始的) int absTimeDiff = Math.Abs(timeDiff); if (absTimeDiff < nearestTimeDiff) { nearestTimeDiff = absTimeDiff; nearestTask = task; } } return nearestTask; } /// /// 显示任务信息 /// private void DisplayTask(ScheduleTask task) { string modeText = GetModeText(task.Mode); string timeText = $"{task.StartHour:D2}:{task.StartMinute:D2}"; // 判断任务是未来还是进行中 int currentTime = DateTime.Now.Hour * 60 + DateTime.Now.Minute; int taskTime = task.StartHour * 60 + task.StartMinute; bool isFutureTask = taskTime > currentTime; if (scheduleText != null) { if (isFutureTask) { // 即将开始的任务 scheduleText.text = $"{modeText}: {timeText}"; } else { // 任务进行中,显示时间段 scheduleText.text = $"{modeText}: {timeText}~{task.EndHour:D2}:{task.EndMinute:D2}"; } } } /// /// 显示无定时任务 /// public void DisplayNoSchedule() { if (scheduleText != null) { string noScheduleText = LanguageManager.Instance?.GetLanguage(LANG_NO_SCHEDULE) ?? "No Schedule"; scheduleText.text = noScheduleText; } } /// /// 设置图标状态 /// private void SetIconScheduled(bool hasSchedule) { if (scheduleIcon != null && scheduleIconSprites != null && scheduleIconSprites.Length >= 2) { scheduleIcon.sprite = hasSchedule ? scheduleIconSprites[1] : scheduleIconSprites[0]; } } /// /// 获取任务模式显示文本(支持多语言) /// private string GetModeText(ScheduleTaskMode mode) { string langKey = mode switch { ScheduleTaskMode.Standby => LANG_STANDBY, ScheduleTaskMode.Scan => LANG_SCAN, ScheduleTaskMode.Sterilize => LANG_ARMED, _ => LANG_STANDBY }; string defaultText = mode switch { ScheduleTaskMode.Standby => "Standby", ScheduleTaskMode.Scan => "Scan", ScheduleTaskMode.Sterilize => "Armed", _ => "Standby" }; return LanguageManager.Instance?.GetLanguage(langKey) ?? defaultText; } /// /// 获取星期对应的标志位 /// private byte GetDayFlag(DayOfWeek dayOfWeek) { return dayOfWeek switch { DayOfWeek.Monday => ScheduleTask.REPEAT_MONDAY, DayOfWeek.Tuesday => ScheduleTask.REPEAT_TUESDAY, DayOfWeek.Wednesday => ScheduleTask.REPEAT_WEDNESDAY, DayOfWeek.Thursday => ScheduleTask.REPEAT_THURSDAY, DayOfWeek.Friday => ScheduleTask.REPEAT_FRIDAY, DayOfWeek.Saturday => ScheduleTask.REPEAT_SATURDAY, DayOfWeek.Sunday => ScheduleTask.REPEAT_SUNDAY, _ => 0 }; } } }