package com.vpn.fastestvpnservice.customItems

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.material.IconButton
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
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.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.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.CountryServerList
import com.vpn.fastestvpnservice.beans.Server
import com.vpn.fastestvpnservice.beans.allLocationsList
import com.vpn.fastestvpnservice.beans.favList

@Composable
fun CountryItem(server: CountryServerList) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(
                color = MaterialTheme.colorScheme.background
            )
            .padding(bottom = 1.dp)

    ) {
        val context = LocalContext.current

        var countrySize by rememberSaveable { mutableStateOf(server.server?.size) }
        var isServerExpanded by rememberSaveable { mutableStateOf(true) }

        if (countrySize != null) {
            countrySize?.let { size ->
                if (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,
                                            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 = "$countrySize 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 {
                    /* Country Location's Row -> 1 Location() */
                    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.country,
                                        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))

                        var isFavorite by rememberSaveable { mutableStateOf(false) }

                        Icon(
                            modifier = Modifier
                                .padding(bottom = 18.dp, end = 8.dp)
                                .size(21.dp, 20.dp)
                                .align(Alignment.CenterVertically)
                                .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,
                        )
                    }
                }
            }

            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: CountryServerList) {
    val context = LocalContext.current

    Column(
        modifier = Modifier
            .padding(
                start = 40.dp, end = 26.dp,
                top = 2.dp, bottom = 28.dp
            )
            .background(Color.Transparent)
    ) {
            server.server?.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.ServerListName,
                                        Toast.LENGTH_SHORT
                                    )
                                    .show()
                            }
                    ) {
                        Text(text = serverInfo.ServerListName,
                            style = TextStyle(
                                fontSize = 16.sp,
                                color = MaterialTheme.colorScheme.primary
                            ),
                            modifier = Modifier
                                .padding(start = 16.dp, bottom = 0.dp)
                                .align(Alignment.CenterVertically)
                        )
                        Spacer(modifier = Modifier.weight(1F))

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

                        Icon(
                            modifier = Modifier
                                .padding(bottom = 0.dp, end = 16.dp)
                                .size(21.dp, 20.dp)
                                .align(Alignment.CenterVertically)
                                .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,
                        )
                    }

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

@Preview
@Composable
fun CountryItemPreview() {
    CountryItem(server = allLocationsList[0])
}