package com.example.jetpackdb

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.jetpackdb.data.local.MyNoteEntity
import com.example.jetpackdb.data.local.MyDatabase
import com.example.jetpackdb.ui.theme.JetpackDbTheme
import com.example.jetpackdb.ui.theme.nowDateTimeToString
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)

        val myDatabase = MyDatabase.getDatabase(application)
        val myNotes: Flow<List<MyNoteEntity>> = myDatabase.myNoteDao().getall();

        setContent {
            MyContent1(myNotes, myDatabase)
        }
    }
}

@Composable
fun MyContent1(myNotesFlow: Flow<List<MyNoteEntity>>, database:MyDatabase?=null)
{
    JetpackDbTheme {
        Column {

            // all notes
            val myNotesAsState:State<List<MyNoteEntity>> = myNotesFlow.collectAsState(initial = listOf())
            for (myNote in myNotesAsState.value) {
                Card (
                    colors = CardDefaults.cardColors(containerColor = Color(red = 200, green = 250, blue = 200)),
                    modifier = Modifier.padding(horizontal = 10.dp, vertical = 10.dp)
                )
                {
                    Text(text = "${myNote.comment} (${myNote.date})")

                    // button "Delete note"
                    Button(onClick = {
                        runBlocking { database?.myNoteDao()?.delete(myNote) }
                    }) { Text(text = "Delete note") }
                }
                Text(text = "")
            }

            // entering text for new note
            var textEditing by remember { mutableStateOf("") }
            Row {
                Text(text = "Enter text:")
                TextField(
                    value = textEditing,
                    onValueChange = { textEditing = it }
                )
            }

            // button "Create note"
            Button(onClick = {
                runBlocking {
                    database?.myNoteDao()?.insert(
                        MyNoteEntity(id = 0, comment = textEditing, date = nowDateTimeToString())
                    )
                }
            }) { Text(text = "Create note") }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun PreviewMyContent1()
{
    val listFlow = flow {
        emit(listOf(
            MyNoteEntity(id = 1, comment = "I go to shop.", date = nowDateTimeToString()),
            MyNoteEntity(id = 2, comment = "I watch TV.", date = nowDateTimeToString()),
            MyNoteEntity(id = 3, comment = "Good bye.", date = nowDateTimeToString())
        ))
    }
    MyContent1(listFlow)
}