/*
 * Copyright 2025 - 2026 PICO. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pico.spatialui.sample.pages.userpage

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
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.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
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.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.pico.spatial.ui.design.Button
import com.pico.spatial.ui.design.ButtonDefaults
import com.pico.spatial.ui.design.Icon
import com.pico.spatial.ui.design.Option
import com.pico.spatial.ui.design.PicoTheme
import com.pico.spatial.ui.design.Text
import com.pico.spatial.ui.design.TextArea
import com.pico.spatial.ui.design.TextField
import com.pico.spatial.ui.design.TextFieldDefaults
import com.pico.spatial.ui.foundation.vibrant.Vibrant
import com.pico.spatial.ui.foundation.vibrant.withVibrant
import com.pico.spatial.ui.graphics.Vibrant
import com.pico.spatialui.sample.R

@Composable
fun LabelWithStar(text: String, modifier: Modifier = Modifier) {
    Row(modifier = modifier, verticalAlignment = Alignment.CenterVertically) {
        Icon(
            painter = painterResource(R.drawable.star),
            contentDescription = null,
            tint = Color(0xFFFF5752).withVibrant(Vibrant.None),
        )
        Spacer(Modifier.width(4.dp))
        Text(
            text = text,
            style = PicoTheme.typography.bodyLarge,
            color = Color.Vibrant.withVibrant(Vibrant.Semidark),
        )
    }
}

@Composable
fun FeedbackContent() {
    Column(modifier = Modifier.fillMaxSize().padding(horizontal = 128.dp).padding(bottom = 48.dp)) {
        var problemModuleText by remember { mutableStateOf("") }
        Column(modifier = Modifier.fillMaxWidth()) {
            LabelWithStar(
                stringResource(R.string.sui_sample_feedback_label_problem_module),
                modifier = Modifier.height(36.dp)
            )

            Spacer(Modifier.width(4.dp))

            TextField(
                value = problemModuleText,
                onValueChange = { problemModuleText = it },
                modifier = Modifier.fillMaxWidth(),
                placeholder = {
                    Text(
                        stringResource(
                            R.string.sui_sample_feedback_placeholder_problem_module_input
                        ),
                        style = PicoTheme.typography.bodyLarge,
                    )
                }
            )
            Spacer(Modifier.width(4.dp))
        }

        Spacer(modifier = Modifier.height(20.dp))

        data class OptionData(val stringRes: Int, val isSelected: Boolean = false)

        val optionList = remember {
            mutableStateListOf(
                OptionData(R.string.sui_sample_feedback_problem_type_suggestion, false),
                OptionData(R.string.sui_sample_feedback_problem_type_account, true),
                OptionData(R.string.sui_sample_feedback_problem_type_community, false),
                OptionData(R.string.sui_sample_feedback_problem_type_traffic, true)
            )
        }

        Column(modifier = Modifier.fillMaxWidth()) {
            LabelWithStar(
                stringResource(R.string.sui_sample_feedback_label_problem_type),
                modifier = Modifier.height(48.dp)
            )
            Spacer(Modifier.width(12.dp))

            Row(
                modifier = Modifier.fillMaxWidth().height(intrinsicSize = IntrinsicSize.Max),
                horizontalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                optionList.forEachIndexed { index, optionData ->
                    Option(
                        selected = optionData.isSelected,
                        modifier = Modifier.weight(1f).fillMaxHeight(),
                        onSelectChange = {
                            optionList[index] =
                                optionList[index].copy(isSelected = !optionData.isSelected)
                        }
                    ) {
                        Text(
                            stringResource(optionData.stringRes),
                            color = Color.Vibrant.withVibrant(Vibrant.Darkest)
                        )
                    }
                }
            }
        }

        Spacer(modifier = Modifier.height(24.dp))

        var feedbackText by remember { mutableStateOf("") }
        Column(modifier = Modifier.fillMaxWidth().weight(1f)) {
            LabelWithStar(
                stringResource(R.string.sui_sample_feedback_question_description),
                modifier = Modifier.height(36.dp)
            )
            Spacer(modifier = Modifier.height(4.dp))
            TextArea(
                value = feedbackText,
                onValueChange = { feedbackText = it },
                modifier = Modifier.fillMaxSize(),
                colors = TextFieldDefaults.textFieldColors(),
            )
        }

        Spacer(Modifier.height(40.dp))

        Button(
            onClick = {},
            modifier = Modifier.width(440.dp).align(Alignment.CenterHorizontally),
            size = ButtonDefaults.Max,
            colors =
                ButtonDefaults.buttonColors(
                    contentColor = Color.Vibrant.withVibrant(Vibrant.Darkest),
                    containerColor = Color.Vibrant.withVibrant(Vibrant.Neutral)
                ),
        ) {
            Text(text = stringResource(R.string.sui_sample_submit))
        }
    }
}

@Preview(showBackground = true, widthDp = 1280, heightDp = 720)
@Composable
fun FeedbackPreview() {
    PicoTheme { FeedbackContent() }
}
