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.PhotoPresentableListItem
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

        clearDatabase()

        mockDatabase()

        setContentView(binding.root)
    }

    private fun clearDatabase() {
        photoRepository.deletePhotos(photoRepository.getAllPhotos())

        for(rock in rockRepository.getAllRocks()){
            rockRepository.deleteRock(rock)
        }

        for(cat in categoryRepository.getAllCategories()){
            categoryRepository.deleteCategory(cat)
        }
    }

    private fun mockDatabase() {
        categoryRepository.insertCategories(getMockedCategories())
        rockRepository.insertRocks(getMockedRocks())
    }

    fun getMockedCategories(count: Int = 9): List<CategoryPresentableListItem>{
        var achaty = CategoryPresentableListItem(
            id = 1,
            name = "Achaty",
            color = -12953037
        )

        var avanturiny = CategoryPresentableListItem(
            id = 2,
            name = "Avanturiny",
            color = -10862570
        )

        var beryly = CategoryPresentableListItem(
            id = 3,
            name = "Beryly",
            color = -11219360
        )

        var aragonity = CategoryPresentableListItem(
            id = 4,
            name = "Aragonity",
            color = -40806
        )

        return listOf(achaty, avanturiny, aragonity, beryly)
    }

    fun getMockedRocks() : List<RockPresentableListItem> {
        var achatOhnivy = RockPresentableListItem(
            id = 1,
            categoryId = 1,
            name = "Achat ohnivy",
            count = 5,
            locality = "Brazilie",
            latitude = -5.325829,
            longitude = -58.148309,
            from = "Darovan",
            date = LocalDate.of(2022, 3, 18),
            where = "Kancelar A255",
            description = "",
            comment = "",
            color = -19855
        )

        var achatPestry = RockPresentableListItem(
            id = 2,
            categoryId = 1,
            name = "Achat pestry",
            count = 3,
            locality = "Brazilie",
            latitude = -5.325829,
            longitude = -58.148309,
            from = "Nalezen",
            date = LocalDate.of(2020, 4, 22),
            where = "Doma",
            description = "Nadhernej kamen",
            comment = "Pojmenovani po nazvu reky Achates",
            color = -51935
        )

        var achatZeleny = RockPresentableListItem(
            id = 3,
            categoryId = 1,
            name = "Achat zeleny",
            count = 12,
            locality = "Ceska republika",
            latitude = 49.227856,
            longitude = 16.565397,
            from = "Darovan",
            date = LocalDate.of(2019, 4, 22),
            where = "Doma",
            description = "",
            comment = "Hoooodne zelenej",
            color = -9470977
        )

        var avanturinZeleny = RockPresentableListItem(
            id = 4,
            categoryId = 2,
            name = "Avanturin zeleny",
            count = 1,
            locality = "Polsko",
            latitude = 52.383389,
            longitude = 17.797879,
            from = "Nalezen",
            date = LocalDate.of(2018, 4, 22),
            where = "Doma v trezoru",
            description = "zeleny, mistami do cervena",
            comment = "Stratili ho polski turisti",
            color = -254000
        )

        var avanturinCerveny = RockPresentableListItem(
            id = 5,
            categoryId = 2,
            name = "Avanturin Cerveny",
            count = 3,
            locality = "Madarsko",
            latitude = 46.713548,
            longitude = 19.599810,
            from = "Darovan",
            date = LocalDate.of(2021, 5, 12),
            where = "Doma v trezoru",
            description = "",
            comment = "",
            color = -100000
        )

        return listOf(achatOhnivy, achatPestry, achatZeleny, avanturinCerveny, avanturinZeleny)
    }
}