package com.vpn.fastestvpnservice.customItems

import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.background
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.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
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 com.vpn.fastestvpnservice.R
import com.vpn.fastestvpnservice.beans.CountryServerList
import com.vpn.fastestvpnservice.beans.Server
import com.vpn.fastestvpnservice.beans.allLocationsList
import com.vpn.fastestvpnservice.beans.favList
import com.vpn.fastestvpnservice.beans.favListServer
import com.vpn.fastestvpnservice.utils.Utils
import com.stealthcopter.networktools.Ping
import com.stealthcopter.networktools.ping.PingResult
import com.stealthcopter.networktools.ping.PingStats
import com.vpn.fastestvpnservice.viewmodels.ServerListViewModel
import com.vpn.fastestvpnservice.viewmodels.SplashViewModel
import java.lang.Exception
import java.util.logging.Handler

@Composable
fun CountryItem(server: Server, category: String) {
    Log.d("test_countrySize", "CountryItem Called")
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(
                color = MaterialTheme.colorScheme.background
            )
            .padding(bottom = 1.dp)
    ) {
        val context = LocalContext.current
        var countrySize by rememberSaveable { mutableStateOf(server.countryServers) }
        var isServerExpanded by rememberSaveable { mutableStateOf(false) }
        val serverListViewModel: ServerListViewModel = viewModel{
            ServerListViewModel(context)
        }
        val splashViewModel: SplashViewModel = viewModel{
            SplashViewModel(context)
        }
//        var countrySize: Int = 0

        Log.d("test_countrySize", "countrySize $countrySize")

//        LaunchedEffect(key1 = Unit) {
//            Log.d("test_countrySize", "countrySize LE() $countrySize")
//            if (countrySize == 0) {
//                Log.d("test_countrySize", "countrySize==0 $countrySize")
//                countrySize = server.totalServers
//            }
//        }

            if (countrySize != null){
                countrySize.let { size ->
                    if (size?.size!! > 1) {
                        Column {
                            /* Country Location's Row */
                            Row(
                                verticalAlignment = Alignment.Top,
                                horizontalArrangement = Arrangement.Start,
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(
                                        start = 16.dp, end = 12.dp, top = 12.dp
                                    )
                                    .clickable(
                                        indication = null,
                                        interactionSource = remember { MutableInteractionSource() }
                                    ) {
                                        Toast
                                            .makeText(
                                                context,
                                                server.country + " " + server.ping,
                                                Toast.LENGTH_SHORT
                                            )
                                            .show()
                                    }
                            ) {
                                val icon = Utils.getDrawable(context, server.iso)
                                Icon(
                                    painter = painterResource(id = icon),
                                    contentDescription = "Server Logo",
                                    tint = Color.Unspecified,
                                    modifier = Modifier
                                        .padding(bottom = 16.dp)
                                        .size(24.dp)
                                )
                                Text(text = server.country!!,
                                    style = TextStyle(
                                        fontSize = 16.sp,
                                        color = MaterialTheme.colorScheme.primary
                                    ),
                                    modifier = Modifier
                                        .padding(start = 16.dp, bottom = 18.dp)
                                        .align(Alignment.CenterVertically)
                                )
                                Spacer(modifier = Modifier.weight(1F))
                                Text(text = "${countrySize?.size} locations",
                                    style = TextStyle(
                                        fontSize = 16.sp,
                                        color = colorResource(id = R.color.blue_text)
                                    ),
                                    modifier = Modifier
                                        .padding(end = 30.dp, bottom = 18.dp)
                                        .align(Alignment.CenterVertically)
                                )
//            Spacer(modifier = Modifier.weight(1F))

                                Icon(
                                    modifier = Modifier
                                        .padding(bottom = 18.dp, end = 0.dp)
                                        .size(12.dp, 7.dp)
                                        .align(Alignment.CenterVertically)
                                        .clickable(
                                            indication = null,
                                            interactionSource = remember { MutableInteractionSource() }
                                        ) { isServerExpanded = !isServerExpanded },

                                    painter = if (isServerExpanded) painterResource(
                                        id = R.drawable.dragarrow3x) else painterResource(
                                        id = R.drawable.downarrow3x),

                                    contentDescription = "Server Logo",
                                    tint = MaterialTheme.colorScheme.primary,
                                )
                            }

                            /* Country Expandable's Row */
                            if (isServerExpanded) {


                                ExpandableRow(server = server)
                            }
                        }
                    }
                    else {
                        Log.d("test_return_ping","C[1]=> " + server.server_name + server.ping)

//                    Log.d("test_servers_count", "${server.server_name} ${server.countryServers?.size}")
                        /* Country Location's Row -> 1 Location() */

//                        var ping by remember { mutableIntStateOf(0) }
                        Ping.onAddress(server.ip as String).setTimeOutMillis(1000).doPing(
                            object : Ping.PingListener{
                                override fun onResult(pingResult: PingResult?) {
                                    android.os.Handler(Looper.getMainLooper()).post {
                                        val ping = pingResult?.timeTaken?.toInt()!!
                                        server.ping = ping
                                        Log.d("test_ping", "ping = $ping")
                                    }
                                }

                                override fun onError(e: Exception?) {}
                                override fun onFinished(pingStats: PingStats?) {}
                            }
                        )

                        Row(
                            verticalAlignment = Alignment.Top,
                            horizontalArrangement = Arrangement.Start,
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(start = 16.dp, end = 7.dp, top = 12.dp)
                                .clickable(
                                    indication = null,
                                    interactionSource = remember { MutableInteractionSource() }
                                ) {
                                    Toast
                                        .makeText(
                                            context,
                                            server.country + " " + server.ping,
                                            Toast.LENGTH_SHORT
                                        )
                                        .show()
                                }
                        ) {
                            val icon = Utils.getDrawable(context, server.iso)
                            Icon(
                                painter = painterResource(id = icon),
                                contentDescription = "Server Logo",
                                tint = Color.Unspecified,
                                modifier = Modifier
                                    .padding(bottom = 16.dp)
                                    .size(24.dp)
                            )
                            val serverTitle = if (category.lowercase().toString() == "servers") server.country else server.server_name
                            Text(text = serverTitle!! + server.ping,
                                style = TextStyle(
                                    fontSize = 16.sp,
                                    color = MaterialTheme.colorScheme.primary
                                ),
                                modifier = Modifier
                                    .padding(start = 16.dp, bottom = 18.dp)
                                    .align(Alignment.CenterVertically)
                            )
                            Spacer(modifier = Modifier.weight(1F))
                            val pings = server.ping
                            Text(
                                text = "${server.ping} ms",
                                style = TextStyle(
                                    fontSize = 16.sp,
                                    color = colorResource(id = R.color.blue_text)
                                ),
                                modifier = Modifier
                                    .padding(end = 30.dp, bottom = 18.dp)
                                    .align(Alignment.CenterVertically)
                            )
                            var isFavorite by rememberSaveable { mutableStateOf(server.isFavourited) }

                            IconButton(
                                modifier = Modifier
                                    .padding(bottom = 18.dp, end = 8.dp)
                                    .size(21.dp, 20.dp)
                                    .align(Alignment.CenterVertically),
                                onClick = {
                                    isFavorite = !isFavorite!!
                                    serverListViewModel.favAndUnFav(server)
                                }
                            )
                            {
                                val favResponse = serverListViewModel.mutableLiveDataFavUnFav.observeAsState().value
                                favResponse?.let {
                                    Log.d("test_fav_response", it.message.toString())
                                    splashViewModel.serverDataApi()
                                    serverListViewModel.mutableLiveDataFavUnFav.value = null
                                }

                                Icon(
//                                    .clickable(
//                                        indication = null,
//                                        interactionSource = remember { MutableInteractionSource() }
//                                    ) {  },

                                    painter = if (isFavorite == true) painterResource(
                                        id = R.drawable.fav_server3x) else painterResource(
                                        id = R.drawable.unfav_server3x),

                                    contentDescription = "Server Logo",
                                    tint = Color.Unspecified,
                                )
                            }
                        }
                    }
                }
            }

            Surface(
                modifier = Modifier
                    .padding(start = 0.dp, end = 0.dp)
                    .height(1.dp)
                    .fillMaxWidth()
                    .alpha(0.6F)
                    .align(Alignment.BottomCenter)
                ,
                color = colorResource(id = R.color.gray_icon)
            ) {}

    }
}

