package com.img.rabbit

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.tween
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.Arrangement
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.width
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.foundation.text.ClickableText
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.scale
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.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.viewmodel.compose.viewModel
import com.img.rabbit.config.Constants
import com.img.rabbit.config.Constants.agreementUrl
import com.img.rabbit.config.Constants.privacyUrl
import com.img.rabbit.pages.LoginScreen
import com.img.rabbit.pages.LoginScreenType
import com.img.rabbit.pages.MainScreen
import com.img.rabbit.provider.storage.GlobalStateManager
import com.img.rabbit.provider.storage.PreferenceUtil
import com.img.rabbit.provider.storage.PreferenceUtil.saveBDVID
import com.img.rabbit.utils.ChannelUtils
import com.img.rabbit.utils.UrlLinkUtils.openAgreement
import com.img.rabbit.viewmodel.GeneralViewModel
import com.img.rabbit.viewmodel.LoginViewModel
import com.img.rabbit.viewmodel.SplashViewModel
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.system.exitProcess

class MainActivity : ComponentActivity() {
    @OptIn(DelicateCoroutinesApi::class)
    @SuppressLint("UnrememberedMutableState", "CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        // 必须在 super.onCreate 之前调用
        val splashScreen = installSplashScreen()
        super.onCreate(savedInstanceState)

        // 启用Edge-to-Edge模式（沉浸模式）
        enableEdgeToEdge()

        setContent {
            var showUpdateDialog by remember { mutableStateOf(false) }

            val splashViewModel: SplashViewModel = viewModel()
            val generalViewModel: GeneralViewModel = viewModel()
            var loginViewModel: LoginViewModel = viewModel()
            val context = LocalContext.current
            var showSplash by remember { mutableStateOf(false) }
            var globalLogin by mutableStateOf(GlobalStateManager(context).globalLoginNotifyFlow().collectAsState(initial = false))
            var globalLogout by mutableStateOf(GlobalStateManager(context).globalLogoutNotifyFlow().collectAsState(initial = false))
            var globalUnBind by mutableStateOf(GlobalStateManager(context).globalUnBindNotifyFlow().collectAsState(initial = false))

            LaunchedEffect(generalViewModel.agreementStatus.value) {
                if (generalViewModel.agreementStatus.value == true){
                    saveBDVID()
                    //获取服务器时间
                    generalViewModel.getServerTime()
                }
            }

            LaunchedEffect(generalViewModel.serverTime.value) {
                if (generalViewModel.serverTime.value != null){
                    // 获取用户配置
                    loginViewModel.requestUserConfig()
                    initUM()
                }
            }

            // 设置启动页显示条件
            splashScreen.setKeepOnScreenCondition {
                splashViewModel.isLoading.value // 当为 true 时，启动页不消失
            }
            // 登录成功后，2秒后自动更新状态
            if(globalLogin.value == true){
                GlobalScope.launch {
                    //延迟2秒，方便处理多有事件都收到通知
                    delay(2*1000)
                    GlobalStateManager(context).storeGlobalLoginNotify(false)
                }
            }

            // 退出成功后，2秒后自动更新状态
            if(globalLogout.value == true){
                GlobalScope.launch {
                    //延迟2秒，方便处理多有事件都收到通知
                    delay(2*1000)
                    GlobalStateManager(context).storeGlobalLogoutNotify(false)
                }
            }

            // 解绑成功后，2秒后自动更新状态
            if(globalUnBind.value == true){
                GlobalScope.launch {
                    //延迟2秒，方便处理多有事件都收到通知
                    delay(2*1000)
                    GlobalStateManager(context).storeGlobalUnBindNotify(false)
                }
            }

            AppTheme {
                SplashScreenContent{
                    //未同意提示政策弹窗
                    if (generalViewModel.agreementStatus.value == false){
                        //同意继续
                        PrivacyPolicyScreen(
                            viewModel = loginViewModel,
                        ) { isAllowPrivacyPolicy ->
                            if (isAllowPrivacyPolicy) {
                                generalViewModel.setIsAgreement(true)
                                showSplash = true
                            } else {
                                // 不同意隐私协议政策，直接退出应用
                                (context as MainActivity).finish()
                                // 强制退出应用进程
                                exitProcess(0)
                            }
                        }
                    }else{
                        showSplash = true
                    }

                    if(showSplash || globalLogout.value == true){
                        // 全局注销，重新登录
                        if(globalLogout.value == true){
                            loginViewModel = viewModel()
                            loginViewModel.requestUserConfig()
                            //loginViewModel.initWXApi(this)
                        }

                        val token = PreferenceUtil.getAccessToken()
                        // 未登录，显示登录页
                        if (token.isNullOrEmpty() && !loginViewModel.isLogin.value) {
                            // 同意隐私协议政策，检验是否有一键登录权限
                            loginViewModel.oneKeyLoginForGeTuiSdk(context as Activity) { isAllowShowOneKeyScreen ->
                                if (isAllowShowOneKeyScreen) {
                                    loginViewModel.loginScreenType.value = LoginScreenType.LOGIN_ONE_KEY
                                } else {
                                    // 检验是否有一键登录权限失败,显示验证码登录
                                    loginViewModel.loginScreenType.value = LoginScreenType.LOGIN_CAPTCHA
                                }
                            }

                            // 显示登录页
                            LoginScreen(generalViewModel = generalViewModel, loginViewModel = loginViewModel, isVisibilityBreak = false)
                        } else {
                            //已登录，显示主页面
                            MainScreen(generalViewModel = generalViewModel, loginViewModel = loginViewModel)
                        }
                    }


                    if(showUpdateDialog){
                        UpdateDialog(
                            title = "新版本，更新提示",
                            newVersion = "1.0.0",
                            desc = "修复了一些问题，新增了一些功能",
                            url = "https://www.baidu.com",
                        ){ state ->
                            showUpdateDialog = state
                        }
                    }
                }
            }

            // 模拟加载过程，2秒后关闭启动页
            LaunchedEffect(Unit) {
                delay(500L)
                splashViewModel.setLoading(false)
            }
        }
    }



