package com.za.ui.order_report

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
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.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.blankj.utilcode.util.TimeUtils
import com.za.base.BaseActivity
import com.za.base.theme.black5
import com.za.base.view.EmptyView
import com.za.base.view.HeadView
import com.za.ext.finish

class HistoryReportActivity : BaseActivity() {
	@Composable
	override fun ContentView() {
		HistoryReportScreen()
	}
}

@Composable
fun HistoryReportScreen(vm : HistoryReportVm = viewModel()) {
	val uiState by vm.uiState.collectAsStateWithLifecycle()
	val context = LocalContext.current
	val lifecycleOwner = LocalLifecycleOwner.current
	DisposableEffect(key1 = lifecycleOwner) {
		val observer = LifecycleEventObserver { _, event ->
			if (event == Lifecycle.Event.ON_RESUME) {
				vm.dispatch(HistoryReportVm.Action.Init)
			}
		}
		lifecycleOwner.lifecycle.addObserver(observer)
		onDispose {
			lifecycleOwner.lifecycle.removeObserver(observer)
		}
	}

	LaunchedEffect(Unit) {
		ReportFloatingManager.startService(context)
	}

	Scaffold(topBar = {
		HeadView(title = "历史报备", onBack = { context.finish() })
	}) {
		LazyColumn(modifier = Modifier
			.fillMaxSize()
			.padding(it),
			horizontalAlignment = Alignment.CenterHorizontally,
			verticalArrangement = Arrangement.Top) {

			if (uiState.historyReportList.isNullOrEmpty()) {
				item {
					EmptyView()
				}
				return@LazyColumn
			}

			items(items = uiState.historyReportList ?: arrayListOf()) { item ->
				Card(modifier = Modifier
					.wrapContentSize()
					.padding(10.dp),
					colors = CardDefaults.cardColors().copy(containerColor = Color.White)) {
					Row(modifier = Modifier.padding(horizontal = 10.dp, vertical = 5.dp),
						verticalAlignment = Alignment.CenterVertically) {
						Box(modifier = Modifier
							.background(color = Color(0x99FF0000).takeIf { item.state == 1 }
								?: Color.Green, shape = RoundedCornerShape(5.dp))
							.padding(horizontal = 10.dp, vertical = 2.dp)) {
							Text("未处理".takeIf { item.state == 1 } ?: "已处理",
								color = Color.Black,
								fontSize = 13.sp)
						}
						Spacer(modifier = Modifier.weight(1f))

						Text(TimeUtils.millis2String(item.createTime ?: System.currentTimeMillis())
							?: "", color = Color.Gray, fontSize = 13.sp)
					}
					HorizontalDivider(color = black5,
						modifier = Modifier
							.fillMaxWidth()
							.padding(horizontal = 10.dp, vertical = 2.dp))
					Box(modifier = Modifier
						.fillMaxWidth()
						.padding(10.dp),
						contentAlignment = Alignment.Center) {
						Text(item.reportTemplate ?: "",
							fontWeight = FontWeight.Bold,
							color = Color.Black)
					}

					Spacer(modifier = Modifier.height(2.dp))
				}
			}
		}
	}
}

@Preview
@Composable
private fun PreviewHistoryReport() {
	HistoryReportScreen()
}