package com.img.rabbit.pages.screen.mine

import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.img.rabbit.R
import com.img.rabbit.pages.toolbar.TitleBar
import com.img.rabbit.provider.storage.GlobalStateManager
import com.img.rabbit.utils.AppDataStoreUtils
import com.img.rabbit.viewmodel.LoginViewModel

@SuppressLint("UnrememberedMutableState")
@Composable
fun SettingScreen(navController: NavHostController, loginViewModel: LoginViewModel) {
    val context = LocalContext.current
    var globalLogin by mutableStateOf(GlobalStateManager(context).globalLoginNotifyFlow().collectAsState(initial = false))
    var cacheDataSize by remember { mutableStateOf("正在计算...") }
    /*
    LaunchedEffect(loginViewModel.logoutState.value) {
        if(loginViewModel.logoutState.value?.status == true){
            // 执行用户数据清除
            PreferenceUtil.clearLogin()
            loginViewModel.setLogin(false)
            // 跳转登录页面
            loginViewModel.restViewModel.intValue = 1

            loginViewModel.logoutState.value = null

            loginViewModel.loginScreenType.value = LoginScreenType.LOGIN_NORMAL
            navController.navigate("login?type=${LoginViewModel.JumpLoginType.FROM_LOGOUT.type}") {
                // 清除所有页面，包括登录页
                popUpTo(navController.graph.startDestinationId) { inclusive = true }
            }
        }
    }
     */

    LaunchedEffect(Unit) {
        AppDataStoreUtils.getAppStorageStats(context){ _, _, _, totalDataCacheSize ->
            cacheDataSize = totalDataCacheSize
        }
    }
    if(globalLogin.value == true){
        navController.popBackStack()
    }



    Scaffold{
        Column(
            modifier = Modifier.fillMaxSize()
        ) {
            TitleBar(navController = navController, paddingValues = it, title = "设置")

            Box(
                modifier = Modifier.fillMaxSize().padding(start = 17.dp, end = 17.dp)
            ){
                // 设置内容
                Column(
                    modifier = Modifier
                        .wrapContentSize()
                        .padding(top = 16.dp)
                        .background(
                            Color(0xFFFFFFFF),
                            RoundedCornerShape(18.dp)
                        )
                        .shadow(
                            elevation = 4.dp,
                            shape = RoundedCornerShape(8.dp),
                            ambientColor = Color(0x4DE5E5E5),
                            spotColor = Color(0x4DE5E5E5)
                        )
                ){
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(52.dp)
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                // 跳转页面
                                //navController.navigate("")
                                AppDataStoreUtils.openAppSettings(context)
                            },
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Box(
                            modifier = Modifier.weight(1f)
                        ){
                            Text(
                                text = "清除缓存",
                                fontSize = 14.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color(0xFF1A1A1A),
                                modifier = Modifier
                                    .wrapContentSize()
                                    .padding(start = 8.dp)
                            )
                        }

                        Row(
                            modifier = Modifier.wrapContentSize()
                        ) {
                            Text(
                                cacheDataSize,
                                fontSize = 16.sp,
                                fontWeight = FontWeight.Normal,
                                modifier = Modifier
                                    .wrapContentSize()
                                    .align(Alignment.CenterVertically),
                                color = Color(0xFF767676),
                                textAlign = TextAlign.Center
                            )
                            Image(
                                painter = painterResource(id = R.mipmap.ic_arrow_right),
                                contentDescription = null,
                                contentScale = ContentScale.FillWidth,
                                modifier = Modifier
                                    .wrapContentSize()
                                    .padding(end = 12.dp)
                                    .align(Alignment.CenterVertically)
                            )
                        }
                    }
                    Box(
                        modifier = Modifier.fillMaxWidth().height(0.5.dp).padding(horizontal = 12.dp).background(
                            Color(0x4DBBBBBB)
                        )
                    )
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(52.dp)
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                // 跳转绑定页面
                                navController.navigate("bindAccount")
                            },
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Box(
                            modifier = Modifier.weight(1f)
                        ){
                            Text(
                                text = "账号绑定",
                                fontSize = 14.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color(0xFF1A1A1A),
                                modifier = Modifier
                                    .wrapContentSize()
                                    .padding(start = 8.dp)
                            )
                        }

                        Image(
                            painter = painterResource(id = R.mipmap.ic_arrow_right),
                            contentDescription = null,
                            contentScale = ContentScale.FillWidth,
                            modifier = Modifier
                                .wrapContentSize()
                                .padding(end = 12.dp)
                                .align(Alignment.CenterVertically)
                        )
                    }
                    Box(
                        modifier = Modifier.fillMaxWidth().height(0.5.dp).padding(horizontal = 12.dp).background(
                            Color(0x4DBBBBBB)
                        )
                    )
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(52.dp)
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                // 跳转账号管理页面
                                navController.navigate("managerAccount")
                            },
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Box(
                            modifier = Modifier.weight(1f)
                        ){
                            Text(
                                text = "账号管理",
                                fontSize = 14.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color(0xFF1A1A1A),
                                modifier = Modifier
                                    .wrapContentSize()
                                    .padding(start = 8.dp)
                            )
                        }

                        Image(
                            painter = painterResource(id = R.mipmap.ic_arrow_right),
                            contentDescription = null,
                            contentScale = ContentScale.FillWidth,
                            modifier = Modifier
                                .wrapContentSize()
                                .padding(end = 12.dp)
                                .align(Alignment.CenterVertically)
                        )
                    }
                    Box(
                        modifier = Modifier.fillMaxWidth().height(0.5.dp).padding(horizontal = 12.dp).background(
                            Color(0x4DBBBBBB)
                        )
                    )
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(52.dp)
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                // 跳转关于我们页面
                                navController.navigate("aboutMine")
                            },
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Box(
                            modifier = Modifier.weight(1f)
                        ){
                            Text(
                                text = "关于我们",
                                fontSize = 14.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color(0xFF1A1A1A),
                                modifier = Modifier
                                    .wrapContentSize()
                                    .padding(start = 8.dp)
                            )
                        }

                        Image(
                            painter = painterResource(id = R.mipmap.ic_arrow_right),
                            contentDescription = null,
                            contentScale = ContentScale.FillWidth,
                            modifier = Modifier
                                .wrapContentSize()
                                .padding(end = 12.dp)
                                .align(Alignment.CenterVertically)
                        )
                    }
                }

                //切换/退出账号
                Column(
                    modifier = Modifier.fillMaxWidth().align(Alignment.BottomCenter)
                ) {
                    //账号切换
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .padding(start = 33.dp, end = 33.dp, bottom = 18.dp)
                            .background(
                                Color(0x00000000),
                                shape = RoundedCornerShape(359.dp),
                            )
                            .border(width = 1.dp, color = Color(0xFF000000), shape = RoundedCornerShape(359.dp))
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                //TODO 账号切换
                                navController.navigate("managerAccount")
                            }
                    ) {
                        Text(
                            "账号切换",
                            color = Color(0xFF1A1A1A),
                            fontSize = 16.sp,
                            fontWeight = FontWeight.Bold,
                            modifier = Modifier
                                .wrapContentWidth()
                                .wrapContentHeight()
                                .padding(vertical = 12.dp)
                                .align(Alignment.Center)
                        )
                    }

                    //退出登录
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .padding(start = 33.dp, end = 33.dp, bottom = 30.dp)
                            .background(
                                Color(0xFF252525),
                                shape = RoundedCornerShape(359.dp),
                            )
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                // 退出登录
                                loginViewModel.requestLogout(context)
                            }
                    ) {
                        Text(
                            "退出登录",
                            color = Color(0xFFC2FF43),
                            fontSize = 16.sp,
                            fontWeight = FontWeight.Bold,
                            modifier = Modifier
                                .wrapContentWidth()
                                .wrapContentHeight()
                                .padding(vertical = 12.dp)
                                .align(Alignment.Center)
                        )
                    }
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
private fun PreviewSettingScreen(){
    SettingScreen(navController = rememberNavController(), loginViewModel = viewModel())
}