//
//  LNFeedManager.swift
//  Gami
//
//  Created by OneeChan on 2026/3/4.
//

import Foundation


protocol LNFeedManagerNotify {
    func onFeedCommentCountChanged(id: String, count: Int)
    func onFeedLikedChanged(id: String, liked: Bool)
    func onFeedDelete(id: String)
    func onPostFeedSuccess()
}
extension LNFeedManagerNotify {
    func onFeedDelete(id: String) { }
    func onFeedCommentCountChanged(id: String, count: Int) { }
    func onFeedLikedChanged(id: String, liked: Bool) { }
    func onPostFeedSuccess() { }
}


class LNFeedManager {
    static let shared = LNFeedManager()
    static let feedMaxIntput = 500
    static let feedCommentMaxInput = 200
    static var videoMute = true
    
    func likeFeed(id: String, queue: DispatchQueue = .main, handler: ((LNFeedLikeResponse?) -> Void)? = nil) {
        LNHttpManager.shared.likeFeed(id: id) { [weak self] res, err in
            queue.asyncIfNotGlobal {
                handler?(res)
            }
            
            if let self, let res, err == nil {
                notifyFeedLikeChanged(id: id, liked: res.like)
            }
        }
    }
    
    func getFeedList(uid: String, next: String?, queue: DispatchQueue = .main, handler: @escaping (LNFeedListResponse?) -> Void) {
        LNHttpManager.shared.getFeedList(uid: uid, size: 30, next: next ?? "") { list, err in
            queue.asyncIfNotGlobal {
                handler(list)
            }
        }
    }
    
    func getFeedDetail(id: String, queue: DispatchQueue = .main, handler: @escaping (LNFeedDetailVO?) -> Void) {
        LNHttpManager.shared.getFeedDetail(id: id) { detail, err in
            queue.asyncIfNotGlobal {
                handler(detail)
            }
        }
    }
    
    func postFeed(item: LNPostFeedItem, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
        LNHttpManager.shared.postFeed(item: item) { [weak self] err in
            queue.asyncIfNotGlobal {
                handler(err == nil)
            }
            
            if let self, err == nil {
                notifyPostFeedSuccess()
            }
        }
    }
    
    func sendFeedComment(id: String, content: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
        LNHttpManager.shared.sendFeedComment(id: id, content: content) { err in
            queue.asyncIfNotGlobal {
                handler(err == nil)
            }
        }
    }
    
    func getFeedCommentList(id: String, next: String?, queue: DispatchQueue = .main, handler: @escaping (LNFeedCommentListResponse?) -> Void) {
        LNHttpManager.shared.getFeedCommentList(id: id, size: 30, next: next ?? "") { list, err in
            queue.asyncIfNotGlobal {
                handler(list)
            }
        }
    }
    
    func listFeedComment(id: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
        LNHttpManager.shared.listFeedComment(id: id) { err in
            queue.asyncIfNotGlobal {
                handler(err == nil)
            }
        }
    }
    
    func deleteFeed(id: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
        LNHttpManager.shared.deleteFeed(id: id) { [weak self] err in
            queue.asyncIfNotGlobal {
                handler(err == nil)
            }
            
            if let self, err == nil {
                notifyFeedDelete(id: id)
            }
        }
    }
}

extension LNFeedManager {
    func notifyFeedCommentChanged(id: String, count: Int) {
        LNEventDeliver.notifyEvent { ($0 as? LNFeedManagerNotify)?.onFeedCommentCountChanged(id: id, count: count) }
    }
    
    func notifyPostFeedSuccess() {
        LNEventDeliver.notifyEvent { ($0 as? LNFeedManagerNotify)?.onPostFeedSuccess() }
    }
    
    func notifyFeedDelete(id: String) {
        LNEventDeliver.notifyEvent { ($0 as? LNFeedManagerNotify)?.onFeedDelete(id: id) }
    }
    
    func notifyFeedLikeChanged(id: String, liked: Bool) {
        LNEventDeliver.notifyEvent { ($0 as? LNFeedManagerNotify)?.onFeedLikedChanged(id: id, liked: liked) }
    }
}
