var HuSNS = window.HuSNS || {};
HuSNS.Like = {
    toggle: function(btn, postId) {
        var $btn = typeof btn === 'string' ? document.querySelector(btn) : btn;
        if (!$btn) return;
        
        var isLiked = $btn.classList.contains('liked');
        
        if (isLiked) {
            this.unlike($btn, postId);
        } else {
            this.like($btn, postId);
        }
    },
    
    like: function(btn, postId) {
        var $btn = typeof btn === 'string' ? document.querySelector(btn) : btn;
        if (!$btn) return;
        
        $btn.classList.add('liked');
        this._updateCount($btn, 1);
        
        if (typeof HuSNS.Request !== 'undefined') {
            HuSNS.Request.post('/api/post/like', { postId: postId })
                .catch(function(err) {
                    $btn.classList.remove('liked');
                    HuSNS.Like._updateCount($btn, -1);
                    HuSNS.Toast.show(err.message || '点赞失败');
                });
        } else {
            $.ajax({
                url: baseUrl + '/?r=mobileApi/post/like',
                method: 'POST',
                data: { postId: postId },
                success: function(res) {
                    if (res.code !== 1) {
                        $btn.classList.remove('liked');
                        HuSNS.Like._updateCount($btn, -1);
                        HuSNS.Toast.show(res.message || '点赞失败');
                    }
                },
                error: function() {
                    $btn.classList.remove('liked');
                    HuSNS.Like._updateCount($btn, -1);
                    HuSNS.Toast.show('网络异常');
                }
            });
        }
    },
    
    unlike: function(btn, postId) {
        var $btn = typeof btn === 'string' ? document.querySelector(btn) : btn;
        if (!$btn) return;
        
        $btn.classList.remove('liked');
        this._updateCount($btn, -1);
        
        if (typeof HuSNS.Request !== 'undefined') {
            HuSNS.Request.post('/api/post/unlike', { postId: postId })
                .catch(function(err) {
                    $btn.classList.add('liked');
                    HuSNS.Like._updateCount($btn, 1);
                    HuSNS.Toast.show(err.message || '取消点赞失败');
                });
        } else {
            $.ajax({
                url: baseUrl + '/?r=mobileApi/post/unlike',
                method: 'POST',
                data: { postId: postId },
                success: function(res) {
                    if (res.code !== 1) {
                        $btn.classList.add('liked');
                        HuSNS.Like._updateCount($btn, 1);
                        HuSNS.Toast.show(res.message || '取消点赞失败');
                    }
                },
                error: function() {
                    $btn.classList.add('liked');
                    HuSNS.Like._updateCount($btn, 1);
                    HuSNS.Toast.show('网络异常');
                }
            });
        }
    },
    
    _updateCount: function(btn, delta) {
        var countEl = btn.querySelector('.like-count');
        if (countEl) {
            var current = parseInt(countEl.textContent) || 0;
            countEl.textContent = current + delta;
        }
    }
};

if (typeof module !== 'undefined' && module.exports) {
    module.exports = HuSNS.Like;
}