package com.vpn.fastestvpnservice.customItems

import android.content.res.Configuration
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.BorderStroke
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.Row
import androidx.compose.foundation.layout.Spacer
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.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
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.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.draw.clip
import androidx.compose.ui.draw.paint
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.layout.ContentScale
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.stealthcopter.networktools.Ping
import com.stealthcopter.networktools.ping.PingResult
import com.stealthcopter.networktools.ping.PingStats
import com.vpn.fastestvpnservice.R
import com.vpn.fastestvpnservice.beans.Server
import com.vpn.fastestvpnservice.beans.ServerList
import com.vpn.fastestvpnservice.beans.favList
import com.vpn.fastestvpnservice.beans.favListServer
import com.vpn.fastestvpnservice.helpers.UIHelper
import com.vpn.fastestvpnservice.utils.Utils
import com.vpn.fastestvpnservice.viewmodels.SearchListViewModel
import com.vpn.fastestvpnservice.viewmodels.ServerListViewModel
import com.vpn.fastestvpnservice.viewmodels.SplashViewModel
import java.lang.Exception

@Composable
fun ServerItem(server: Server) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(color = MaterialTheme.colorScheme.background)
            .padding(bottom = 1.dp)
    ) {
        val context = LocalContext.current
        val serverListViewModel: ServerListViewModel = viewModel{
            ServerListViewModel(context)
        }
        val splashViewModel: SplashViewModel = viewModel{
            SplashViewModel(context)
        }

        Row(
            verticalAlignment = Alignment.Top,
            horizontalArrangement = Arrangement.Start,
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 12.dp, end = 7.dp, top = 12.dp)
                .clickable(
                    indication = null,
                    interactionSource = remember { MutableInteractionSource() }
                ) {
                    Toast
                        .makeText(
                            context,
                            server.server_name,
                            Toast.LENGTH_SHORT
                        )
                        .show()
                }
        ) {
            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 {
                            ping = pingResult?.timeTaken?.toInt()!!
                        }
                    }

                    override fun onError(e: Exception?) {}
                    override fun onFinished(pingStats: PingStats?) {}
                }
            )
            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.server_name!!,
                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 = "${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)
            )
//            Spacer(modifier = Modifier.weight(1F))
            var isFavorite by rememberSaveable { mutableStateOf(server.isFavourited) }

            IconButton(
                modifier = Modifier
                    .padding(bottom = 18.dp, end = 8.dp)
                    .size(22.dp, 21.dp),
                onClick = {
                    isFavorite = !isFavorite!!
                    serverListViewModel.favAndUnFav(server)
//                    serverListViewModel.updateFavServer(server)
                }
            ) {
                val favResponse = serverListViewModel.mutableLiveDataFavUnFav.observeAsState().value
                favResponse?.let {
                    Log.d("test_fav_response", it.message.toString())
                    splashViewModel.serverDataApi()
                    serverListViewModel.mutableLiveDataFavUnFav.value = null
                }
                Icon(
                    modifier = Modifier
//                        .padding(bottom = 18.dp, end = 8.dp)
//                        .size(22.dp, 21.dp)
//                        .clickable(
//                            indication = null,
//                            interactionSource = remember { MutableInteractionSource() }
//                        ) {
////                            isFavorite = !isFavorite!!
////                        serverListViewModel.updateFavServer(server)
////                        serverListViewModel.favAndUnFav(server)
//                        }
                        ,

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

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


//            IconButton(
//                onClick = { isFavorite = !isFavorite },
//                modifier = Modifier
//                    .clickable(
//                        indication = null,
//                        interactionSource = remember { MutableInteractionSource() },
//                        ) {
//                        isFavorite = !isFavorite
//                    }
//            ) {
//
//
//            }

        }

        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 FavoriteServerItem(server: Server) {
    val context = LocalContext.current
    var serverListViewModel: ServerListViewModel = viewModel {
        ServerListViewModel(context = context)
    }
    val splashViewModel: SplashViewModel = viewModel{
        SplashViewModel(context)
    }

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(MaterialTheme.colorScheme.background)
            .padding(bottom = 1.dp)
    ) {
        Row(
            verticalAlignment = Alignment.Top,
            horizontalArrangement = Arrangement.Start,
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 12.dp, end = 7.dp, top = 12.dp)
                .clickable(
                    indication = null,
                    interactionSource = remember { MutableInteractionSource() }
                ) {
                    Toast
                        .makeText(
                            context,
                            server.server_name,
                            Toast.LENGTH_SHORT
                        )
                        .show()
                }
        ) {
            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 {
                            ping = pingResult?.timeTaken?.toInt()!!
                        }
                    }

                    override fun onError(e: Exception?) {}
                    override fun onFinished(pingStats: PingStats?) {}
                }
            )
            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)
                    .clip(CircleShape)
                    .paint(
                        painter = painterResource(id = icon),
                        contentScale = ContentScale.FillHeight
                    )

            )

            Text(text = server.server_name!!,
                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 = "${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)
            )
