/*
 * 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.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
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.size
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
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.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.Icon
import com.pico.spatial.ui.design.PicoTheme
import com.pico.spatial.ui.design.SideNavigation
import com.pico.spatial.ui.design.SideNavigationItem
import com.pico.spatial.ui.design.Text
import com.pico.spatial.ui.design.TitleAlignment
import com.pico.spatial.ui.design.TitleBar
import com.pico.spatial.ui.foundation.hover.spatialHoverEffect
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

enum class NavigationItemKey(@StringRes val stringRes: Int, @DrawableRes val iconRes: Int) {
    Relationship(R.string.sui_sample_nav_item_follow_list, R.drawable.follow_list),
    Setting(R.string.sui_sample_nav_item_setting, R.drawable.setting),
    Feedback(R.string.sui_sample_nav_item_feedback, R.drawable.feedback),
    PreferenceAnalysis(R.string.sui_sample_nav_item_preference_analysis, R.drawable.content),
    BrowserHistory(R.string.sui_sample_nav_item_browser_history, R.drawable.bell),
    DownloadHistory(R.string.sui_sample_nav_item_download_history, R.drawable.history),
    LegalNotice(R.string.sui_sample_nav_item_legal_notice, R.drawable.notice),
    About(R.string.sui_sample_nav_item_about, R.drawable.ic_info)
}

fun getNavigationItemDataGroupList() =
    listOf(
        listOf(
            NavigationItemKey.Relationship,
            NavigationItemKey.Setting,
            NavigationItemKey.Feedback
        ),
        listOf(
            NavigationItemKey.PreferenceAnalysis,
            NavigationItemKey.BrowserHistory,
            NavigationItemKey.DownloadHistory
        ),
        listOf(NavigationItemKey.LegalNotice, NavigationItemKey.About)
    )

@Composable
fun UserPage() {
    val groupList = remember { getNavigationItemDataGroupList() }

    var currentSelectedItemKey by rememberSaveable {
        mutableStateOf(NavigationItemKey.Relationship)
    }

    val clickHandler = { key: NavigationItemKey -> currentSelectedItemKey = key }

    Row(modifier = Modifier.fillMaxSize()) {
        SideNavigation(
            modifier = Modifier.fillMaxHeight().width(316.dp),
            header = {
                Box(modifier = Modifier.padding(start = 8.dp, top = 32.dp, bottom = 32.dp)) {
                    Text(
                        text = stringResource(R.string.sui_sample_user_name_placeholder),
                        maxLines = 1,
                        color = Color.Vibrant.withVibrant(Vibrant.UltraDark)
                    )
                }
            }
        ) {
            Column(
                modifier = Modifier.fillMaxWidth(),
                verticalArrangement = Arrangement.spacedBy(16.dp)
            ) {
                groupList.forEach { group ->
                    Column {
                        group.forEach { itemData ->
                            key(itemData) {
                                SideNavigationItem(
                                    selected = currentSelectedItemKey == itemData,
                                    leading = {
                                        Icon(
                                            painter = painterResource(itemData.iconRes),
                                            contentDescription = null
                                        )
                                    },
                                    modifier =
                                        Modifier.spatialHoverEffect().clickable {
                                            clickHandler(itemData)
                                        }
                                ) {
                                    Text(text = stringResource(itemData.stringRes), maxLines = 1)
                                }
                            }
                        }
                    }
                }
            }
        }

        Column(
            modifier =
                Modifier.weight(1f)
                    .fillMaxHeight()
                    .background(color = Color.Vibrant.withVibrant(Vibrant.Light)),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            TitleBar(
                title = {
                    Text(
                        stringResource(currentSelectedItemKey.stringRes),
                        color = Color.Vibrant.withVibrant(Vibrant.UltraDark)
                    )
                },
                titleAlignment = TitleAlignment.CenterInBar
            )

            Box(modifier = Modifier.fillMaxWidth().weight(1f)) {
                androidx.compose.animation.Crossfade(targetState = currentSelectedItemKey) {
                    targetKey ->
                    UserDetailDispatcher(targetKey)
                }
            }
        }
    }
}

@Composable
fun UserDetailDispatcher(key: NavigationItemKey) {
    when (key) {
        NavigationItemKey.Relationship -> RelationshipContent()
        NavigationItemKey.Setting -> SettingContent()
        NavigationItemKey.Feedback -> FeedbackContent()
        else -> PlaceHolderContent()
    }
}

@Composable
fun PlaceHolderContent() {
    Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Spacer(modifier = Modifier.weight(0.382f))

            Icon(
                painter = painterResource(R.drawable.follow_list),
                contentDescription = null,
                modifier = Modifier.size(64.dp),
                tint = Color.Vibrant.withVibrant(Vibrant.Dark)
            )

            Spacer(Modifier.height(10.dp))

            Text(
                stringResource(R.string.sui_sample_common_empty_title),
                style = PicoTheme.typography.titleLarge,
                color = Color.Vibrant.withVibrant(Vibrant.Semidark)
            )

            Spacer(Modifier.height(10.dp))

            Text(
                stringResource(R.string.sui_sample_common_empty_description),
                style = PicoTheme.typography.bodyMedium,
                color = Color.Vibrant.withVibrant(Vibrant.Dark)
            )

            Spacer(modifier = Modifier.weight(0.618f))
        }
    }
}

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