package com.framework.mvvm.ui.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.framework.mvvm.model.vo.Login
import com.framework.mvvm.mv.AdmViewModel
import com.framework.mvvm.ui.LocalNavHostController
import com.sybotan.android.demo.R
import kotlinx.coroutines.launch
import org.kodein.di.compose.localDI
import org.kodein.di.instance

@Composable
fun LoginScreen() {
    val di = localDI()
    val vm by di.instance<AdmViewModel>()
    val scope = rememberCoroutineScope()
    val nav = LocalNavHostController.current!!

    var username by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }
    var isOpenDialog by remember { mutableStateOf(false) }
    var alert by remember { mutableStateOf("") }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White),
        verticalArrangement = Arrangement.Top,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Spacer(modifier = Modifier.height(160.dp))

        Image(
            painter = painterResource(id = R.drawable.logo),
            contentDescription = null,
            modifier = Modifier
                .width(160.dp)
        )

        Spacer(modifier = Modifier.height(72.dp))

        OutlinedTextField(
            value = username,
            onValueChange = { text ->
                username = text
            },
            label = { Text("用户名") }
        )

        Spacer(modifier = Modifier.height(16.dp))

        OutlinedTextField(
            value = password,
            onValueChange = { text -> password = text },
            label = { Text("密 码") }
        )

        Spacer(modifier = Modifier.height(48.dp))

        Button(
            modifier = Modifier
                .width(160.dp)
                .height(56.dp),
            onClick = {
                if (username.isEmpty()) {
                    alert = "用户名不能为空"
                    isOpenDialog = true
                    return@Button
                }

                if (password.isEmpty()) {
                    isOpenDialog = true
                    alert = "密码不能为空"
                    return@Button
                }

                scope.launch {
                    val result = vm.login(Login(userName = username, password = password))
                    if (result.isNotEmpty()) {
                        nav.navigate("project") {
                            popUpTo("login") { inclusive = true }
                        }
                    }
                }

            }) {
            Text(text = "登录", fontSize = 18.sp)
        }

        if (isOpenDialog) {
            AlertDialog(
                onDismissRequest = {
                    isOpenDialog = false
                    alert = ""
                },
                title = {
                    Text(text = "提示")
                },
                text = {
                    Text(text = alert)
                },
                confirmButton = {
                    TextButton(
                        onClick = {
                            isOpenDialog = false
                            alert = ""
                        }
                    ) {
                        Text("OK")
                    }
                }
            )
        }
    }
}

@Preview
@Composable
fun PreviewLoginScreen() {
    LoginScreen()
}
