package com.vpn.fastestvpnservice.customItems

import android.content.res.Configuration
import android.widget.Toast
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.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.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.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.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
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 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.utils.Utils

@Composable
fun ServerItem(server: ServerList) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(color = MaterialTheme.colorScheme.background)
            .padding(bottom = 1.dp)
    ) {
        val context = LocalContext.current

        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.ServerListName,
                            Toast.LENGTH_SHORT
                        )
                        .show()
                }
        ) {
            Icon(
                painter = painterResource(id = server.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 = server.ping,
                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(false) }

            Icon(
                modifier = Modifier
                    .padding(bottom = 18.dp, end = 8.dp)
                    .size(22.dp, 21.dp)
                    .clickable(
                        indication = null,
                        interactionSource = remember { MutableInteractionSource() }
                    ) { isFavorite = !isFavorite },

                painter = if (isFavorite) 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) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(MaterialTheme.colorScheme.background)
            .padding(bottom = 1.dp)
    ) {
        val context = LocalContext.current

        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()
                }
        ) {
            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 = server.name!!,
                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(true) }

            Icon(
                modifier = Modifier
                    .padding(bottom = 18.dp, end = 0.dp)
                    .size(22.dp, 21.dp)
                    .clickable(
                        indication = null,
                        interactionSource = remember { MutableInteractionSource() }
                    ) { isFavorite = !isFavorite },

                painter = if (isFavorite) 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 ServerSearchItem(server: ServerList) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(color = MaterialTheme.colorScheme.background)
            .padding(bottom = 1.dp)

    ) {
        val context = LocalContext.current

        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.ServerListName,
                            Toast.LENGTH_SHORT
                        )
                        .show()
                }
        ) {
            Icon(
                painter = painterResource(id = server.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 = server.ping,
                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(false) }

            Icon(
                modifier = Modifier
                    .padding(bottom = 18.dp, end = 8.dp)
                    .size(22.dp, 21.dp)
                    .clickable(
                        indication = null,
                        interactionSource = remember { MutableInteractionSource() }
                    ) { isFavorite = !isFavorite },

                painter = if (isFavorite) 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 = favList[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 = favList[0])
}