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.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.defaultMinSize
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.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
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.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.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.text.style.TextOverflow
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

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeCompose(pv: PaddingValues, zIndex: Float, vmHome: VMHome) {
    var isRefresh by remember { mutableStateOf(false) }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .zIndex(zIndex)
            .background(Color.White)
    ) {
        PullToRefreshBox(isRefresh, onRefresh = {
            isRefresh = true
        }) {
            Column(Modifier.fillMaxSize()) {
                // 顶部工具栏
                TopToolBar(pv, vmHome)
                Box() {
                    TODOList(pv, vmHome)
                    TODO(vmHome)
                }
            }
        }
    }
}

/**
 * 顶部工具栏
 */
@Composable
private fun TopToolBar(pv: PaddingValues, vmHome: VMHome) {
    val state by vmHome.state.collectAsState()
    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
        ) {
            Icon(
                painter = painterResource(R.drawable.user),
                modifier = Modifier
                    .size(36.dp)
                    .clip(RoundedCornerShape(50))
                    .background(Color(0xFFFFD700))
                    .padding(10.dp),
                contentDescription = null,
                tint = Color.White
            )
            Column(Modifier.padding(horizontal = 10.dp)) {
                Text(state.username, fontSize = 16.sp, lineHeight = 16.sp, fontWeight = FontWeight.Medium, color = Color.White)
                Text("操作员 电气维护组", fontSize = 12.sp, lineHeight = 12.sp, color = Color.White.copy(alpha = 0.8f))
            }
            Spacer(Modifier.weight(1f))
            Box(modifier = Modifier.fillMaxHeight(), contentAlignment = Alignment.Center) {
                Icon(
                    painter = painterResource(R.drawable.bell),
                    contentDescription = null,
                    modifier = Modifier
                        .size(36.dp)
                        .clip(RoundedCornerShape(6.dp))
                        .clickable(onClick = {})
                        .padding(9.dp),
                    tint = Color.White
                )
                Text(
                    "5", color = Color.White, modifier = Modifier
                        .offset(x = 8.dp, y = 15.dp)
                        .defaultMinSize(14.dp, 14.dp)
                        .clip(RoundedCornerShape(14.dp))
                        .background(Color.Red)
                        .align(Alignment.TopCenter)
                        .padding(top = 1.dp)
                        .padding(horizontal = 2.dp),
                    textAlign = TextAlign.Center,
                    fontSize = 10.sp,
                    lineHeight = 10.sp
                )
            }
            Icon(
                painter = painterResource(R.drawable.settings),
                contentDescription = null,
                modifier = Modifier
                    .size(36.dp)
                    .clip(RoundedCornerShape(6.dp))
                    .clickable(onClick = {})
                    .padding(10.dp),
                tint = Color.White
            )
        }
    }
}

/**
 * 待办任务
 */
