package com.ys.bdtp.adm.mvvm.ui.compose

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Popup
import com.google.accompanist.insets.LocalWindowInsets
import com.google.accompanist.insets.rememberInsetsPaddingValues
import com.ys.bdtp.adm.R
import com.ys.bdtp.adm.app.theme.textBlackColor
import com.ys.bdtp.adm.app.theme.whiteColor

@Composable
fun LogoutPopup(click: () -> Unit) {
    val inset = rememberInsetsPaddingValues(
        LocalWindowInsets.current.statusBars,
        additionalTop = 68.dp
    )
    val y = with(LocalDensity.current) {
        inset.calculateTopPadding().toPx().toInt()
    }
    Popup(
        alignment = Alignment.TopEnd,
        offset = IntOffset(-24, y),
    ) {
        Row(
            modifier = Modifier
                .size(116.dp, 50.dp)
                .background(color = whiteColor, shape = RoundedCornerShape(4.dp))
                .clickable {
                    click()
                },
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center
        ) {
            Icon(
                modifier = Modifier.size(16.dp),
                painter = painterResource(R.drawable.logout),
                contentDescription = "",
                tint = Color(0xFF1F2429)
            )

            Spacer(modifier = Modifier.width(12.dp))

            Text(text = "退出登录", fontSize = 14.sp, color = textBlackColor)
        }
    }
}

@Preview
@Composable
fun PreviewLogoutPopup() {
    LogoutPopup(click = {})
}
