package me.eternal.purrfectsnap.ui.manager.pages.tracker

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
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.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material3.Card
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateMapOf
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.graphicsLayer
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.launch
import me.eternal.purrfectsnap.common.data.ExportedTrackerData
import me.eternal.purrfectsnap.ui.manager.Routes
import org.json.JSONArray

class FriendTrackerConfigImportScreen : Routes.Route() {
    override val translation by lazy { context.translation.getCategory("manager.friend_tracker_import") }

    @OptIn(ExperimentalMaterial3Api::class)
    override val content: @Composable (androidx.navigation.NavBackStackEntry) -> Unit = {
        val configJson = routes.friendTrackerConfigJsonForImport ?: ""
        val parser = remember { TrackerConfigParser(this) }
        val featuresByCategory = remember {
            parser.parse(configJson)
        }
        val expandedState = remember { mutableStateMapOf<String, Boolean>() }

        Scaffold(
            topBar = {
                TopAppBar(
                    title = { Text(translation["title"]) },
                    navigationIcon = {
                        IconButton(onClick = { routes.navController.popBackStack() }) {
                            Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = translation["back_button_description"])
                        }
                    },
                    actions = {
                        TextButton(onClick = {
                            runCatching {
                                val trackerData = context.gson.fromJson(configJson, ExportedTrackerData::class.java)
                                context.trackerDataManager.importTrackerData(trackerData)
                            }.onSuccess {
                                context.shortToast(translation["imported_toast"])
                                routes.onRuleImported?.invoke()
                                routes.navController.popBackStack()
                            }.onFailure {
                                context.longToast(translation.format("import_failed_toast", "message" to (it.message ?: "Unknown")))
                            }
                        }) {
                            Text(translation["confirm_button"])
                        }
                    }
                )
            }
        ) { padding ->
            val trackerData = remember { context.gson.fromJson(configJson, ExportedTrackerData::class.java) }
            val isSingleRule = remember { trackerData.rules.size == 1 }

            LazyColumn(
                modifier = Modifier.padding(padding).fillMaxSize(),
                contentPadding = PaddingValues(start = 16.dp, top = 16.dp, end = 16.dp, bottom = 16.dp + routes.bottomPadding),
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                items(featuresByCategory.toList()) { (category, features) ->
                    var isExpanded by remember { mutableStateOf(isSingleRule) }
                    val rotationState by animateFloatAsState(targetValue = if (isExpanded) 180f else 0f, label = "rotationState")

                    Card(
                        modifier = Modifier.fillMaxWidth().clickable { isExpanded = !isExpanded },
                        shape = RoundedCornerShape(16.dp)
                    ) {
                        Column(modifier = Modifier.padding(12.dp)) {
                            Row(verticalAlignment = Alignment.CenterVertically) {
                                Text(
                                    text = category,
                                    fontWeight = FontWeight.Bold,
                                    fontSize = 18.sp,
                                    modifier = Modifier.weight(1f)
                                )
                                IconButton(onClick = { isExpanded = !isExpanded }) {
                                    Icon(
                                        imageVector = Icons.Default.KeyboardArrowDown,
                                        contentDescription = translation["expand_button_description"],
                                        modifier = Modifier.graphicsLayer(rotationZ = rotationState)
                                    )
                                }
                            }
                            AnimatedVisibility(visible = isExpanded) {
                                Column {
                                    HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp))
                                    features.forEach { feature ->
                                        Row(
                                            modifier = Modifier
                                                .fillMaxWidth()
                                                .padding(vertical = 4.dp)
                                                .padding(start = (feature.indentation * 16).dp),
                                            verticalAlignment = Alignment.Top
                                        ) {
                                            Text(
                                                text = feature.name,
                                                modifier = Modifier.weight(1f),
                                                fontWeight = FontWeight.SemiBold,
                                            )
                                            Spacer(modifier = Modifier.width(16.dp))
                                            Text(
                                                text = parser.parseValue(feature.key, feature.value).toString(),
                                                color = MaterialTheme.colorScheme.primary,
                                                textAlign = TextAlign.End,
                                            )
                                        }
                                        HorizontalDivider(modifier = Modifier.padding(vertical = 4.dp))
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