    /**
     * 初始化友盟
     */
    private fun initUM() {
        UMConfigure.preInit(applicationContext, Constants.UmengAppkey, ChannelUtils.getChannel(applicationContext))
        UMConfigure.init(this, Constants.UmengAppkey, ChannelUtils.getChannel(applicationContext), UMConfigure.DEVICE_TYPE_PHONE, "")
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)
    }
}


@Composable
fun AppTheme(content: @Composable () -> Unit) {
    // 使用Material3主题
    androidx.compose.material3.MaterialTheme {
        content()
    }
}


@Composable
fun SplashScreenContent(
    onAnimationFinished: @Composable () -> Unit  // 改为Composable函数类型
) {
    val scale = remember { Animatable(0f) }
    var animationFinished by remember { mutableStateOf(false) }

    LaunchedEffect(Unit) {
        scale.animateTo(
            targetValue = 1f,
            animationSpec = tween(durationMillis = 800)
        )
        animationFinished = true
    }

    Box(modifier = Modifier.fillMaxSize()) {
        if (!animationFinished) {
            // 显示启动页动画
            Image(
                painter = painterResource(id = R.mipmap.ic_splash_mask),
                contentDescription = null,
                contentScale = ContentScale.FillWidth,
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight()
                    .scale(scale.value)
            )
            Image(
                painter = painterResource(id = R.mipmap.ic_splash_logo),
                contentDescription = null,
                modifier = Modifier
                    .align(Alignment.Center)
                    .wrapContentSize()
                    .scale(scale.value)
            )
        } else {
            // 动画完成后显示主界面
            onAnimationFinished()
        }
    }
}