@Composable
fun ColumnScope.ExpandableRow(server: Server) {
    val context = LocalContext.current
    val serverListViewModel: ServerListViewModel = viewModel{
        ServerListViewModel(context)
    }
    val splashViewModel: SplashViewModel = viewModel{
        SplashViewModel(context)
    }

    Column(
        modifier = Modifier
            .padding(
                start = 40.dp, end = 26.dp,
                top = 2.dp, bottom = 28.dp
            )
            .background(Color.Transparent)
    ) {

//        var ping: Int = 0


        // Expandable Row

        server.countryServers?.let { serverData ->
                serverData.forEachIndexed { index, serverInfo ->
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.Start,
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(
                                vertical = 15.dp
                            )
                            .background(Color.Transparent)
                            .clickable(
                                indication = null,
                                interactionSource = remember { MutableInteractionSource() }
                            ) {
                                Toast
                                    .makeText(
                                        context,
                                        serverInfo.server_name + " " + serverInfo.ping,
                                        Toast.LENGTH_SHORT
                                    )
                                    .show()
                            }
                    ) {
                        Text(text = serverInfo.server_name!!,
                            style = TextStyle(
                                fontSize = 16.sp,
                                color = MaterialTheme.colorScheme.onSecondary
                            ),
                            modifier = Modifier
                                .padding(start = 16.dp, bottom = 0.dp)
                                .align(Alignment.CenterVertically)
                                .alpha(0.4F)
                        )
                        Spacer(modifier = Modifier.weight(1F))

//                        serverListViewModel.calculatePing(server)

                        Text(text = "${serverInfo.ping} ms",
                            style = TextStyle(
                                fontSize = 16.sp,
                                color = colorResource(id = R.color.blue_text),
                                lineHeight = 30.sp,
                                textAlign = TextAlign.Right
                            ),
                            modifier = Modifier
                                .padding(start = 0.dp, end = 30.dp)
                                .align(Alignment.CenterVertically)
                        )
                        var isFavorite by rememberSaveable {
                            mutableStateOf(serverInfo.isFavourited)
                        }

                        IconButton(
                            modifier = Modifier
                                .padding(bottom = 0.dp, end = 16.dp)
                                .size(21.dp, 20.dp)
                                .align(Alignment.CenterVertically),
                            onClick = {
                            isFavorite = !isFavorite!!
                            serverListViewModel.favAndUnFav(serverInfo)
                        }
                        )
                        {

                            val favResponse = serverListViewModel.mutableLiveDataFavUnFav.observeAsState().value
                            favResponse?.let {
                                Log.d("test_fav_response", it.message.toString())
                                splashViewModel.serverDataApi()
                                serverListViewModel.mutableLiveDataFavUnFav.value = null
                            }
                            Icon(
//                                    .clickable(
//                                        indication = null,
//                                        interactionSource = remember { MutableInteractionSource() }
//                                    ) {  },

                                painter = if (isFavorite == true) painterResource(
                                    id = R.drawable.fav_server3x) else painterResource(
                                    id = R.drawable.unfav_server3x),

                                contentDescription = "Server Logo",
                                tint = Color.Unspecified,
                            )
                        }
                    }

                    Surface(
                        modifier = Modifier
                            .padding(top = 0.dp, end = 0.dp)
                            .height(1.dp)
                            .fillMaxWidth()
                            .alpha(0.6F)
                        ,
                        color = colorResource(id = R.color.gray_icon)
                    ) {}
                }
            }
    }
}

fun calculatePing(server: Server, onPingResult: (Int) -> Unit) {
    Ping.onAddress(server.ip as String).setTimeOutMillis(1000).doPing(
        object : Ping.PingListener{
            override fun onResult(pingResult: PingResult?) {
                android.os.Handler(Looper.getMainLooper()).post {
                    val ping = pingResult?.timeTaken?.toInt()!!
                    onPingResult(ping)
                    Log.d("test_ping", "ping[0] = $ping")
                }
            }

            override fun onError(e: java.lang.Exception?) {}
            override fun onFinished(pingStats: PingStats?) {}
        }
    )
}

@Preview
@Composable
fun CountryItemPreview() {
    CountryItem(server = favListServer[0], "servers")
}