package cz.muni.sutralog

import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import cz.muni.sutralog.data.CategoryPresentableListItem
import cz.muni.sutralog.data.RockPresentableListItem
import cz.muni.sutralog.databinding.ActivityMainBinding
import cz.muni.sutralog.repository.CategoryRepository
import cz.muni.sutralog.repository.RockRepository
import java.time.LocalDate
import kotlin.random.Random

class MainActivity : AppCompatActivity() {
    // TODO for testing only. remove
    private val categoryRepository: CategoryRepository by lazy {
        CategoryRepository(applicationContext)
    }
    // TODO for testing only. remove
    private val rockRepository: RockRepository by lazy {
        RockRepository(applicationContext)
    }

    private val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // TODO for testing only. remove
        mockDatabase()
        setContentView(binding.root)
    }

    private fun mockDatabase() {
        categoryRepository.saveCategories(getMockedCategories())

        rockRepository.saveRocks(getMockedRocks())
    }

    fun getMockedCategories(count: Int = 9): List<CategoryPresentableListItem> =
        mutableListOf<CategoryPresentableListItem>().apply {
            repeat(count) {
                val item = CategoryPresentableListItem(
                    id = it.toLong() + 1,
                    name = "kategorie $it",
                    color = Color.parseColor("#e2e2e2")
                )
                add(item)
            }
        }

    fun getMockedRocks(count: Int = 9): List<RockPresentableListItem> =
        mutableListOf<RockPresentableListItem>().apply {
            repeat(count) {
                val randomColor = Color.argb(255, Random.nextInt(256), Random.nextInt(256), Random.nextInt(256))
                val item = RockPresentableListItem(
                    id = it.toLong() + 1,
                    name = "Superšutr",
                    color = randomColor,
                    categoryId = (it % 2 + 1).toLong(),
                    comment = "commeeeeeeeent",
                    count = 2,
                    date = LocalDate.of(2020,10,10),
                    description = "descriptiooooooon",
                    folderPath = "some/path/probably/with/rock/id",
                    from = "from wherever",
                    latitude = 55.55555,
                    longitude = 44.4444,
                    locality = "locaaaalityyyy",
                    where = "somewhere",
                    pictureName = "awdawda."
                )
                add(item)
            }
        }
}