package com.jihe.punchnet.screen

import android.widget.Toast
import androidx.collection.emptyLongSet
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
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.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.room.util.TableInfo
import com.jihe.punchnet.data.RouteItem
import com.jihe.punchnet.data.RouteViewModel
import com.jihe.punchnet.helper.IntToIPString
import com.jihe.punchnet.helper.parseCIDRAndGW
import com.jihe.punchnet.sdlan.network.maskIPToDigit

@Composable
fun AddOrModifyRoute(
    routeViewModel: RouteViewModel,
    initRoute: MutableState<RouteItem?>,
    shouldShowRoute: MutableState<Boolean>,

) {
    if (shouldShowRoute.value) {
        val context = LocalContext.current
        var cidr by remember {
            mutableStateOf(
                if (initRoute.value == null)
                    ""
                else
                    ""
            )
        }

        var gw by remember {
            mutableStateOf(
                if (initRoute.value == null)
                    ""
                else
                    ""
            )
        }

        AlertDialog(
            onDismissRequest = {
                shouldShowRoute.value = false
            },
            confirmButton = {
                Button(
                    onClick = {
                        val item = parseCIDRAndGW(cidr, gw)
                        if (item == null) {
                            Toast.makeText(context, "数据解析出错", Toast.LENGTH_SHORT).show()
                        } else {
                            routeViewModel.insert(item)
                            shouldShowRoute.value = false
                        }
                    }
                ) {
                    Text("确定")
                }
            },
            title = {
                Text(
                    "添加新路由"
                )
            },
            text = {
                Column {
                    TextField(
                        value = cidr,
                        onValueChange = { newValue ->
                            cidr = newValue
                        },
                        placeholder = {
                            Text(
                                "192.168.0.0/24",
                                color = MaterialTheme.colorScheme.onBackground.copy(.5f)
                            )
                        },
                        label = {
                            Text("CIDR")
                        },
                        modifier = Modifier.padding(start = 16.dp)
                    )


                    TextField(
                        value = gw,
                        onValueChange = { newValue ->
                            gw = newValue
                        },
                        placeholder = {
                            Text(
                                "10.167.69.13",
                                color = MaterialTheme.colorScheme.onBackground.copy(.5f)
                            )
                        },
                        label = {
                            Text("gateway")
                        },
                        modifier = Modifier.padding(start = 16.dp, top = 8.dp)
                    )
                }
            }
        )
    }
}


@Composable
fun RouteScreen(
    enabled: Boolean,
    routeViewModel: RouteViewModel,
    modifier: Modifier = Modifier,
) {
    val showAddRoute: MutableState<RouteItem?> = remember { mutableStateOf(null) }
    val shouldShow: MutableState<Boolean> = remember { mutableStateOf(false) }

    AddOrModifyRoute(routeViewModel, showAddRoute, shouldShow)

    val routeItems by routeViewModel.allRoutes.collectAsState(initial = emptyList())
    LazyColumn {
        item {
            Row(
            ) {

            }
        }
        items(routeItems) {item ->
            RouteCard(routeViewModel, item, enabled)
        }
        item {
            Button(
                enabled = enabled,
                shape = RectangleShape,
                onClick = {
                    shouldShow.value = true
                },
                modifier = Modifier.fillMaxWidth()
            ) {
                Text("添加路由")
            }
        }
    }
}

@Composable
fun RouteCard(
    routeViewModel: RouteViewModel,
    item: RouteItem,
    enabled: Boolean,
    modifier: Modifier = Modifier,
) {
    Card (
        modifier = Modifier.fillMaxWidth()

    ){
        Row (
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.height(36.dp)
        ){
            Icon(
                Icons.Default.Clear,
                contentDescription = "delete route",
                modifier = Modifier.size(30.dp).clickable(
                    enabled = enabled,
                ) {
                    routeViewModel.deleteById(item.id)
                }
            )
            Text(
                "${IntToIPString(item.net_ip)}/${maskIPToDigit(item.mask_ip)}",
                modifier = Modifier.padding(start = 8.dp)
            )

            Text(
                IntToIPString(item.gateway),
                modifier = Modifier.padding(start = 8.dp)
            )

        }
    }
}