//
//  FriendSearchListController.swift

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

import JXSegmentedView
import UIKit

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

        // Do any additional setup after loading the view.
        setupViews()
        setupDats()
        setupEvents()
    }

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

    private func setupDats() {
        // 初始化时加载所有好友
        loadData()
    }
    
    override func loadData() {
        guard let viewModel = viewModel else { return }
        
        viewModel.searchFriends(page: 1, text: nil) { [weak self] result, friends in
            DispatchQueue.main.async {
                if result {
                    self?.view.setupEmpty(empty: friends.count <= 0,startY: 100, msg: "No result Yet")
                    self?.tableView.reloadData()
                } else {
                    self?.view.setupEmpty(empty: true, msg: "No result Yet")
                }
            }
        }
    }
    
    /// 执行搜索
    func performSearch(with text: String) {
        guard let viewModel = viewModel else { return }
        
        viewModel.searchingStr = text
        viewModel.searchFriends(page: 1, text: text) { [weak self] result, friends in
            DispatchQueue.main.async {
                if result {
                    self?.view.setupEmpty(empty: friends.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?.searchResultCount ?? 0
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FriendsListCell", for: indexPath) as! FriendsListCell
        cell.delegate = self
        
        if let viewModel = viewModel,
           indexPath.row < viewModel.searchFrineds.count {
            let friend = viewModel.searchFrineds[indexPath.row]
            // 使用支持高亮显示的config方法
            cell.config(friend, searchText: viewModel.searchingStr.isEmpty ? nil : viewModel.searchingStr)
        }
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let viewModel = viewModel,
              indexPath.row < viewModel.searchFrineds.count,
              let data = viewModel.searchFrineds[indexPath.row] as? RelationFriend,
              let aiId = data.aiId else {
            return
        }
        AppRouter.goAIRoleHome(aiId: Int(aiId))
    }
}

extension FriendSearchListController: FriendsListCellDelegate {
    func friendsCellTapChat(aiId: Int) {
        AppRouter.goChatVC(aiId: aiId)
    }
}

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