package cz.muni.sutralog

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.card.MaterialCardView
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.PhotoRepository
import cz.muni.sutralog.repository.RockRepository
import java.io.File
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 photoRepository: PhotoRepository by lazy {
        PhotoRepository(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.insertCategories(getMockedCategories())
        rockRepository.insertRocks(getMockedRocks())
    }

    fun getMockedCategories(count: Int = 9): List<CategoryPresentableListItem> =
        mutableListOf<CategoryPresentableListItem>().apply {
            repeat(count) {
                val randomColor = Color.argb(255, Random.nextInt(256), Random.nextInt(256), Random.nextInt(256))
                val item = CategoryPresentableListItem(
                    id = it.toLong() + 1,
                    name = "kategorie $it",
                    color = randomColor
                )
                add(item)
            }
        }

    fun getMockedRocks(count: Int = 80): 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 " + it.toLong(),
                    color = randomColor,
                    categoryId = (it % 9 + 1).toLong(),
                    comment = "commeeeeeeeent",
                    count = 2,
                    date = LocalDate.of(2020,10,10),
                    description = "descriptiooooooon",
                    from = "from wherever",
                    latitude = 55.55555,
                    longitude = 44.4444,
                    locality = "locaaaalityyy",
                    where = "somewhere",
                    pictureName = "awdawda."
                )
                add(item)
            }
        }
}