package com.example.pencilmuse.views.MainActivity.components

import android.graphics.drawable.Icon
import androidx.compose.foundation.layout.Arrangement

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.example.pencilmuse.models.NotesManager
import android.content.Context


@Composable
fun Notes(navHost: NavHostController, context: Context) {
    val noteManager = remember { NotesManager(context) }
    val notes = noteManager.loadNotes().reversed()

    Column(
        modifier = Modifier
            .fillMaxSize(1f)

    ) {
        LazyColumn {
            items(notes) { note ->
                NoteItem(note = note, onRemove = { noteManager.removeNote(it) })
            }
        }

        Spacer(modifier = Modifier.weight(1f))

        AddNoteForm(onSubmit = { noteManager.saveNote(it) })
    }
}

@Composable
fun NoteItem(note: String, onRemove: (String) -> Unit) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(8.dp)
    ) {
        Row(
            modifier = Modifier
                .padding(8.dp),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Text(text = note)
            IconButton(onClick = { onRemove(note) }) {
                Icon(Icons.Filled.Delete, contentDescription = "Удалить заметку")
            }
        }
    }
}

@Composable
fun AddNoteForm(onSubmit: (String) -> Unit) {
    var noteText = remember { mutableStateOf("") }

    OutlinedTextField(
        noteText.value,
        onValueChange = { noteText.value = it },
        label = { Text("Ваша заметка") },
        modifier = Modifier.fillMaxWidth(),
    )

    Spacer(modifier = Modifier.height(16.dp))

    Button(
        onClick = {
            if (noteText.value.isNotBlank()) {
                onSubmit(noteText.value)
                noteText.value=""
            }
        },
        enabled = noteText.value.isNotBlank(),
        //modifier = Modifier.align(Alignment.End)
    ) {
        Text("Сохранить")
    }
}