package com.vpn.fastestvpnservice.screens.helpScreensAll

import android.graphics.Bitmap
import android.util.Log
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
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.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
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.compose.ui.viewinterop.AndroidView
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.vpn.fastestvpnservice.R
import com.vpn.fastestvpnservice.constants.TermsAndConditionsDesc
import com.vpn.fastestvpnservice.sealedClass.BottomBarScreen
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun About(navHostController: NavHostController) {
    val configuration = LocalConfiguration.current
    val screenHeight = configuration.screenHeightDp.dp
    val screenWidth = configuration.screenWidthDp.dp

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(
                colorResource(id = R.color.white)
            )
            .fillMaxSize()
    ) {
        var showLoader by remember { mutableStateOf(true) }

            ShowHeaderAbout(navHostController = navHostController)

            Box(modifier = Modifier
                .padding(top = 60.dp)
                .fillMaxSize()
                .background(Color.Transparent)
            ) {

                val aboutUrl = "https://fastestvpn.com/about?device=ios"
                AndroidView(factory = {
                    WebView(it).apply {
                        layoutParams = ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT
                        )
                        webViewClient = WebViewClient()

                    settings.javaScriptEnabled = true

                        webViewClient = object : WebViewClient() {
                            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                                super.onPageStarted(view, url, favicon)
                                Log.d("test_webview", "onPageStarted")
                                showLoader = true
                            }

                            override fun onPageFinished(view: WebView?, url: String?) {
                                super.onPageFinished(view, url)
                                Log.d("test_webview", "onPageFinished")
                                showLoader = false
                            }
                        }

//                        loadUrl(aboutUrl)
                    }
                },
                    update = {
                        Log.d("test_webview", "update")
                        it.loadUrl(aboutUrl)
                    }
                )

                if (showLoader) {
                    var progress by remember { mutableFloatStateOf(0.1F) }

                    LaunchedEffect(key1 = Unit) {
                        for (i in 1..100) {
                            progress = i.toFloat()/100F
                            delay(50)
                        }
                    }

                    CircularProgressIndicator(
                        progress = progress,
                        modifier = Modifier
                            .align(Alignment.Center)
                            .size(50.dp),
                        colorResource(id = R.color.dark_blue_gray_text),
                        strokeWidth = 5.dp,
                    )
                }
            }
    }
}

@Composable
fun BoxScope.ShowHeaderAbout(navHostController: NavHostController) {
    IconButton(
        onClick = {
            navHostController.popBackStack()
            navHostController.popBackStack()
//                    navHostController.navigate(BottomBarScreen.Help.route)
        },
        modifier = Modifier
            .align(Alignment.TopStart)
            .padding(top = 10.dp)
            .padding(start = 16.dp)
            .size(30.dp, 32.dp)
    ) {
        Icon(
            painter = painterResource(id = R.drawable.backarrow3x),
            contentDescription = "Arrow-Back",
            tint = colorResource(id = R.color.dark_blue_gray_text),
            modifier = Modifier.size(18.dp, 12.dp)
        )
    }
    Surface(
        modifier = Modifier
            .padding(top = 10.dp)
            .height(32.dp)
            .align(Alignment.TopCenter)
            .padding(5.dp),
        color = colorResource(id = R.color.transparent)
    ) {
        Text(text = "About Fastest VPN",
            style = TextStyle(
                fontSize = 18.sp,
                color = colorResource(id = R.color.dark_blue_gray_text),
                textAlign = TextAlign.Center,
                fontWeight = FontWeight.Medium
            ),
            modifier = Modifier.fillMaxHeight()

        )
    }

//    Column(
//        modifier = Modifier
//            .fillMaxWidth()
//            .fillMaxHeight()
//            .padding(top = 100.dp)
//            .padding(16.dp)
//            .background(Color.Transparent)
//            .verticalScroll(rememberScrollState())
//    ) {
//        Text(text = TermsAndConditionsDesc,
//            style = TextStyle(
//                fontSize = 14.sp,
//                lineHeight = 26.sp,
//                color = MaterialTheme.colorScheme.primary
//            )
//        )
//    }
}

@Preview
@Composable
fun AboutPreview() {
    About(rememberNavController())
}