package com.img.rabbit.pages

import androidx.compose.animation.core.Animatable
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.img.rabbit.R
import com.img.rabbit.utils.NetworkStatus

@Composable
fun NetworkDisconnectedPage(onNetworkStatus: (Boolean) -> Unit) {
    val context = LocalContext.current
    val scale = remember { Animatable(0f) }

    Box(
        modifier = Modifier.fillMaxSize().background(color = Color.White)
    ) {
        Image(
            painter = painterResource(id = R.mipmap.ic_normal_top_mask),
            contentDescription = null,
            contentScale = ContentScale.FillWidth,
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight()
        )

        /*
        // 返回按钮
        Image(
            painter = painterResource(id = R.mipmap.ic_back),
            contentDescription = null,
            modifier = Modifier
                .padding(top = 56.dp, start = 16.dp)
                .wrapContentSize()
                .clickable {
                    // 点击返回按钮，返回上一页
                    (context as MainActivity).onBackPressed()
                    //navController.popBackStack()
                }
        )
         */

        Column(
            modifier = Modifier
                .fillMaxSize()
        ) {

            Image(
                painter = painterResource(id = R.mipmap.ic_network_disconnected),
                contentDescription = null,
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight()
                    .padding(top = 253.dp)
            )
            Text(
                text = "网络连接断开，请检查网络设置",
                fontSize = 16.sp,
                fontWeight = FontWeight.Normal,
                color = Color(0xFF1A1A1A),
                modifier = Modifier
                    .wrapContentWidth()
                    .wrapContentHeight()
                    .align(Alignment.CenterHorizontally)
                    .padding(top = 23.dp)
            )

             Text(
                text = "刷新页面",
                fontSize = 16.sp,
                fontWeight = FontWeight.Normal,
                color = Color(0xFFAAAAAA),
                modifier = Modifier
                    .wrapContentWidth()
                    .wrapContentHeight()
                    .align(Alignment.CenterHorizontally)
                    .padding(top = 57.dp)
                    .clickable(
                        indication = null,
                        interactionSource = remember { MutableInteractionSource() }
                    ) {
                        // 点击刷新按钮，刷新页面
                        onNetworkStatus(NetworkStatus.isNetworkAvailable(context))
                    }
            )

            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight()
                    .padding(horizontal = 71.dp)
                    .align(Alignment.CenterHorizontally)
                    .padding(top = 11.dp)
                    .background(color = Color(0xFF252525), shape = RoundedCornerShape(359.dp))
                    .clickable(
                        indication = null,
                        interactionSource = remember { MutableInteractionSource() }
                    ) {
                        // 点击开启网络权限按钮，跳转至系统设置页面
                        NetworkStatus.openNetworkSettings(context)
                    }
            ){
                Text(
                    text = "开启网络权限",
                    fontSize = 16.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color(0xFFC2FF43),
                    modifier = Modifier
                        .wrapContentWidth()
                        .wrapContentHeight()
                        .align(Alignment.CenterHorizontally)
                        .padding(top = 12.dp, bottom = 12.dp)
                )
            }

        }
    }

}

@Preview
@Composable
private fun PreviewNetworkDisconnectedPage() {
    NetworkDisconnectedPage(onNetworkStatus = {})
}
