//
//  RoleHomeAboutController.swift

//
//  Created by Leon on 2025/7/24.
//

import JXPagingView
import UIKit
class RoleHomeAboutController: CLViewController<RoleHomeAboutView> {
    var aiId: Int?
    var page: Int = 1

    var info: AIRoleInfo?
    var datas: [AIRoleGiftReceivedList]?
    var listViewDidScrollCallback: ((UIScrollView) -> Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupViews()
        setupDatas()
        setupEvents()
    }

    private func setupViews() {
        navigationView.isHidden = true
        
        container.container.scrollView.delegate = self
    }

    private func setupDatas() {
        loadStatistics()
        loadGifts()
    }

    private func setupEvents() {
    }

    // MARK: - Public

    func loadStatistics() {
        guard let id = aiId else { return }

        AIRoleProvider.request(.aiRoleStatistics(aiId: id), modelType: AIRoleStatisticsResponse.self) { [weak self] result in
            switch result {
            case let .success(success):
                self?.container.configStat(data: success)
            case let .failure(failure):
                dlog(failure)
            }
        }
        // Load Gifts
    }

    func loadGifts() {
        guard let id = aiId else { return }
        var paramas = [String: Any]()
        paramas.updateValue(id, forKey: "aiId")
        let page = ["pn": page, "ps": 10]
        paramas.updateValue(page, forKey: "page")
        AIRoleProvider.request(.aiRoleGotGiftList(params: paramas), modelType: ResponseContentPageData<AIRoleGiftReceivedList>.self) { [weak self] result in
            switch result {
            case let .success(success):
                self?.datas = success?.datas
                self?.container.configGifts(data: success?.datas)
            case .failure:
                break
            }
        }
    }

    func config(info: AIRoleInfo?) {
        self.info = info
        guard let data = info else { return }

        container.config(info: data)
    }
}

extension RoleHomeAboutController: UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        listViewDidScrollCallback?(scrollView)
    }
}

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

    func listScrollView() -> UIScrollView {
        return container.container.scrollView
    }

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