@Composable
private fun TODO(vmHome: VMHome) {
    Column(
        Modifier
            .fillMaxWidth()
            .background(Color(0xFFF8F9FA))
            .padding(top = 3.dp)
    ) {
        Text(
            "中午好，今天有2个待办任务", Modifier
                .fillMaxWidth()
                .height(51.dp)
                .background(Color(0xFFE6F7FF)),
            fontSize = 14.sp,
            lineHeight = 51.sp,
            color = Text,
            textAlign = TextAlign.Center
        )
        Row(Modifier.padding(vertical = 15.dp)) {
            Spacer(Modifier.width(10.dp))
            Column(
                Modifier
                    .weight(1f)
                    .aspectRatio(1f)
                    .clip(RoundedCornerShape(12.dp))
                    .background(Color(0xFFFFF5EB)),
                verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text("2", fontSize = 24.sp, fontWeight = FontWeight.Bold, color = Color(0xFFFF4500))
                Text("待处理", fontSize = 12.sp, color = Text, modifier = Modifier.padding(top = 5.dp))
                Icon(
                    painterResource(R.drawable.job_todo), contentDescription = null, modifier = Modifier
                        .padding(top = 3.dp)
                        .size(16.dp),
                    tint = Text
                )
            }
            Spacer(Modifier.width(10.dp))
            Column(
                Modifier
                    .weight(1f)
                    .aspectRatio(1f)
                    .clip(RoundedCornerShape(12.dp))
                    .background(Color(0xFFFFF5EB)),
                verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text("5", fontSize = 24.sp, fontWeight = FontWeight.Bold, color = Color(0xFFFFA500))
                Text("进行中", fontSize = 12.sp, color = Text, modifier = Modifier.padding(top = 5.dp))
                Icon(
                    painterResource(R.drawable.job_ing), contentDescription = null, modifier = Modifier
                        .padding(top = 3.dp)
                        .size(16.dp),
                    tint = Text
                )
            }
            Spacer(Modifier.width(10.dp))
            Column(
                Modifier
                    .weight(1f)
                    .aspectRatio(1f)
                    .clip(RoundedCornerShape(12.dp))
                    .background(Color(0xFFFFF5EB)),
                verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text("10", fontSize = 24.sp, fontWeight = FontWeight.Bold, color = Color(0xFF32CD32))
                Text("本月完成", fontSize = 12.sp, color = Text, modifier = Modifier.padding(top = 5.dp))
                Icon(
                    painterResource(R.drawable.job_finish), contentDescription = null, modifier = Modifier
                        .padding(top = 3.dp)
                        .size(16.dp),
                    tint = Text
                )
            }
            Spacer(Modifier.width(10.dp))
        }
    }
}

/**
 * 待办列表
 */
@Composable
private fun TODOList(pv: PaddingValues, vmHome: VMHome) {
    val todoList = listOf(1, 2)
    val messageList = listOf(1, 2, 3)
    Column(
        Modifier
            .padding(horizontal = 20.dp)
            .padding(bottom = pv.calculateBottomPadding())
            .fillMaxSize()
            .verticalScroll(state = rememberScrollState())
            .padding(top = 190.dp)
    ) {
        Row(
            Modifier
                .fillMaxWidth()
                .height(46.dp)
                .clickable(indication = null, interactionSource = remember { MutableInteractionSource() }, onClick = {
                    vmHome.updateNavIndex(1)
                }),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text("我的当前任务", fontSize = 14.sp, fontWeight = FontWeight.Medium, color = Text)
            Spacer(Modifier.weight(1f))
            Text("查看全部", fontSize = 14.sp, color = Color(0xFF1E90FF))
        }
        Column(Modifier.fillMaxWidth()) {
            repeat(todoList.size) { JobListItem() }
        }
        Row(
            Modifier
                .fillMaxWidth()
                .height(46.dp)
                .clickable(indication = null, interactionSource = remember { MutableInteractionSource() }, onClick = {
                    vmHome.updateNavIndex(2)
                }),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text("最新消息", modifier = Modifier.fillMaxHeight(), fontSize = 14.sp, fontWeight = FontWeight.Medium, color = Text)
            Spacer(Modifier.weight(1f))
            Text("全部消息", modifier = Modifier.fillMaxHeight(), fontSize = 14.sp, color = Color(0xFF1E90FF))
        }
        Column(Modifier.fillMaxWidth()) {
            messageList.forEach {
                MessageListItem()
            }
        }
    }
}

@Composable
fun MessageListItem() {
    Column(
        Modifier
            .padding(bottom = 15.dp)
            .fillMaxWidth()
            .height(102.dp)
            .border(1.dp, color = Color(0xFFEEEEEE), shape = RoundedCornerShape(12.dp))
            .clip(RoundedCornerShape(12.dp))
            .background(Color(0xFFF8F9FA))
            .clickable(onClick = {})
            .padding(16.dp)
    ) {
        Text(
            "系统通知：您有新的作业任务待处理，请及时查看并执行操作任务。请及时查看并执行操作任务。请及时查看并执行操作任务。",
            modifier = Modifier.fillMaxWidth(),
            fontSize = 14.sp,
            color = Text,
            maxLines = 2,
            overflow = TextOverflow.Ellipsis
        )
        Spacer(Modifier.weight(1f))
        Row(Modifier.fillMaxWidth()) {
            Spacer(Modifier.weight(1f))
            Text("5分钟前", fontSize = 12.sp, color = Color(0xFF999999))
        }
    }
}