//
//  LNSkillCommentsPanel.swift
//  Gami
//
//  Created by OneeChan on 2026/2/3.
//

import Foundation
import UIKit
import SnapKit
import MJRefresh


class LNSkillCommentsPanel: LNPopupView {
    private let tableView = UITableView()
    
    private var skillId = ""
    private var nextTag: String?
    private var list: [LNSkillCommentVO] = []
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ skillId: String) {
        self.skillId = skillId
        
        tableView.mj_header?.beginRefreshing()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNSkillCommentsPanel {
    private func loadList() {
        LNGameMateManager.shared.getSkillCommentList(id: skillId, next: nextTag) { [weak self] res in
            guard let self else { return }
            tableView.mj_header?.endRefreshing()
            
            if let list = res?.list {
                if nextTag == nil {
                    self.list = list
                } else {
                    self.list.append(contentsOf: list)
                }
                tableView.reloadData()
            }
            nextTag = res?.next
            if res?.next.isEmpty != false {
                tableView.mj_footer?.endRefreshingWithNoMoreData()
            } else {
                tableView.mj_footer?.endRefreshing()
            }
        }
    }
}

extension LNSkillCommentsPanel: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: LNSkillCommentCell.className, for: indexPath) as! LNSkillCommentCell
        
        cell.update(list[indexPath.row])
        
        return cell
    }
}

extension LNSkillCommentsPanel {
    private func setupViews() {
        containerHeight = .percent(0.8)
        
        let header = buildHeader()
        container.addSubview(header)
        header.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let listView = buildListView()
        container.addSubview(listView)
        listView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(header.snp.bottom)
            make.bottom.equalToSuperview()
        }
    }
    
    private func buildHeader() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(52)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "B00111")
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        return container
    }
    
    private func buildListView() -> UIView {
        let header = MJRefreshNormalHeader { [weak self] in
            guard let self else { return }
            self.nextTag = nil
            self.loadList()
        }
        header.lastUpdatedTimeLabel?.isHidden = true
        header.stateLabel?.isHidden = true
        tableView.mj_header = header
        
        let footer = MJRefreshAutoNormalFooter { [weak self] in
            guard let self else { return }
            self.loadList()
        }
        footer.setTitle("", for: .noMoreData)
        footer.setTitle(.init(key: "A00046"), for: .idle)
        tableView.mj_footer = footer
        
        tableView.register(LNSkillCommentCell.self, forCellReuseIdentifier: LNSkillCommentCell.className)
        tableView.dataSource = self
        tableView.allowsSelection = false
        tableView.separatorStyle = .singleLine
        tableView.separatorInset = .init(top: 0, left: 16, bottom: 0, right: 16)
        tableView.backgroundColor = .clear
        
        return tableView
    }
}

private class LNSkillCommentCell: UITableViewCell {
    private let commentView = LNSkillCommentItemView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.addSubview(commentView)
        commentView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.verticalEdges.equalToSuperview()
        }
    }
    
    func update(_ comment: LNSkillCommentVO) {
        commentView.update(comment)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