@Composable
private fun PrivacyPolicyScreen(viewModel: LoginViewModel, onAgreementChange: (Boolean) -> Unit) {
    val context = LocalContext.current
    Box(
        modifier = Modifier.fillMaxSize()
    ){
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(Color(0xCC000000))
        ){
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(horizontal = 38.dp, vertical = 213.dp)
                    .align(Alignment.Center)
                    .background(Color.White, shape = RoundedCornerShape(26.dp))
            ){
                Image(
                    painter = painterResource(id = R.mipmap.ic_privacy_policy_top_mask),
                    contentDescription = null,
                    modifier = Modifier
                        .fillMaxSize(),
                    alignment = Alignment.TopCenter
                )

                Column(
                    modifier = Modifier
                        .padding(top = 36.dp)
                        .align(Alignment.TopCenter)
                ) {
                    Text(
                        text = "用户协议与隐私政策",
                        modifier = Modifier
                            .wrapContentSize()
                            .align(Alignment.CenterHorizontally),
                        fontWeight = FontWeight.Normal,
                        fontSize = 18.sp,
                        color = Color(0xFF1A1A1A)
                    )
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(24.dp)
                    )

                    val agreement = "请您务必审慎阅读、充分理解《服务协议》与《隐私政策》各条款，包括但不限于：为了更好的向您提供服务，我们需要访问您的相册、相机等。您可以阅读《隐私政策》了解详细信息。如果您同意，请点击下面同意按钮开始接受我们的服务。"
                    val annotatedText = buildAnnotatedString {
                        append(agreement)
                        val startIndexForService = agreement.indexOf("《服务协议》")
                        val startIndexPrivacy1 = agreement.indexOf("《隐私政策》")
                        val startIndexPrivacy2 = agreement.lastIndexOf("《隐私政策》")

                        val serviceLength = "《服务协议》".length
                        val privacyLength = "《服务协议》".length

                        // 高亮显示 "《服务协议》"
                        addStyle(
                            style = SpanStyle(
                                color = Color(0xFF0066CC),
                                textDecoration = TextDecoration.None
                            ),
                            start = startIndexForService, //  "《服务协议》" 开始下标13
                            end = startIndexForService + serviceLength //  "《服务协议》" 结束下标19
                        )
                        addStringAnnotation(
                            tag = "AGREEMENT",
                            annotation = "service_agreement",
                            start = startIndexForService,
                            end = startIndexForService + serviceLength
                        )

                        // 高亮显示 "《隐私政策》"
                        addStyle(
                            style = SpanStyle(
                                color = Color(0xFF0066CC),
                                textDecoration = TextDecoration.None
                            ),
                            start = startIndexPrivacy1, //  "《隐私政策》" 开始下标20
                            end = startIndexPrivacy1 + privacyLength //  "《隐私政策》" 结束下标26
                        )
                        addStringAnnotation(
                            tag = "PRIVACY",
                            annotation = "privacy_policy",
                            start = startIndexPrivacy1,
                            end = startIndexPrivacy1 + privacyLength
                        )

                        // 高亮显示 "《隐私政策》"
                        addStyle(
                            style = SpanStyle(
                                color = Color(0xFF0066CC),
                                textDecoration = TextDecoration.None
                            ),
                            start = startIndexPrivacy2, //  "《隐私政策》" 开始下标
                            end = startIndexPrivacy2 + privacyLength //  "《隐私政策》" 结束下标
                        )
                        addStringAnnotation(
                            tag = "PRIVACY",
                            annotation = "privacy_policy",
                            start = startIndexPrivacy2,
                            end = startIndexPrivacy2 + privacyLength
                        )
                    }

                    ClickableText(
                        text = annotatedText,
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .align(Alignment.CenterHorizontally)
                            .padding(horizontal = 12.dp),
                        style = TextStyle(
                            fontWeight = FontWeight.Normal,
                            fontSize = 12.sp,
                            color = Color(0xFF1A1A1A)
                        ),
                        onClick = { offset ->
                            val agreementAnnotation = annotatedText.getStringAnnotations("AGREEMENT", offset, offset).firstOrNull()
                            val privacyAnnotation = annotatedText.getStringAnnotations("PRIVACY", offset, offset).firstOrNull()

                            when {
                                agreementAnnotation != null -> {
                                    openAgreement(context, "服务协议", agreementUrl, false)
                                }
                                privacyAnnotation != null -> {
                                    openAgreement(context, "隐私政策", privacyUrl, false)
                                }
                            }
                        }
                    )

                }

                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .wrapContentHeight()
                        .align(Alignment.BottomCenter)
                ) {
                    //同意按钮，用户协议与隐私政策
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .padding(start = 33.dp, end = 33.dp)
                            .background(
                                Color(0xFF252525),
                                shape = RoundedCornerShape(359.dp),
                            )
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                onAgreementChange(true)
                                viewModel.setIsPolicyAgreement(true)
                            }
                    ) {
                        Text(
                            "同意",
                            color = Color(0xFFC2FF43),
                            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)
                            .background(
                                Color(0x00000000),
                                shape = RoundedCornerShape(359.dp),
                            )
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                onAgreementChange(false)
                                viewModel.setIsPolicyAgreement(false)
                            }
                    ) {
                        Text(
                            "不同意",
                            color = Color(0xFFAAAAAA),
                            fontSize = 14.sp,
                            fontWeight = FontWeight.Bold,
                            modifier = Modifier
                                .wrapContentWidth()
                                .wrapContentHeight()
                                .padding(vertical = 12.dp)
                                .align(Alignment.Center)
                        )
                    }
                }
            }
        }
    }
}



