package com.ys.bdtp.adm.mvvm.ui.screen.cad

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
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.flowWithLifecycle
import com.ys.bdtp.adm.app.theme.textBlackColor
import com.ys.bdtp.adm.app.theme.trDivColor
import com.ys.bdtp.adm.app.theme.whiteColor
import com.ys.bdtp.adm.mvvm.model.Data
import com.ys.bdtp.adm.mvvm.model.State
import com.ys.bdtp.adm.mvvm.model.db.entity.task.CadEntity
import com.ys.bdtp.adm.mvvm.model.db.entity.task.FloorEntity
import com.ys.bdtp.adm.mvvm.ui.compose.Blank
import com.ys.bdtp.adm.mvvm.ui.compose.SelectState
import com.ys.bdtp.adm.mvvm.ui.compose.Table
import com.ys.bdtp.adm.mvvm.vm.cad.CadViewModel
import org.kodein.di.compose.rememberViewModel

@Composable
fun CadContent(selectState: SelectState, floorEntity: FloorEntity?) {
    val vm by rememberViewModel<CadViewModel>()
    val floorState by remember { mutableStateOf(floorEntity) }
    // 根据floor 获取 cad 文件
    val lifecycleOwner = LocalLifecycleOwner.current
    val lifecycleWare = remember(lifecycleOwner) {
        vm.getFloorWithCads(floorEntity?.id ?: "")
            .flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)

    }
    val projectData = lifecycleWare.collectAsState(Data.loading())

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(whiteColor)
            .padding(horizontal = 50.dp, vertical = 50.dp)
    ) {
        if (selectState == SelectState.Left) {
            Table(
                name = {
                    Text(text = "土建图纸核查", color = textBlackColor, fontSize = 16.sp)
                },
                th = {
                    CadDrawingTitle()
                },
                tr = {
                    val data = projectData.value
                    when (data.state) {
                        is State.Loading -> {}
                        is State.Success -> {
                            LazyColumn(modifier = Modifier.fillMaxSize()) {
                                data.data?.let {
                                    items(it.cads) { cad: CadEntity ->
                                        Box {
                                            CadDrawingItem(cad)
                                            Spacer(
                                                modifier = Modifier
                                                    .align(Alignment.BottomCenter)
                                                    .fillMaxWidth().height(1.dp)
                                                    .background(trDivColor)
                                            )
                                        }
                                    }
                                }
                            }
                        }
                        is State.Failure -> {}
                    }
                }
            )
        } else {
            Table(
                name = {
                    Text(text = "机电图纸核查", color = textBlackColor, fontSize = 16.sp)
                },
                th = {
                    CadDrawingTitle()
                },
                tr = {
                    val data = projectData.value
                    when (data.state) {
                        is State.Loading -> {}
                        is State.Success -> {
                            LazyColumn(modifier = Modifier.fillMaxSize()) {
                                data.data?.let {
                                    items(it.cads) { cad: CadEntity ->
                                        Box {
                                            CadDrawingItem(cad)
                                            Spacer(
                                                modifier = Modifier
                                                    .align(Alignment.BottomCenter)
                                                    .fillMaxWidth().height(1.dp)
                                                    .background(trDivColor)
                                            )
                                        }
                                    }
                                }
                            }
                        }
                        is State.Failure -> {}
                    }

                    /*   LazyColumn(modifier = Modifier.fillMaxSize()) {
                           items(20) {
                               Box {
                                   CadDrawingItem()
                                   Spacer(
                                       modifier = Modifier
                                           .align(Alignment.BottomCenter)
                                           .fillMaxWidth().height(1.dp)
                                           .background(trDivColor)
                                   )
                               }
                           }
                       }*/
                }
            )
        }

    }
}

@Preview(widthDp = 910, heightDp = 711)
@Composable
fun PreviewCadContent() {
    // CadContent(SelectState.Left)
}