package com.iscs.bozzys.ui.pages.home

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
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.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.pulltorefresh.PullToRefreshBox
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.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import com.iscs.bozzys.R
import com.iscs.bozzys.ui.pages.compose.JobListItem
import com.iscs.bozzys.ui.pages.vm.VMHome
import com.iscs.bozzys.ui.theme.Main
import com.iscs.bozzys.ui.theme.Text

@Composable
fun JobsCompose(pv: PaddingValues, zIndex: Float, vmHome: VMHome) {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .zIndex(zIndex)
            .background(Color.White)
    ) {
        // 顶部工具栏
        Column(
            modifier = Modifier
                .padding(bottom = pv.calculateBottomPadding())
                .fillMaxSize()
        ) {
            TopToolBar(pv, vmHome)
            MessageList()
        }
    }
}

/**
 * 顶部工具栏
 */
@Composable
private fun TopToolBar(pv: PaddingValues, vmHome: VMHome) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(Main)
            .padding(top = pv.calculateTopPadding())
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(66.dp)
                .padding(start = 20.dp, end = 10.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text("我的作业", fontSize = 18.sp, color = Color.White, fontWeight = FontWeight.Medium)
            Spacer(Modifier.weight(1f))
            Icon(
                painter = painterResource(R.drawable.add),
                contentDescription = null,
                modifier = Modifier
                    .size(36.dp)
                    .clip(RoundedCornerShape(12.dp))
                    .clickable(onClick = {})
                    .padding(6.dp),
                tint = Color.White
            )
        }
    }
}

/**
 * 消息列表
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun MessageList() {
    val isRefresh = remember { mutableStateOf(false) }
    val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
    PullToRefreshBox(isRefresh.value, onRefresh = {
        isRefresh.value = true
    }, modifier = Modifier.fillMaxSize()) {
        LazyColumn(
            modifier = Modifier
                .padding(top = 5.dp)
                .fillMaxSize()
                .padding(horizontal = 20.dp)
        ) {
            items(list) { item ->
                when (item) {
                    1 -> MessageListItemTitle("今天")
                    6 -> MessageListItemTitle("昨天")
                    else -> JobListItem()
                }
            }
        }
    }
}

/**
 * 消息标题
 */
@Composable
private fun MessageListItemTitle(title: String) {
    Text(
        title,
        fontSize = 16.sp,
        lineHeight = 21.sp,
        fontWeight = FontWeight.Medium,
        color = Text,
        modifier = Modifier
            .padding(bottom = 10.dp)
            .fillMaxWidth()
    )
}