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

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
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.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import com.ys.bdtp.adm.app.theme.bgBlueColor
import com.ys.bdtp.adm.app.theme.underColor
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.vo.request.requestBody
import com.ys.bdtp.adm.mvvm.vm.offline.OfflineViewModel
import org.kodein.di.compose.rememberViewModel


/**
 * 离线数据左侧UI
 */
@Composable
fun OfflineSide2(projectId: String) {
    val vm by rememberViewModel<OfflineViewModel>()
    val scope = rememberCoroutineScope()


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

    val projectsState = projectsAware.collectAsState(Data.loading())

    Column(
        modifier = Modifier
            .width(280.dp)
            .fillMaxHeight()
            .background(underColor)
            .padding(vertical = 30.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Button(
            modifier = Modifier.size(220.dp, 42.dp),
            onClick = {
                //scope.launch {
                /* vm.getDict(
                     body = requestBody(

                     ),
                     success = {
                         println("success: $it")
                     },
                     failure = {
                         println("failure: $it")
                     }
                 )*/
                //}
            },
            colors = ButtonDefaults.buttonColors(
                contentColor = whiteColor,
                backgroundColor = bgBlueColor
            ),
            elevation = ButtonDefaults.elevation(0.dp)
        ) {
            Text(text = "全部同步")
        }

        Spacer(modifier = Modifier.height(32.dp))
        Button(
            modifier = Modifier.size(220.dp, 42.dp),
            onClick = {
                when (projectsState.value.state) {
                    is State.Loading -> {

                    }
                    is State.Success -> {
                        projectsState.value.data?.forEach {
                            vm.getDict(
                                body = requestBody(
                                    userId = it.project.userId,
                                    groupCode = it.project.groupCode,
                                    projectId = it.project.projectId,
                                ),
                                success = {
                                    println("success: $it")
                                },
                                failure = {
                                    println("failure: $it")
                                }
                            )
                        }
                    }
                    is State.Failure -> {

                    }
                }
            },
            colors = ButtonDefaults.buttonColors(
                contentColor = whiteColor,
                backgroundColor = bgBlueColor
            ),
            elevation = ButtonDefaults.elevation(0.dp)
        ) {
            Text(text = "字典数据")
        }
        Spacer(modifier = Modifier.height(32.dp))
        Button(
            modifier = Modifier.size(220.dp, 42.dp),
            onClick = {
                when (projectsState.value.state) {
                    is State.Loading -> {

                    }
                    is State.Success -> {
                        projectsState.value.data?.forEach {
                            vm.getConfig(
                                body = requestBody(
                                    userId = it.project.userId,
                                    groupCode = it.project.groupCode,
                                    projectId = it.project.projectId,
                                ),
                                success = {
                                    println("success: $it")
                                },
                                failure = {
                                    println("failure: $it")
                                }
                            )
                        }
                    }
                    is State.Failure -> {

                    }
                }
                /*vm.getConfig(
                    body = requestBody(
                        userId = "17337681032",
                        groupCode = "JDKJ",
                        projectId = "Pj1101080665",
                    ),
                    success = {
                        println("success: $it")
                    },
                    failure = {
                        println("failure: $it")
                    }
                )*/
            },
            colors = ButtonDefaults.buttonColors(
                contentColor = whiteColor,
                backgroundColor = bgBlueColor
            ),
            elevation = ButtonDefaults.elevation(0.dp)
        ) {
            Text(text = "配置数据")
        }
        Spacer(modifier = Modifier.height(32.dp))
        Button(
            modifier = Modifier.size(220.dp, 42.dp),
            onClick = {
                when (projectsState.value.state) {
                    is State.Loading -> {

                    }
                    is State.Success -> {
                        projectsState.value.data?.forEach {
                            vm.getBuildingsAndFloors(
                                body = requestBody(
                                    userId = it.project.userId,
                                    groupCode = it.project.groupCode,
                                    projectId = it.project.projectId,
                                ),
                                success = {
                                    println("success: $it")
                                },
                                failure = {
                                    println("failure: $it")
                                }
                            )
                        }
                    }
                    is State.Failure -> {

                    }
                }

                /* vm.getBuildingsAndFloors(
                     body = requestBody(
                         userId = "17337681032",
                         groupCode = "JDKJ",
                         projectId = "Pj1101080665",
                     ),
                     success = {
                         println("success: $it")
                     },
                     failure = {
                         println("failure: $it")
                     }
                 )*/
            },
            colors = ButtonDefaults.buttonColors(
                contentColor = whiteColor,
                backgroundColor = bgBlueColor
            ),
            elevation = ButtonDefaults.elevation(0.dp)
        ) {
            Text(text = "获取建筑与楼层数据")
        }

        Spacer(modifier = Modifier.height(32.dp))
        Button(
            modifier = Modifier.size(220.dp, 42.dp),
            onClick = {
                when (projectsState.value.state) {
                    is State.Loading -> {

                    }
                    is State.Success -> {
                        projectsState.value.data?.forEach { project ->
                            project.buildings.forEach { building ->
                                vm.getMission(
                                    body = requestBody(
                                        userId = project.project.userId,
                                        groupCode = project.project.groupCode,
                                        projectId = project.project.projectId,
                                        buildingId = building.id
                                    ),
                                    success = {
                                        println("success: ")
                                    },
                                    failure = {
                                        println("failure: $it")
                                    }
                                )
                            }
                        }
                    }
                    is State.Failure -> {

                    }
                }

                /*vm.getMission(
                    body = requestBody(
                        userId = "1468407036472936433",
                        groupCode = "XG",
                        projectId = "Pj5001120003",
                        buildingId = "Bd5001120003a9b2f891b91d11ea82bfaf908bb0a852"
                    ),
                    success = {
                        println("success: ")
                    },
                    failure = {
                        println("failure: $it")
                    }
                )*/
            },
            colors = ButtonDefaults.buttonColors(
                contentColor = whiteColor,
                backgroundColor = bgBlueColor
            ),
            elevation = ButtonDefaults.elevation(0.dp)
        ) {
            Text(text = "任务数据")
        }
    }
}


@Preview
@Composable
fun PreviewOfflineSide2() {
    OfflineSide2("")
}