// pages/community/index.js
import { MOCK_POSTS } from '../../utils/mockData';

Page({
    data: {
        posts: [],
        displayedPosts: [],
        activePostId: null,  // For showing action popup
        showCommentBar: false,
        commentingPostId: null,
        commentText: ''
    },

    onLoad() {
        this.setData({ posts: MOCK_POSTS });
        this.updateDisplayedPosts();
    },

    onShow() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({ selected: 2 });
        }
        // Refresh posts in case new ones were added
        this.setData({ posts: MOCK_POSTS });
        this.updateDisplayedPosts();
    },

    updateDisplayedPosts() {
        const { posts } = this.data;
        // Show all posts with likedByMe flag
        const displayed = posts.map(post => ({
            ...post,
            likedByMe: post.likes.includes('我的花园')
        }));
        this.setData({ displayedPosts: displayed });
    },

    // Preview image in full screen
    previewImage(e) {
        const { url, urls } = e.currentTarget.dataset;
        const resolvedUrls = urls.map(img => {
            if (img.indexOf('http') === 0 || img.indexOf('wxfile') === 0) {
                return img;
            }
            return `/assets/${img}`;
        });

        wx.previewImage({
            current: url,
            urls: resolvedUrls
        });
    },

    // Toggle action popup (WeChat style)
    toggleActionPopup(e) {
        const postId = e.currentTarget.dataset.id;
        if (this.data.activePostId === postId) {
            this.setData({ activePostId: null });
        } else {
            this.setData({ activePostId: postId });
        }
    },

    hideActionPopup() {
        if (this.data.activePostId) {
            this.setData({ activePostId: null });
        }
    },

    stopPropagation() {
        // Just stop event propagation, do nothing
    },

    // Like post
    likePost(e) {
        const postId = e.currentTarget.dataset.id;
        const posts = this.data.posts.map(post => {
            if (post.id === postId) {
                const likes = [...post.likes];
                const myName = '我的花园';
                if (likes.includes(myName)) {
                    // Unlike
                    const idx = likes.indexOf(myName);
                    likes.splice(idx, 1);
                } else {
                    // Like
                    likes.push(myName);
                }
                return { ...post, likes };
            }
            return post;
        });

        this.setData({
            posts,
            activePostId: null  // Hide popup after action
        }, () => {
            this.updateDisplayedPosts();
        });
    },

    // Show comment input bar
    showCommentInput(e) {
        const postId = e.currentTarget.dataset.id;
        this.setData({
            showCommentBar: true,
            commentingPostId: postId,
            commentText: '',
            activePostId: null  // Hide popup
        });
    },

    hideCommentBar() {
        this.setData({
            showCommentBar: false,
            commentingPostId: null,
            commentText: ''
        });
    },

    onCommentInput(e) {
        this.setData({ commentText: e.detail.value });
    },

    submitComment() {
        const { commentText, commentingPostId } = this.data;

        if (!commentText.trim()) {
            return;
        }

        const posts = this.data.posts.map(post => {
            if (post.id === commentingPostId) {
                const comments = [...post.comments, {
                    id: Date.now().toString(),
                    user: '我的花园',
                    content: commentText.trim()
                }];
                return { ...post, comments };
            }
            return post;
        });

        this.setData({
            posts,
            showCommentBar: false,
            commentingPostId: null,
            commentText: ''
        }, () => {
            this.updateDisplayedPosts();
            wx.showToast({ title: '评论成功', icon: 'success' });
        });
    },

    goToCreatePost() {
        wx.navigateTo({
            url: '/pages/community/create/index'
        });
    }
})
