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

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
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.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.runtime.*
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.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import com.ys.bdtp.adm.app.theme.underColor
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.ProjectEntity
import com.ys.bdtp.adm.mvvm.ui.compose.Head
import com.ys.bdtp.adm.mvvm.ui.compose.HeadRight
import com.ys.bdtp.adm.mvvm.ui.compose.LogoutPopup
import com.ys.bdtp.adm.mvvm.ui.screen.main.*
import com.ys.bdtp.adm.mvvm.vm.task.TaskViewModel
import kotlinx.coroutines.launch
import org.kodein.di.compose.rememberViewModel

@Composable
fun Task(projectId: String) {
    val nav = LocalNavHostController.current!!
    val vm by rememberViewModel<TaskViewModel>()
    var project by remember { mutableStateOf<ProjectEntity?>(null) }
    var popupState by remember { mutableStateOf(false) }
    val scope = rememberCoroutineScope()

    val lifecycleOwner = LocalLifecycleOwner.current
    val userFlowLifecycleAware = remember(vm.userStateFlow, lifecycleOwner) {
        vm.userStateFlow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
    }

    val userState = userFlowLifecycleAware.collectAsState(Data.loading())

    if (userState.value.state == State.Success) {
        val user = userState.value.data
        if (user == null) {
            nav.taskToLogin()
        }
    }

    LaunchedEffect(Unit) {
        project = vm.getProject(projectId)
    }


    Column(modifier = Modifier.fillMaxSize().background(Color.White)) {
        Head(
            left = {
                TaskHeadLeft(
                    title = "${project?.projectName}",
                    click = {
                        nav.navigateUp()
                    })
            },
            right = {
                TaskHeadRight(
                    role = { "管家" },
                    click = { popupState = !popupState }
                )
            }
        )
        if (popupState) {
            LogoutPopup(
                click = {
                    scope.launch {
                        vm.logout()
                    }
                    popupState = false
                }
            )
        }

        Spacer(
            modifier = Modifier.fillMaxWidth().height(1.dp).background(Color(0xFFEFF0F1))
        )

        Row {
            Box(
                modifier = Modifier
                    .fillMaxHeight()
                    .wrapContentWidth()
                    .background(color = underColor)
                    .padding(horizontal = 32.dp, vertical = 30.dp)
            ) {
                // 工作分类
                TaskSide(nav, projectId)
                // 项目切换
            }

            Spacer(modifier = Modifier.width(1.dp).fillMaxHeight().background(Color(0xFFE5E5E5)))

            Column(
                modifier = Modifier
                    .fillMaxHeight()
                    .wrapContentWidth()
                    .padding(horizontal = 40.dp, vertical = 32.dp)
            ) {
                //ProjectBlank()
                TaskContent("查看全部项目任务")
            }
        }
    }
}


@Preview(
    widthDp = 1137,
    heightDp = 711,
)
@Composable
fun PreviewProjectScreen() {
    Task("111")
}