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

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
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.graphics.Color
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.painterResource
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.R
import com.ys.bdtp.adm.app.theme.bgBlueColor
import com.ys.bdtp.adm.app.theme.blueColor
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.vo.request.requestBody
import com.ys.bdtp.adm.mvvm.ui.compose.Div
import com.ys.bdtp.adm.mvvm.vm.offline.OfflineViewModel
import kotlinx.coroutines.launch
import org.kodein.di.compose.rememberViewModel


/**
 * 离线数据左侧UI
 */
@Composable
fun OfflineSide() {
    val vm by rememberViewModel<OfflineViewModel>()

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

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

    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 {
                    // 下载字典
                    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")
                            }
                        )
                    }
                }
                scope.launch {
                    // 下载配置
                    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")
                            }
                        )
                    }
                }
                scope.launch {
                    // 下载建筑与楼层数据
                    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")
                            }
                        )
                    }
                }

            },
            colors = ButtonDefaults.buttonColors(
                contentColor = whiteColor,
                backgroundColor = bgBlueColor
            ),
            elevation = ButtonDefaults.elevation(0.dp)
        ) {
            Text(text = "全部同步")
        }

        Spacer(modifier = Modifier.fillMaxWidth().height(25.dp))
        Div(Div.Row)
        Spacer(modifier = Modifier.fillMaxWidth().height(10.dp))

        // 项目任务
        Column(
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Row(
                modifier = Modifier
                    .width(216.dp)
                    .height(60.dp)
                    .clickable {

                    },
                verticalAlignment = Alignment.CenterVertically
            ) {
                Spacer(modifier = Modifier.width(16.dp))
                Image(
                    painter = painterResource(R.drawable.red_circle),
                    contentDescription = "",
                    modifier = Modifier.size(8.dp)
                )
                Spacer(modifier = Modifier.width(12.dp))
                Text(text = "项目任务")
            }

            // 工程数据
            Row(
                modifier = Modifier
                    .width(216.dp)
                    .height(60.dp)
                    .clickable {

                    },
                verticalAlignment = Alignment.CenterVertically
            ) {
                Spacer(modifier = Modifier.width(16.dp))
                Image(
                    painter = painterResource(R.drawable.red_circle),
                    contentDescription = "",
                    modifier = Modifier.size(8.dp)
                )
                Spacer(modifier = Modifier.width(12.dp))
                Text(text = "工程数据")
            }

            // 楼层模型
            Row(
                modifier = Modifier
                    .width(216.dp)
                    .height(60.dp)
                    .clickable {

                    },
                verticalAlignment = Alignment.CenterVertically
            ) {
                Spacer(modifier = Modifier.width(16.dp))
                Image(
                    painter = painterResource(R.drawable.red_circle),
                    contentDescription = "",
                    modifier = Modifier.size(8.dp)
                )
                Spacer(modifier = Modifier.width(12.dp))
                Text(text = "楼层模型")
            }

            // 参考图纸
            Row(
                modifier = Modifier
                    .width(216.dp)
                    .height(60.dp)
                    .clickable {

                    },
                verticalAlignment = Alignment.CenterVertically
            ) {
                Spacer(modifier = Modifier.width(16.dp))
                Image(
                    painter = painterResource(R.drawable.red_circle),
                    contentDescription = "",
                    modifier = Modifier.size(8.dp)
                )
                Spacer(modifier = Modifier.width(12.dp))
                Text(text = "参考图纸")
            }

            // 交付范围
            Row(
                modifier = Modifier
                    .width(216.dp)
                    .height(60.dp)
                    .clickable {

                    },
                verticalAlignment = Alignment.CenterVertically
            ) {
                Spacer(modifier = Modifier.width(16.dp))
                Image(
                    painter = painterResource(R.drawable.red_circle),
                    contentDescription = "",
                    modifier = Modifier.size(8.dp)
                )
                Spacer(modifier = Modifier.width(12.dp))
                Text(text = "交付范围")
            }

            // 交付规则
            Row(
                modifier = Modifier
                    .width(216.dp)
                    .height(60.dp)
                    .clickable {

                    },
                verticalAlignment = Alignment.CenterVertically
            ) {
                Spacer(modifier = Modifier.width(16.dp))
                Image(
                    painter = painterResource(R.drawable.red_circle),
                    contentDescription = "",
                    modifier = Modifier.size(8.dp)
                )
                Spacer(modifier = Modifier.width(12.dp))
                Text(text = "交付规则")
            }
        }
    }
}

@Preview
@Composable
fun PreviewOfflineSide() {
    OfflineSide()
}