<template>
    <div :class="cardClasses" @click="$emit('click')">
        <template v-if="favorite.ref">
            <div class="favorites-search-card__content">
                <div class="favorites-search-card__avatar">
                    <img :src="userImage(favorite.ref, true)" loading="lazy" />
                </div>
                <div class="favorites-search-card__detail">
                    <div class="favorites-search-card__title">
                        <span class="name" :style="displayNameStyle">{{ favorite.ref.displayName }}</span>
                    </div>
                    <div v-if="favorite.ref.location !== 'offline'" class="favorites-search-card__location">
                        <Location
                            :location="favorite.ref.location"
                            :traveling="favorite.ref.travelingToLocation"
                            :link="false" />
                    </div>
                    <span v-else class="text-xs text-muted-foreground">{{ favorite.ref.statusDescription }}</span>
                </div>
            </div>
            <div class="favorites-search-card__actions">
                <template v-if="editMode">
                    <div class="favorites-search-card__action favorites-search-card__action--checkbox" @click.stop>
                        <Checkbox v-model="isSelected" />
                    </div>
                    <div class="favorites-search-card__action-group">
                        <div class="favorites-search-card__action favorites-search-card__action--full" @click.stop>
                            <FavoritesMoveDropdown
                                :favoriteGroup="favoriteFriendGroups"
                                :currentGroup="group"
                                :currentFavorite="favorite"
                                class="favorites-search-card__dropdown"
                                type="friend" />
                        </div>
                        <div class="favorites-search-card__action">
                            <TooltipWrapper side="left" :content="t('view.favorite.unfavorite_tooltip')">
                                <Button
                                    size="icon-sm"
                                    variant="ghost"
                                    class="rounded-full text-xs h-6 w-6"
                                    @click.stop="handleDeleteFavorite">
                                    <Trash2 class="h-4 w-4" />
                                </Button>
                            </TooltipWrapper>
                        </div>
                    </div>
                </template>
                <template v-else>
                    <div class="favorites-search-card__action">
                        <TooltipWrapper side="right" :content="t('view.favorite.edit_favorite_tooltip')">
                            <Button
                                size="icon-sm"
                                variant="ghost"
                                class="rounded-full text-xs h-6 w-6"
                                @click.stop="showFavoriteDialog('friend', favorite.id)"
                                ><Star class="h-4 w-4"
                            /></Button>
                        </TooltipWrapper>
                    </div>
                </template>
            </div>
        </template>
        <template v-else>
            <div class="favorites-search-card__content">
                <div class="favorites-search-card__avatar is-empty"></div>
                <div class="favorites-search-card__detail">
                    <span>{{ favorite.name || favorite.id }}</span>
                </div>
            </div>
            <div class="favorites-search-card__actions">
                <div class="favorites-search-card__action">
                    <Button
                        class="rounded-full text-xs h-6 w-6"
                        size="icon-sm"
                        variant="outline"
                        @click.stop="handleDeleteFavorite">
                        <Trash2 class="h-4 w-4" />
                    </Button>
                </div>
            </div>
        </template>
    </div>
</template>

<script setup>
    import { Star, Trash2 } from 'lucide-vue-next';
    import { Button } from '@/components/ui/button';
    import { Checkbox } from '@/components/ui/checkbox';
    import { computed } from 'vue';
    import { storeToRefs } from 'pinia';
    import { useI18n } from 'vue-i18n';

    import { favoriteRequest } from '../../../api';
    import { useFavoriteStore } from '../../../stores';
    import { userImage } from '../../../shared/utils';

    import FavoritesMoveDropdown from './FavoritesMoveDropdown.vue';

    const props = defineProps({
        favorite: { type: Object, required: true },
        group: { type: Object, default: null },
        editMode: { type: Boolean, default: false },
        selected: { type: Boolean, default: false }
    });

    const emit = defineEmits(['click', 'toggle-select']);

    const { favoriteFriendGroups } = storeToRefs(useFavoriteStore());
    const { showFavoriteDialog } = useFavoriteStore();
    const { t } = useI18n();

    const isSelected = computed({
        get: () => props.selected,
        set: (value) => emit('toggle-select', value)
    });

    const cardClasses = computed(() => [
        'favorites-search-card',
        'favorites-search-card--friend',
        {
            'is-selected': props.selected,
            'is-edit-mode': props.editMode
        }
    ]);

    const displayNameStyle = computed(() => {
        if (props.favorite?.ref?.$userColour) {
            return {
                color: props.favorite.ref.$userColour
            };
        }
        return {};
    });

    function handleDeleteFavorite() {
        favoriteRequest.deleteFavorite({
            objectId: props.favorite.id
        });
    }
</script>
