/*
 * 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.StringRes
import androidx.compose.foundation.background
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.Spacer
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.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
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.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import com.pico.spatial.ui.design.PicoTheme
import com.pico.spatial.ui.design.SegmentControl
import com.pico.spatial.ui.design.SegmentItem
import com.pico.spatial.ui.design.Text
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
import com.pico.spatialui.sample.viewmodel.RelationshipViewModel

enum class RelationshipTabKey(@StringRes val stringRes: Int) {
    Mutual(R.string.sui_sample_follow_list_tab_follow_mutual),
    Following(R.string.sui_sample_follow_list_tab_following),
    Followers(R.string.sui_sample_follow_list_tab_followers),
    Recommended(R.string.sui_sample_follow_list_tab_recommended)
}

@Composable
fun RelationshipContent(viewModel: RelationshipViewModel = viewModel()) {
    Column(
        modifier = Modifier.fillMaxWidth().padding(horizontal = 128.dp).padding(bottom = 45.dp),
        verticalArrangement = Arrangement.spacedBy(20.dp)
    ) {
        var selectedTab by rememberSaveable { mutableStateOf(RelationshipTabKey.Mutual) }
        SegmentControl(modifier = Modifier.fillMaxWidth()) {
            RelationshipTabKey.entries.forEach { tab ->
                SegmentItem(
                    selected = tab == selectedTab,
                    onClick = { selectedTab = tab },
                    title = { Text(stringResource(tab.stringRes)) }
                )
            }
        }

        val userList by
            when (selectedTab) {
                RelationshipTabKey.Mutual -> viewModel.mutualList.collectAsStateWithLifecycle()
                RelationshipTabKey.Followers ->
                    viewModel.myFollowerList.collectAsStateWithLifecycle()
                RelationshipTabKey.Following ->
                    viewModel.myFollowingList.collectAsStateWithLifecycle()
                RelationshipTabKey.Recommended ->
                    viewModel.recommendedUserList.collectAsStateWithLifecycle()
            }

        LazyColumn(
            modifier = Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)),
            verticalArrangement = Arrangement.spacedBy(1.dp)
        ) {
            items(items = userList, key = { it.id }) { user ->
                Row(
                    modifier =
                        Modifier.fillMaxWidth()
                            .height(72.dp)
                            .background(color = Color.Vibrant.withVibrant(Vibrant.Light)),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Spacer(Modifier.width(16.dp))
                    AsyncImage(
                        model = user.imageId,
                        contentDescription = null,
                        modifier = Modifier.size(40.dp).clip(CircleShape)
                    )
                    Spacer(Modifier.width(16.dp))

                    Column(modifier = Modifier.weight(1f)) {
                        Text(
                            text = stringResource(user.nameRes),
                            style = PicoTheme.typography.titleMedium,
                            color = PicoTheme.colorTokens.LabelPrimary,
                        )
                        Spacer(Modifier.height(4.dp))
                        Text(
                            text = stringResource(user.descriptionRes),
                            style = PicoTheme.typography.bodySmall,
                            color = PicoTheme.colorTokens.LabelTertiaryAlpha,
                        )
                    }
                }
            }
        }
    }
}

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