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

import android.content.SharedPreferences
import android.net.ConnectivityManager
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.runtime.*
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.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import com.ys.bdtp.adm.app.theme.underColor
import com.ys.bdtp.adm.app.tools.isNet
import com.ys.bdtp.adm.mvvm.model.Data
import com.ys.bdtp.adm.mvvm.model.State
import com.ys.bdtp.adm.mvvm.ui.compose.*
import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
import com.ys.bdtp.adm.mvvm.ui.screen.main.projectToLogin
import com.ys.bdtp.adm.mvvm.vm.project.ProjectViewModel
import kotlinx.coroutines.launch
import org.kodein.di.compose.rememberViewModel

/**
 * 项目界面
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Project() {
    val nav = LocalNavHostController.current!!
    val vm by rememberViewModel<ProjectViewModel>()
    val scope = rememberCoroutineScope()
    var popupState by remember { mutableStateOf(false) }

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

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

    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        Head(
            left = {
                ProjectHeadLeft()
            },
            right = {
                HeadRight(
                    role = { "项目管家" },
                    click = {
                        popupState = !popupState
                    }
                )
            }
        )

        if (popupState) {
            LogoutPopup(
                click = {
                    scope.launch {
                        vm.logout()
                    }
                    popupState = false
                }
            )
        }

        Div(Div.Row)
        Column(
            modifier = Modifier
                .fillMaxSize()
                .wrapContentWidth()
                .background(underColor)
                .padding(horizontal = 32.dp, vertical = 40.dp)
        ) {
            val userWithProjects = dataState.value
            when (userWithProjects.state) {
                is State.Loading -> {
                    // TODO 给与加载提示
                }
                is State.Success -> {
                    val uwp = userWithProjects.data
                    if (uwp == null) {
                        nav.projectToLogin()
                    } else if (uwp.projects.isEmpty()) {
                        Blank()
                    } else {
                        ProjectContent(uwp.projects)
                    }
                }
                is State.Failure -> {
                    // TODO 给与失败提示
                }
            }
        }
    }
}

@Preview(
    widthDp = 1280,
    heightDp = 800,
    showBackground = true,
    backgroundColor = 0xFFFFFFFF
)
@Composable
fun PreviewProject() {
    Project()
}