import kotlin.text.toInt

// different vaiations of for loops

// ranges
fun printOneToTen() {
    for (i in 1..10) {
        println(i)
    }
}

fun printTenToOne() {
    for (i in 10 downTo 1) {
        println(i)
    }
}

fun printOddsOneToTen() {
    for (i in 1..10 step 2) {
        println(i)
    }
}

fun printOddsTenToOne() {
    for (i in 10 downTo 1 step 2) {
        println(i)
    }
}

fun printEvensOneToTen() {
    for (i in 1 until 10 step 2) {
        println(i)
    }
}

fun printEvensTenToOne() {
    for (i in 10 downTo 1 step 2) {
        println(i)
    }
}

// iterating over collections
fun printList() {
    val list = listOf("a", "b", "c")
    for (item: String in list) {
        println(item)
    }
}

// some examples
// add all numbers from 1 to given int
fun addAllNumbersUpTo(int: Int) {
    var res: Int = 0
    for (i in 1..int) res += i
    println("the sum is $res")
}

// hashmap given. for each person print out corresponding number
fun printCorrespondingNumber() {
    val customers = hashMapOf("Peter" to 3, "Anne" to 2, "Chris" to 39)
    // loop through keys and get corresponding value
    for (customerName in customers.keys) {
        // get corresponding value
        val number = customers[customerName]
        println("$customerName has the number $number")
    }
}

/**
 * ask user to input a year, for each month of that year print out how many days there are consider
 * leap years (divisible by 4)
 */
fun daysInMonth() {
    println("enter year")
    val input = readLine() ?: "2020"
    val year = input.toInt()

    for (i in 1..12) {
        val message =
                when (i) {
                    1 -> "January: 31 days"
                    2 -> if (year % 4 == 0) "Febuary: 29 days" else "Febuary: 28 days"
                    3 -> "March: 31 days"
                    4 -> "April: 30 days"
                    5 -> "May: 31 days"
                    6 -> "June: 30 days"
                    7 -> "July: 31 days"
                    8 -> "August: 31 days"
                    9 -> "September: 30 days"
                    10 -> "October: 31 days"
                    11 -> "November: 30 days"
                    12 -> "December: 31 days"
                    else -> ""
                }
        println(message)
    }
}

// race countdown
fun countdown() {
    for (i in 10 downTo 0) {
        when (i) {
            9 -> println(i.toString() + ": start your engines")
            6 -> println(i.toString() + ": on your marks")
            3 -> println(i.toString() + ": get set")
            0 -> println(i.toString() + ": go")
            else -> println(i)
        }
    }
}

// print all even numbers form 10 to 1 with "step"
fun printEven() {
    for (i in 10 downTo 1 step 2) println(i)
}

// nested for loops - print out matrix of a certain size
fun nestedFor(size: Int) {
    for (i in 0..size) {
        for (j in 0..size) {
            // "/t" it tab
            print("$i,$j \t")
        }
        println()
    }
}

// ask user to input a number, print out every number smaller that this number that is divisible by
// 7 in descending order
fun multiplesOfSeven() {
    println("input a number")
    val input = readLine() ?: "0"
    val number = input.toInt()

    for (i in number downTo 0) {
        if (i % 7 == 0) print(i.toString() + "\t")
    }
}

// print lower triangle of a rectangle with "width" and "hight"
fun lowerTriangle(width: Int, hight: Int) {
    var lineWidth: Int = 1

    for (i in 1..hight) {
        for (j in 1..lineWidth) {
            if (lineWidth <= width) {
                print("X")
            } else {
                return
            }
        }
        lineWidth++
        println()
    }
}

fun main(args: Array<String>) {
    // printCorrespondingNumber()
    // addAllNumbersUpTo(5)
    // daysInMonth()
    // countdown()
    // printEven()
    // nestedFor(5)
    // multiplesOfSeven()
    // lowerTriangle(5, 6)
}
