package cinema

const val STANDARD: Int = 10
const val DISCOUNT: Int = 8
const val B_SYMBOL: Char = 'B'
const val S_SYMBOL: Char = 'S'
const val SMALL_ROOM: Int = 60

enum class Cinema(var number: Int) {
    ROW(7), SEATS(8),
    TOTAL_SEATS(ROW.number * SEATS.number),
    SELECT_ROW(0), SELECT_SEAT(0),
    PURCHASED_TICKET(0), CURRENT_INCOME(0),
    TOTAL_INCOME(0),
}

var mappingSeats = MutableList(Cinema.ROW.number) { MutableList(Cinema.SEATS.number) { S_SYMBOL } }

fun userInput() {
    println("Enter the number of rows:")
    Cinema.ROW.number = readln().toInt()

    println("Enter the number of seats in each row:")
    Cinema.SEATS.number = readln().toInt()

    Cinema.TOTAL_SEATS.number = Cinema.ROW.number * Cinema.SEATS.number
    mappingSeats = MutableList(Cinema.ROW.number) { MutableList(Cinema.SEATS.number) { S_SYMBOL } }

    if (Cinema.TOTAL_SEATS.number <= SMALL_ROOM) {
        Cinema.TOTAL_INCOME.number = Cinema.TOTAL_SEATS.number * STANDARD
    } else {
        val frontHalf = (Cinema.ROW.number / 2) * Cinema.SEATS.number * STANDARD
        val backHalf = ((Cinema.ROW.number / 2) + (Cinema.ROW.number % 2)) * Cinema.SEATS.number * DISCOUNT
        Cinema.TOTAL_INCOME.number = frontHalf + backHalf
    }
    seatsPreview(mappingSeats)
}

fun buyTicket() {
    try {
        println("Enter a row number:")
        Cinema.SELECT_ROW.number = readln().toInt()

        println("Enter a seat number in that row:")
        Cinema.SELECT_SEAT.number = readln().toInt()

        println("Ticket price: $${ticketPrice()}")

        if (mappingSeats[Cinema.SELECT_ROW.number - 1][Cinema.SELECT_SEAT.number - 1] == B_SYMBOL) {
            throw Exception()
        } else {
            mappingSeats[Cinema.SELECT_ROW.number - 1][Cinema.SELECT_SEAT.number - 1] = B_SYMBOL
        }
        Cinema.PURCHASED_TICKET.number += 1

    } catch (e: IndexOutOfBoundsException) {
        println("Wrong input!")
        buyTicket()
    } catch (e: Exception) {
        println("That ticket has already been purchased")
        buyTicket()
    }
    Cinema.CURRENT_INCOME.number += ticketPrice()
}

fun seatsPreview(mappingSeats: MutableList<MutableList<Char>>) {
    print("Cinema:\n  ")
    (1..mappingSeats[0].size).forEach { print("$it ") }
    println()
    for (i in 1..mappingSeats.size) {
        print("$i ")
        println(mappingSeats[i - 1].joinToString(separator = " "))
    }
}

fun ticketPrice(): Int {
    val ticketPrice =
        if (Cinema.TOTAL_SEATS.number <= SMALL_ROOM || Cinema.SELECT_ROW.number <= mappingSeats.size / 2) {
            STANDARD
        } else {
            DISCOUNT
        }
    return ticketPrice
}

fun main() {
    userInput()
    cinema@ while (true) {
        println(
            """
        1. Show the seats
        2. Buy a ticket
        0. Exit
    """.trimIndent()
        )

        when (readln()) {
            "1" -> seatsPreview(mappingSeats)
            "2" -> buyTicket()
            "0" -> break@cinema
        }
    }
}
package cinema

const val STANDARD: Int = 10
const val DISCOUNT: Int = 8
const val B_SYMBOL: Char = 'B'
const val S_SYMBOL: Char = 'S'
const val SMALL_ROOM: Int = 60

enum class Cinema(var number: Int) {
    ROW(7), SEATS(8),
    TOTAL_SEATS(ROW.number * SEATS.number),
    SELECT_ROW(0), SELECT_SEAT(0),
    PURCHASED_TICKET(0), CURRENT_INCOME(0),
    TOTAL_INCOME(0),
}

var mappingSeats = MutableList(Cinema.ROW.number) { MutableList(Cinema.SEATS.number) { S_SYMBOL } }

fun userInput() {
    println("Enter the number of rows:")
    Cinema.ROW.number = readln().toInt()

    println("Enter the number of seats in each row:")
    Cinema.SEATS.number = readln().toInt()

    Cinema.TOTAL_SEATS.number = Cinema.ROW.number * Cinema.SEATS.number
    mappingSeats = MutableList(Cinema.ROW.number) { MutableList(Cinema.SEATS.number) { S_SYMBOL } }

    if (Cinema.TOTAL_SEATS.number <= SMALL_ROOM) {
        Cinema.TOTAL_INCOME.number = Cinema.TOTAL_SEATS.number * STANDARD
    } else {
        val frontHalf = (Cinema.ROW.number / 2) * Cinema.SEATS.number * STANDARD
        val backHalf = ((Cinema.ROW.number / 2) + (Cinema.ROW.number % 2)) * Cinema.SEATS.number * DISCOUNT
        Cinema.TOTAL_INCOME.number = frontHalf + backHalf
    }
    seatsPreview(mappingSeats)
}

fun buyTicket() {
    try {
        println("Enter a row number:")
        Cinema.SELECT_ROW.number = readln().toInt()

        println("Enter a seat number in that row:")
        Cinema.SELECT_SEAT.number = readln().toInt()

        println("Ticket price: $${ticketPrice()}")

        if (mappingSeats[Cinema.SELECT_ROW.number - 1][Cinema.SELECT_SEAT.number - 1] == B_SYMBOL) {
            throw Exception()
        } else {
            mappingSeats[Cinema.SELECT_ROW.number - 1][Cinema.SELECT_SEAT.number - 1] = B_SYMBOL
        }
        Cinema.PURCHASED_TICKET.number += 1

    } catch (e: IndexOutOfBoundsException) {
        println("Wrong input!")
        buyTicket()
    } catch (e: Exception) {
        println("That ticket has already been purchased")
        buyTicket()
    }
    Cinema.CURRENT_INCOME.number += ticketPrice()
}

fun seatsPreview(mappingSeats: MutableList<MutableList<Char>>) {
    print("Cinema:\n  ")
    (1..mappingSeats[0].size).forEach { print("$it ") }
    println()
    for (i in 1..mappingSeats.size) {
        print("$i ")
        println(mappingSeats[i - 1].joinToString(separator = " "))
    }
}

fun ticketPrice(): Int {
    val ticketPrice =
        if (Cinema.TOTAL_SEATS.number <= SMALL_ROOM || Cinema.SELECT_ROW.number <= mappingSeats.size / 2) {
            STANDARD
        } else {
            DISCOUNT
        }
    return ticketPrice
}

fun main() {
    userInput()
    cinema@ while (true) {
        println(
            """
        1. Show the seats
        2. Buy a ticket
        0. Exit
    """.trimIndent()
        )

        when (readln()) {
            "1" -> seatsPreview(mappingSeats)
            "2" -> buyTicket()
            "0" -> break@cinema
        }
    }
}