import java.util.*
import TimeInterval.*

data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
    //可以使用>、<、>=、<=运算符
    override fun compareTo(other: MyDate) = when {
        year != other.year -> year - other.year
        month != other.month -> month - other.month
        else -> dayOfMonth - other.dayOfMonth
    }
}

fun MyDate.followingDate(): MyDate {
    val c = Calendar.getInstance()
    c.set(year, month, dayOfMonth)
    println(c.get(Calendar.YEAR))
    println(c.get(Calendar.MONTH))
    println(c.get(Calendar.DATE))
    val millisecondsInADay = 24 * 60 * 60 * 1000L
    println(c.timeInMillis)
    val timeInMillis = c.timeInMillis + millisecondsInADay
    val result = Calendar.getInstance()
    result.timeInMillis = timeInMillis
    println((result.timeInMillis-c.timeInMillis)==millisecondsInADay)
    println(result.timeInMillis)
    println(result.get(Calendar.YEAR))
    println(result.get(Calendar.MONTH))
    println(result.get(Calendar.DATE))
    return MyDate(result.get(Calendar.YEAR), result.get(Calendar.MONTH), result.get(Calendar.DATE))
}
//可以使用..运算符
operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other)

class DateRange(val start: MyDate, val end: MyDate) : Iterable<MyDate> {
    //可以使用for(date in start..end)语句
    override fun iterator(): Iterator<MyDate> {
        return object : Iterator<MyDate> {
            var current: MyDate = start

            override fun next(): MyDate {
                if (!hasNext()) throw NoSuchElementException()
                val result = current
                current = current.followingDate()
                return result
            }

            override fun hasNext(): Boolean = current <= end
        }
    }
}
fun MyDate.addTimeIntervals(timeInterval: TimeInterval, amount: Int): MyDate {
    val c = Calendar.getInstance()
    c.set(year + if (timeInterval == TimeInterval.YEAR) amount else 0, month, dayOfMonth)
    var timeInMillis:Long = c.timeInMillis
    val millisecondsInADay = 24 * 60 * 60 * 1000L
    timeInMillis += amount * when (timeInterval) {
        TimeInterval.DAY -> millisecondsInADay
        TimeInterval.WEEK -> 7 * millisecondsInADay
        TimeInterval.YEAR -> 0L
    }
    val result = Calendar.getInstance()
    result.timeInMillis = timeInMillis
    return MyDate(result.get(Calendar.YEAR), result.get(Calendar.MONTH), result.get(Calendar.DATE))
}

enum class TimeInterval { DAY, WEEK, YEAR }
class RepeatedTimeInterval(val timeInterval:TimeInterval,val number:Int)
operator fun MyDate.plus(timeInterval: TimeInterval): MyDate = addTimeIntervals(timeInterval,1)

operator fun  TimeInterval.times(n:Int)=RepeatedTimeInterval(this,n)
operator fun MyDate.plus(timeIntervals:RepeatedTimeInterval) = addTimeIntervals(timeIntervals.timeInterval,timeIntervals.number)

fun task1(today: MyDate): MyDate {
    return today + YEAR + WEEK
}

fun task2(today: MyDate): MyDate {
    return today + YEAR * 2 + WEEK * 3 + DAY * 5
}
fun main(){
//    println(task1(MyDate(1,1,1)))
//    println(task2(MyDate(1,1,1)))
//    for (date in MyDate(2020,1,1)..MyDate(2020,2,1)) {
//        println(date)
//    }
println(MyDate(2020,1,29).followingDate())
}