package com.sp365.calendar.utils import com.sp365.calendar.model.Priority import com.sp365.calendar.model.ViewType import java.time.Instant import java.time.LocalDate import java.time.LocalTime import java.time.ZoneId import java.util.UUID fun generateDaysList(startDate: LocalDate, yearsBefore: Int, yearsAfter: Int): List { // Đặt ngày bắt đầu là 01/01/1902 và ngày kết thúc là 10 năm sau ngày hiện tại val startDate = LocalDate.of(1902, 1, 1) val endDate = LocalDate.now().plusYears(10) val daysCount = endDate.toEpochDay() - startDate.toEpochDay() return List(daysCount.toInt()) { startDate.plusDays(it.toLong()) } } // Hàm tính số tháng từ tháng hiện tại đến tháng đã chọn fun calculateMonthsFromStart(date: LocalDate): Int { // Sử dụng tháng hiện tại làm mốc để tính số tháng từ mốc này đến `date` val referenceDate = LocalDate.now().withDayOfMonth(1) // Ngày đầu tháng hiện tại return referenceDate.until(date.withDayOfMonth(1), java.time.temporal.ChronoUnit.MONTHS).toInt() } // Hàm tính số tuần từ tuần hiện tại đến tuần đã chọn fun calculateWeeksFromStart(date: LocalDate): Int { val referenceDate = LocalDate.now().with(java.time.DayOfWeek.MONDAY) // Tuần hiện tại return referenceDate.until( date.with(java.time.DayOfWeek.MONDAY), java.time.temporal.ChronoUnit.WEEKS ).toInt() } fun toDate(dateString: String?): LocalDate { if (dateString == null) return LocalDate.now() return Instant.parse(dateString) .atZone(ZoneId.systemDefault()) .toLocalDate() } fun toTime(timeString: String?): LocalTime { if (timeString == null) return LocalTime.now() return Instant.parse(timeString) .atZone(ZoneId.systemDefault()) .toLocalTime() } fun toPriority(value: String?): Priority { return when (value) { "none" -> Priority.None "low" -> Priority.Low "medium" -> Priority.Medium "high" -> Priority.High "urgent" -> Priority.Urgent else -> Priority.Low } } fun toPriority(value: Int?): Priority { return when (value) { 0 -> Priority.None 1 -> Priority.Low 10 -> Priority.Medium 20 -> Priority.High 30 -> Priority.Urgent else -> Priority.Low } } fun toViewType(value: String): ViewType { return when (value) { "day" -> ViewType.Day "month" -> ViewType.Month else -> ViewType.Day } } fun generateID(): String { return UUID.randomUUID().toString() }