@Composable
private fun UpdateDialog(
    title: String = "新版本，更新提示",
    newVersion: String = "1.0.0",
    desc: String = "修复了一些问题，新增了一些功能",
    url: String,
    onStatusChange: (state: Boolean) -> Unit
){
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color(0x80000000))
            .clickable(
                indication = null,
                interactionSource = remember { MutableInteractionSource() }
            ) {
                onStatusChange(false)
            },
        verticalArrangement = Arrangement.Center
    ){
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight()
                .padding(horizontal = 36.dp)
                .background(Color.White, shape = RoundedCornerShape(26.dp))
                .align(Alignment.CenterHorizontally)
                .clickable(
                    indication = null,
                    interactionSource = remember { MutableInteractionSource() }
                ) {
                    //什么都不用做，只是解决点击穿透问题
                }
        ) {
            Image(
                painter = painterResource(id = R.mipmap.ic_dialog_top_mask1),
                contentDescription = null,
                contentScale = ContentScale.FillWidth,
                modifier = Modifier
                    .fillMaxWidth()
            )

            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight()
                    .padding(top = 46.dp)
            ) {
                Text(
                    text = title,
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color(0xFF1A1A1A),
                    modifier = Modifier
                        .wrapContentSize()
                        .align(Alignment.CenterHorizontally)
                )

                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(14.dp)
                )

                Text(
                    text = newVersion,
                    fontSize = 12.sp,
                    fontWeight = FontWeight.Normal,
                    color = Color(0xFF767676),
                    modifier = Modifier
                        .wrapContentSize()
                        .align(Alignment.CenterHorizontally)
                )

                Text(
                    text = desc,
                    fontSize = 12.sp,
                    fontWeight = FontWeight.Normal,
                    color = Color(0xFF767676),
                    modifier = Modifier
                        .wrapContentSize()
                        .align(Alignment.CenterHorizontally)
                )


                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(16.dp)
                )

                //切换/退出账号
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(start = 18.dp, end = 18.dp, bottom = 20.dp)
                ) {
                    //取消解绑手机号
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .weight(1f)
                            .background(
                                Color(0x00000000),
                                shape = RoundedCornerShape(359.dp),
                            )
                            .border(
                                width = 1.dp,
                                color = Color(0xFF000000),
                                shape = RoundedCornerShape(359.dp)
                            )
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                // 取消解绑手机号
                                onStatusChange(false)
                            }
                    ) {
                        Text(
                            "取消",
                            color = Color(0xFF1A1A1A),
                            fontSize = 16.sp,
                            fontWeight = FontWeight.Bold,
                            modifier = Modifier
                                .wrapContentWidth()
                                .wrapContentHeight()
                                .padding(vertical = 12.dp)
                                .align(Alignment.Center)
                        )
                    }

                    Box(
                        modifier = Modifier
                            .width(11.dp)
                    )



                    //退出登录
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .weight(1f)
                            .background(
                                Color(0xFF252525),
                                shape = RoundedCornerShape(359.dp),
                            )
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                //TODO 确定解绑手机号
                                onStatusChange(false)
                            }
                    ) {
                        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
fun MainScreenPreview() {
    AppTheme {
        MainScreen(generalViewModel = viewModel(), loginViewModel = viewModel())
    }
}