// pages/community/index.js
import request from '../../utils/request';

Page({
    data: {
        posts: [],
        displayedPosts: [],
        activePostId: null,  // For showing action popup
        showCommentBar: false,
        commentingPostId: null,
        commentText: '',
        isLoading: false,
        current: 1,
        pageSize: 10,
        hasMore: true
    },

    onLoad() {
        this.fetchPosts(true);
    },

    onShow() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({ selected: 2 });
        }
    },

    // Called by create post page
    onRefresh() {
        this.fetchPosts(true);
    },

    onPullDownRefresh() {
        this.fetchPosts(true).then(() => {
            wx.stopPullDownRefresh();
        });
    },

    onReachBottom() {
        if (this.data.hasMore && !this.data.isLoading) {
            this.fetchPosts(false);
        }
    },

    async fetchPosts(reset = false) {
        if (this.data.isLoading) return;

        this.setData({ isLoading: true });

        const current = reset ? 1 : this.data.current;
        const pageSize = this.data.pageSize;

        try {
            // Correct API Endpoint and Params
            const res = await request.post('/post/page', { current, pageSize });

            // Handle response structure: { code: 200, data: { list: [], ... } } 
            // OR if request.js unwraps it: { list: [], ... }
            const data = res.data || res || {};
            const rawList = data.list || [];

            // Map backend data to UI model
            const newPosts = rawList.map(item => {
                const publisher = item.publisher || {};
                const avatarObj = publisher.avatar || {};

                return {
                    id: item.id,
                    user: publisher.nickName || publisher.name || '花友',
                    avatar: avatarObj.url || '/assets/default_avatar.png',
                    content: item.content,
                    images: (item.imgList || []).map(img => img.url),
                    time: item.createdAtStr || '刚刚',
                    likes: (item.likeList || []).map(l => l.liker ? (l.liker.nickName || l.liker.name) : '花友'),
                    comments: (item.commentList || []).map(c => ({
                        id: c.id,
                        user: c.commentator ? (c.commentator.nickName || c.commentator.name) : '花友',
                        content: c.content
                    })),
                    likedByMe: item.hasLiked === 1,
                    likeCount: item.likeCount || 0,
                    commentCount: item.commentCount || 0,
                    isExpanded: false
                };
            });

            if (reset) {
                this.setData({
                    posts: newPosts,
                    displayedPosts: newPosts,
                    current: 2,
                    hasMore: newPosts.length >= pageSize
                });
            } else {
                const combined = [...this.data.posts, ...newPosts];
                this.setData({
                    posts: combined,
                    displayedPosts: combined,
                    current: current + 1,
                    hasMore: newPosts.length >= pageSize
                });
            }

        } catch (err) {
            console.error('Fetch posts failed', err);
            wx.showToast({ title: '加载失败', icon: 'none' });
        } finally {
            this.setData({ isLoading: false });
        }
    },

    updateDisplayedPosts() {
        // Just sync posts to displayedPosts
        this.setData({ displayedPosts: this.data.posts });
    },

    // 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
    async likePost(e) {
        const postId = e.currentTarget.dataset.id;
        const post = this.data.posts.find(p => p.id === postId);
        if (!post) return;

        const type = post.likedByMe ? 2 : 1;

        try {
            await request.get('/post/like', { id: postId, type });

            // Optimistic Update: Only toggle button state. Do NOT modify likes list text.
            const updatedPosts = this.data.posts.map(p => {
                if (p.id === postId) {
                    return { ...p, likedByMe: !p.likedByMe };
                }
                return p;
            });

            this.setData({
                posts: updatedPosts,
                displayedPosts: updatedPosts,
                activePostId: null
            });

            // Call page API to refresh list data (including Like List text)
            this.fetchPosts(true);

        } catch (err) {
            console.error('Like failed', err);
            wx.showToast({ title: '操作失败', icon: 'none' });
        }
    },

    // 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 });
    },

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

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

        try {
            await request.post('/post/comment', {
                postId: commentingPostId,
                content: commentText.trim()
            });

            wx.showToast({ title: '评论成功', icon: 'success' });

            // Optimistic update
            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,
                displayedPosts: posts,
                showCommentBar: false,
                commentingPostId: null,
                commentText: ''
            });

            // Silent refresh from server
            this.fetchPosts(true);

        } catch (err) {
            console.error('Comment failed', err);
            wx.showToast({ title: '评论失败', icon: 'none' });
        }
    },

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

    toggleCommentExpand(e) {
        const postId = e.currentTarget.dataset.id;
        const posts = this.data.posts.map(p => {
            if (p.id === postId) {
                return { ...p, isExpanded: !p.isExpanded };
            }
            return p;
        });
        this.setData({ posts, displayedPosts: posts });
    }
})
