//
//  ChatSearchListController.swift

//
//  Created by Leon on 2025/8/15.
//

import JXSegmentedView
import UIKit

class ChatSearchListController: CLBaseTableController {
    weak var viewModel : FriendsSearchViewModel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupViews()
        setupDats()
        setupEvents()
    }

    private func setupViews() {
        navigationView.isHidden = true
        
        tableView.register(SessionListCell.self, forCellReuseIdentifier: "SessionListCell")
        
        // 添加空状态视图
        view.setupEmpty(empty: true,startY: 100,  msg: "No result Yet")
    }

    private func setupDats() {
        // 初始化时加载所有会话
        loadData()
    }
    
    override func loadData() {
        guard let viewModel = viewModel else { return }
        
        viewModel.searchConversations(text: nil) { [weak self] result, conversations in
            DispatchQueue.main.async {
                if result {
                    self?.view.setupEmpty(empty: conversations.count <= 0,startY: 100, msg: "No result Yet")
                    self?.tableView.reloadData()
                } else {
                    self?.view.setupEmpty(empty: true,startY: 100, msg: "No result Yet")
                }
            }
        }
    }
    
    /// 执行搜索
    func performSearch(with text: String) {
        guard let viewModel = viewModel else { return }
        
        viewModel.searchingStr = text
        viewModel.searchConversations(text: text) { [weak self] result, conversations in
            DispatchQueue.main.async {
                if result {
                    self?.view.setupEmpty(empty: conversations.count <= 0,startY: 100, msg: "No result Yet")
                    self?.tableView.reloadData()
                } else {
                    self?.view.setupEmpty(empty: true,startY: 100, msg: "No result Yet")
                }
            }
        }
    }

    private func setupEvents() {
    }
    
    // MARK: - Override
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel?.searchConversationResultCount ?? 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SessionListCell", for: indexPath) as! SessionListCell
        
        if let viewModel = viewModel,
           indexPath.row < viewModel.searchConversations.count {
            let conversation = viewModel.searchConversations[indexPath.row]
            // 使用支持高亮显示的config方法
            cell.config(conversation, searchText: viewModel.searchingStr.isEmpty ? nil : viewModel.searchingStr)
        }
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let viewModel = viewModel,
              indexPath.row < viewModel.searchConversations.count else {
            return
        }
        
        let conversation = viewModel.searchConversations[indexPath.row]
        let conversationId = conversation.conversationId
        AppRouter.goChatVC(conversationId: conversationId, complete: nil)
    }
}

extension ChatSearchListController: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
