//
//  LeaderboardListController.swift

//
//  Created by Leon on 2025/9/9.
//

import JXPagingView
import UIKit

class LeaderboardListController: CLBaseTableController {
    var type: DiscoverRankType = .chat

    // 拆分后的数据
    private var topThreeData: [AIDiscoverTop] = []
    private var normalListData: [AIDiscoverTop] = []

    override func viewDidLoad() {
        super.viewDidLoad()

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

    private func setupViews() {
        tableView.register(LeaderboardListCell.self, forCellReuseIdentifier: "LeaderboardListCell")
        tableView.register(LeaderboardListTopThreeCell.self, forCellReuseIdentifier: "LeaderboardListTopThreeCell")

        // addRefreshHeaderFooter()
        addRefreshHeader()
    }

    private func setupDatas() {
//        loadData()
        tableView.mj_header?.beginRefreshing()
    }

    override func loadData() {
        var api: DiscoverAPI = .rankChatList
        if type == .heartbeat {
            api = .rankHeartbeatList
        } else if type == .gift {
            api = .rankGiftList
        }
        DiscoverProvider.request(api, modelType: [AIDiscoverTop].self) { [weak self] result in
            self?.tableView.mj_header?.endRefreshing()
            switch result {
            case let .success(model):
                self?.splitData(model ?? [])
//                #warning("test")
//                self?.splitData([])
                self?.tableView.reloadData()
            case .failure:
                break
            }
        }
    }

    // 拆分数据：前三条给TopThree，剩余给普通列表
    private func splitData(_ data: [AIDiscoverTop]) {
        topThreeData = Array(data.prefix(3))
        normalListData = Array(data.dropFirst(3))
        
        if data.count > 0{
            view.hideEmpty()
        }else{
            view.showEmpty(text: "暂无数据")
        }
    }

    private func setupEvents() {
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    // override tableView delegate
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return topThreeData.isEmpty ? 0 : 1
        }
        return normalListData.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "LeaderboardListTopThreeCell", for: indexPath) as! LeaderboardListTopThreeCell
            cell.config(topThreeData, type: type)
            return cell
        }

        let cell = tableView.dequeueReusableCell(withIdentifier: "LeaderboardListCell", for: indexPath) as! LeaderboardListCell
        let data = normalListData[indexPath.row]
        cell.config(data, type: type)
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            return
        }

        let data = normalListData[indexPath.row]
        guard let aiId = data.aiId else {
            return
        }
//        AppRouter.goAIRoleHome(aiId: aiId)
        AppRouter.goChatVC(aiId: aiId)
    }
}

extension LeaderboardListController: JXPagingViewListViewDelegate {
    func listView() -> UIView {
        return view
    }

    func listScrollView() -> UIScrollView {
        return tableView
    }

    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> Void) {
        listViewDidScrollCallback = callback
    }
}