//            Spacer(modifier = Modifier.weight(1F))
            var isFavorite by rememberSaveable { mutableStateOf(server.isFavourited) }



            IconButton(
                onClick = {
                    isFavorite = !isFavorite!!
                    serverListViewModel.favAndUnFav(server)
                          },
                modifier = Modifier
                    .padding(bottom = 18.dp, end = 0.dp)
                    .size(22.dp, 21.dp)
//                    .clickable(
//                        indication = null,
//                        interactionSource = remember { MutableInteractionSource() }
//                    ) {  },
            ) {

                val favResponse = serverListViewModel.mutableLiveDataFavUnFav.observeAsState().value
                favResponse?.let {
                    Log.d("test_fav_response", it.message.toString())
                    splashViewModel.serverDataApi()
                    serverListViewModel.mutableLiveDataFavUnFav.value = null
                }

                Icon(

                    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 ServerSearchItem(server: Server) {
    val context = LocalContext.current
    var serverListViewModel: ServerListViewModel = viewModel {
        ServerListViewModel(context = context)
    }
    val splashViewModel: SplashViewModel = viewModel{
        SplashViewModel(context)
    }
    val searchListViewModel: SearchListViewModel = viewModel{
        SearchListViewModel(context, serverListViewModel, splashViewModel)
    }
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(color = Color.White)
            .padding(bottom = 1.dp)
    ) {

        Row(
            verticalAlignment = Alignment.Top,
            horizontalArrangement = Arrangement.Start,
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 12.dp, end = 7.dp, top = 12.dp)
                .clickable(
                    indication = null,
                    interactionSource = remember { MutableInteractionSource() }
                ) {
                    Toast
                        .makeText(
                            context,
                            server.server_name,
                            Toast.LENGTH_SHORT
                        )
                        .show()
                }
        ) {
            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 {
                            ping = pingResult?.timeTaken?.toInt()!!
                        }
                    }

                    override fun onError(e: Exception?) {}
                    override fun onFinished(pingStats: PingStats?) {}
                }
            )
            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.server_name!!,
                style = TextStyle(
                    fontSize = 16.sp,
                    color = colorResource(id = R.color.dark_blue_gray_text)
                ),
                modifier = Modifier
                    .padding(start = 16.dp, bottom = 18.dp)
                    .align(Alignment.CenterVertically)
            )
            Spacer(modifier = Modifier.weight(1F))
            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)
            )
//            Spacer(modifier = Modifier.weight(1F))
            var isFavorite by rememberSaveable { mutableStateOf(server.isFavourited) }

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

                val serversApiResponse = splashViewModel.mutableLiveDataServerData.observeAsState().value
                serversApiResponse?.let {
                    serverListViewModel._mutableLiveDataGetFavList.value =
                        serverListViewModel.getFavList()
                }


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

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



//            IconButton(
//                onClick = { isFavorite = !isFavorite },
//                modifier = Modifier
//                    .clickable(
//                        indication = null,
//                        interactionSource = remember { MutableInteractionSource() },
//                        ) {
//                        isFavorite = !isFavorite
//                    }
//            ) {
//
//
//            }

        }

        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)
        ) {}
    }
}

@Preview
@Composable
fun ServerItemPreview() {
    ServerItem(server = favListServer[0])
}

@Preview
@Composable
fun FavoriteServerItemPreview() {
    FavoriteServerItem(server = favListServer[0])
}

@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun FavoriteServerItemPreviewDark() {
    FavoriteServerItem(server = favListServer[0])
}

@Preview
@Composable
fun ServerSearchItemPreview() {
    ServerSearchItem(server = favListServer[0])
}