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

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
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.FloorEntity
import com.ys.bdtp.adm.mvvm.ui.compose.*
import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
import com.ys.bdtp.adm.mvvm.vm.cad.CadViewModel
import org.kodein.di.compose.rememberViewModel

@Composable
fun CadScreen(projectId: String) {
    var selectState by remember { mutableStateOf<SelectState>(SelectState.Left) }
    val nav = LocalNavHostController.current!!
    val vm by rememberViewModel<CadViewModel>()

    val lifecycleOwner = LocalLifecycleOwner.current
    val lifecycleWare = remember(lifecycleOwner) {
        vm.getProjectWithBuildingAndFloors(projectId)
            .flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
    }
    val projectData = lifecycleWare.collectAsState(Data.loading())
    var floorState by remember { mutableStateOf<FloorEntity?>(null) }
    Column(modifier = Modifier.background(Color.White)) {
        Head(
            left = {
                CadHeadLeft(
                    title = "CAD图纸核查",
                    click = {
                        nav.navigateUp()
                    }
                )
            },
            middle = {
                SelectButton(
                    SelectState.Left,
                    click = {
                        selectState = it
                    }
                )
            },
            right = {
                HeadRight(
                    role = { "物业管家" },
                    click = {

                    }
                )
            }
        )
        Div(Div.Row)
        Body(
            modifier = Modifier.fillMaxSize(),
            side = {
                when (projectData.value.state) {
                    is State.Loading -> {

                    }
                    is State.Success -> {
                        val project = projectData.value.data
                        if (project != null) {
                            CadSide(
                                buildings = project.buildings,
                                click = { floorEntity ->
                                    floorState = floorEntity
                                }

                            )
                        }
                    }
                    is State.Failure -> {

                    }
                }
            },
            content = {
                CadContent(selectState, floorState)
            },
            div = { Div(Div.Col) }
        )
    }
}

@Preview(widthDp = 1137, heightDp = 711)
@Composable
fun PreviewCadScreen() {
    CadScreen("nihao")
}