package com.example.tasks.screens

import androidx.compose.foundation.background
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.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.tasks.model.Task
import com.example.tasks.ui.theme.SFPro
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import com.google.firebase.Firebase
import com.google.firebase.firestore.firestore
import kotlinx.coroutines.delay
import java.time.LocalDate
import java.time.temporal.ChronoUnit

@Composable
fun HistoryChief(surname: String, name: String, patronymic: String) {
    val fs = Firebase.firestore
    val list = remember {
        mutableStateOf(emptyList<Task>())
    }
    var refreshing by remember { mutableStateOf(false) }
    LaunchedEffect(refreshing) {
        if (refreshing) {
            delay(2000)
            refreshing = false
        }
    }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(20.dp)
            .padding(top = 40.dp)
            .background(MaterialTheme.colorScheme.background),
        verticalArrangement = Arrangement.Top,
        horizontalAlignment = Alignment.Start
    ) {
        Text(
            text = "История",
            fontSize = 35.sp,
            fontFamily = SFPro,
            fontWeight = FontWeight.Bold,
            color = MaterialTheme.colorScheme.onBackground
        )

        Spacer(modifier = Modifier.height(30.dp))
        SwipeRefresh(
            state = rememberSwipeRefreshState(isRefreshing = refreshing),
            onRefresh = { refreshing = true },
        ) {

            fs.collection("tasks")
                .whereEqualTo(
                    "author", "$surname $name $patronymic"
                ).get()
                .addOnCompleteListener {
                    if (it.isSuccessful) {
                        list.value = it.result.toObjects(Task::class.java)
                    }
                }


            LazyColumn(
                modifier = Modifier
                    .fillMaxSize()
            ) {
                items(list.value) { task ->
                    val endDate = LocalDate.parse(task.end_date)
                    val dayEnd = ChronoUnit.DAYS.between(LocalDate.now(), endDate)
                    if (dayEnd < 0 || task.done == "Выполнено") {
                        Card(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 30.dp)

                        ) {
                            Row(modifier = Modifier.fillMaxWidth()) {
                                TableCellHead(text = "Исполнитель: ")
                                TableCell(text = task.surname + " " + task.name[0] + "." + task.patronymic[0] + ".")
                            }
                            Row(modifier = Modifier.fillMaxWidth()) {
                                TableCellHead(text = "Дата начала: ")
                                TableCell(text = task.start_date)
                            }
                            Row(modifier = Modifier.fillMaxWidth()) {
                                TableCellHead(text = "Дата конца: ")
                                TableCell(text = task.end_date)
                            }
                            Row(modifier = Modifier.fillMaxWidth()) {
                                TableCellHead(text = "Задание: ")
                                TableCell(text = task.task)
                            }
                        }
                    }
                }
            }
        }


    }
}


