//
//  NoticeCenterListController.swift

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

import UIKit

class NoticeCenterListController: CLBaseTableController {
    var titleView: TitleView!
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
        setupDatas()
        setupEvents()
    }

    private func setupViews() {
        titleView = {
            let v = TitleView()
            return v
        }()
        let title = "Notice"
        titleView.title = title
        titleView.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: titleView.preCalculateHeight())
        tableView.tableHeaderView = titleView
        navigationView.alpha0Title = title

        tableView.snp.remakeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
        tableView.register(NoticeCenterListCell.self, forCellReuseIdentifier: "NoticeCenterListCell")

        addRefreshHeaderFooter()
    }

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

    private func setupEvents() {
    }

    override func loadData() {
        let pageData = RequestPageData()
        pageData.pn = page

        let pageParams = pageData.toNonNilDictionary()
        var params = [String: Any]()
        params.updateValue(pageParams, forKey: "page")

        IMProvider.request(.messageList(params: params), modelType: ResponseContentPageData<MessageNotice>.self) { [weak self] result in
            self?.tableView.mj_header?.endRefreshing()
            self?.tableView.mj_footer?.endRefreshing()
            switch result {
            case let .success(model):
                if let messages = model?.datas {
                    if pageData.pn == 1 {
                        self?.datas = messages
                        self?.tableView.mj_footer?.resetNoMoreData()
                        self?.view.setupEmpty(empty: messages.count <= 0, msg: "暂无内容")
                    } else {
                        self?.datas.append(contentsOf: messages)
                        if messages.count <= 0 {
                            self?.tableView.mj_footer?.endRefreshingWithNoMoreData()
                        }
                    }
                    self?.tableView.reloadData()
                }
            case .failure:
                break
            }
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NoticeCenterListCell", for: indexPath) as! NoticeCenterListCell
        if let data = datas[indexPath.row] as? MessageNotice {
            cell.config(data)
        }
        cell.delegate = self
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }

    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        NaviAlphaHandle.changeNaviTitleAlpha(scrollView: scrollView, titleLabel: navigationView.titleLabel)
    }
}

extension NoticeCenterListController: NoticeCenterListCellDelegate{
    func noticeCellTapCheck(data:MessageNotice){
        guard let type = data.type else{return}
        
        switch type {
        case .vipRenewSuccess:
            // 跳转会员中心
            AppRouter.goVIPCenter()
            break
        case .vipRenewFail:
            // 跳转会员中心
            AppRouter.goVIPCenter()
            break
        case .myAiGifted:
            // 跳转钱包流水
//            Hud.toast(str: "钱包流水")
            AppRouter.goBillList()
            break
        case .myAiAlbumPhotoUnlock:
            // 跳转钱包流水
//            Hud.toast(str: "钱包流水")
            AppRouter.goBillList()
            break
        case .heartBeatLevelDown:
            // 跳转与角色聊天页面
            guard let model = data.getExtra(), let aiId = model.aiId else{
                assert(false)
                return
            }
            AppRouter.goChatVC(aiId: aiId)
            break
        default:
            break
        }
    }
}
