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

import Foundation
import UIKit
import SnapKit


class LNSkillCommentsView: UIView {
    private let starLabel = UILabel()
    private let countLabel = UILabel()
    private let stackView = UIStackView()
    
    private var curSkill: LNGameMateSkillDetailVO?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ detail: LNGameMateSkillDetailVO) {
        curSkill = detail
        starLabel.text = "\(detail.star)"
        
        LNGameMateManager.shared.getSkillCommentList(id: detail.id) { [weak self] res in
            guard let self else { return }
            guard let res else { return }
            isHidden = res.list.isEmpty
            countLabel.text = "(\(res.total))"
            reloadList(res.list)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNSkillCommentsView {
    private func reloadList(_ list: [LNSkillCommentVO]) {
        stackView.arrangedSubviews.forEach {
            stackView.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        
        let comments = list.prefix(3)
        comments.forEach {
            let itemView = LNSkillCommentItemView()
            itemView.update($0)
            stackView.addArrangedSubview(itemView)
            if $0.userNo != comments.last?.userNo {
                let line = UIView()
                line.backgroundColor = .fill_2
                line.snp.makeConstraints { make in
                    make.height.equalTo(1)
                }
                stackView.addArrangedSubview(line)
            }
        }
    }
    
    private func setupViews() {
        let header = buildHeader()
        addSubview(header)
        header.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview().offset(18)
        }
        
        stackView.axis = .vertical
        stackView.spacing = 0
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(header.snp.bottom)
            make.bottom.equalToSuperview()
        }
    }
    
    private func buildHeader() -> UIView {
        let container = UIView()
        container.onTap { [weak self] in
            guard let self else { return }
            guard let curSkill else { return }
            let panel = LNSkillCommentsPanel()
            panel.update(curSkill.id)
            panel.popup(self)
        }
        
        let starIc = UIImageView(image: .icStarFill)
        container.addSubview(starIc)
        starIc.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.height.equalTo(16)
        }
        
        starLabel.font = .heading_h3
        starLabel.textColor = .text_5
        container.addSubview(starLabel)
        starLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.equalTo(starIc.snp.trailing).offset(4)
        }
        
        let dotView = UIView()
        dotView.backgroundColor = .text_3
        dotView.layer.cornerRadius = 2
        container.addSubview(dotView)
        dotView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(starLabel.snp.trailing).offset(8)
            make.width.height.equalTo(4)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00163")
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(dotView.snp.trailing).offset(8)
        }
        
        countLabel.font = .heading_h4
        countLabel.textColor = .text_5
        container.addSubview(countLabel)
        countLabel.snp.makeConstraints { make in
            make.leading.equalTo(titleLabel.snp.trailing)
            make.centerY.equalTo(titleLabel)
        }
        
        let arrow = UIImageView.arrowImageView(size: 10)
        arrow.tintColor = .text_4
        container.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        let moreLabel = UILabel()
        moreLabel.text = .init(key: "A00048")
        moreLabel.font = .body_s
        moreLabel.textColor = .text_5
        container.addSubview(moreLabel)
        moreLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(arrow.snp.leading).offset(-4)
        }
        
        return container
    }
}


class LNSkillCommentItemView: UIView {
    private let avatar = UIImageView()
    private let nameLabel = UILabel()
    private let starView = LNFiveStarScoreView()
    private let commentLabel = UILabel()
    private let timeLabel = UILabel()
    private var comment: LNSkillCommentVO?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ comment: LNSkillCommentVO) {
        avatar.showAvatar(comment.avatar)
        nameLabel.text = comment.nickname
        starView.score = comment.star
        commentLabel.text = comment.comment
        timeLabel.text = TimeInterval(comment.time / 1_000).tencentIMTimeDesc
        
        self.comment = comment
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNSkillCommentItemView {
    private func setupViews() {
        avatar.layer.cornerRadius = 21
        avatar.clipsToBounds = true
        addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalToSuperview().offset(18)
            make.width.height.equalTo(42)
        }
        
        nameLabel.font = .heading_h4
        nameLabel.textColor = .text_5
        addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.leading.equalTo(avatar.snp.trailing).offset(10)
            make.top.equalTo(avatar)
        }
        
        starView.icSize = 13
        starView.spacing = 2
        addSubview(starView)
        starView.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.centerY.equalTo(nameLabel)
            make.leading.greaterThanOrEqualTo(nameLabel.snp.trailing).offset(16)
        }
        
        commentLabel.font = .body_m
        commentLabel.textColor = .text_5
        commentLabel.numberOfLines = 0
        addSubview(commentLabel)
        commentLabel.snp.makeConstraints { make in
            make.leading.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(4)
            make.trailing.equalToSuperview()
        }
        
        timeLabel.font = .body_xs
        timeLabel.textColor = .text_3
        addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.leading.equalTo(nameLabel)
            make.top.equalTo(commentLabel.snp.bottom).offset(4)
            make.bottom.equalToSuperview().offset(-18)
        }
    }
}
