//
//  LNProfileSkillListProvider.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/30.
//

import Foundation
import UIKit
import SnapKit


class LNProfileSkillListProvider: LNProfileBaseProvider {
    private weak var tableView: UITableView?
    private(set) var title: String = .init(key: "A00229")
    private var curDetail: LNUserProfileVO?
    
    var numberOfCell: Int {
        curDetail?.skills.isEmpty == false ? 1 : 0
    }
    
    func update(_ detail: LNUserProfileVO) {
        curDetail = detail
        tableView?.reloadData()
    }
    
    func registerCell(tableView: UITableView) {
        tableView.register(LNProfileSkillListCell.self, forCellReuseIdentifier: LNProfileSkillListCell.className)
        
        self.tableView = tableView
    }
    
    func buildCell(tableView: UITableView, indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: LNProfileSkillListCell.className, for: indexPath) as! LNProfileSkillListCell
        
        if let curDetail {
            cell.update(curDetail)
        }
        
        return cell
    }
}


private class LNProfileSkillListCell: UITableViewCell {
    private let stackView = UIStackView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupViews()
    }
    
    func update(_ detail: LNUserProfileVO) {
        stackView.arrangedSubviews.forEach {
            stackView.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        detail.skills.forEach { skill in
            let view = LNProfileSkillListItemView(isMyUid: detail.userNo.isMyUid)
            view.update(skill)
            view.onTap { [weak self] in
                guard let self else { return }
                pushToSkillDetail(skill.id)
            }
            stackView.addArrangedSubview(view)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNProfileSkillListCell {
    private func setupViews() {
        stackView.axis = .vertical
        stackView.spacing = 7
        contentView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.bottom.equalToSuperview().offset(-28)
            make.top.equalToSuperview().offset(8)
        }
    }
}


private class LNProfileSkillListItemView: UIView {
    private let isMyUid: Bool
    
    private let bg = UIImageView()
    private let icon = UIImageView()
    private let nameLabel = UILabel()
    private let priceLabel = UILabel()
    
    func update(_ skill: LNGameMateSkillVO) {
        bg.sd_setImage(with: URL(string: skill.cover))
        icon.sd_setImage(with: URL(string: skill.icon))
        nameLabel.text = skill.name
        priceLabel.text = .init(key: "A00040", skill.price.toDisplay, skill.unit)
    }
    
    init(isMyUid: Bool) {
        self.isMyUid = isMyUid
        super.init(frame: .zero)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNProfileSkillListItemView {
    private func setupViews() {
        backgroundColor = .lightGray
        layer.cornerRadius = 12
        clipsToBounds = true
        snp.makeConstraints { make in
            make.height.equalTo(66)
        }
        
        addSubview(bg)
        bg.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        addSubview(icon)
        icon.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(46)
        }
        
        let order = buildOrderView()
        addSubview(order)
        order.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-14)
        }
        
        let textView = UIView()
        addSubview(textView)
        textView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(icon.snp.trailing).offset(10)
            make.trailing.lessThanOrEqualTo(order.snp.leading).offset(-10)
        }
        
        nameLabel.font = .heading_h4
        nameLabel.textColor = .text_1
        nameLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
        nameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        textView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        let coin = UIImageView.coinImageView()
        textView.addSubview(coin)
        coin.snp.makeConstraints { make in
            make.leading.bottom.equalToSuperview()
            make.top.equalTo(nameLabel.snp.bottom).offset(4)
            make.width.height.equalTo(16)
        }
        
        priceLabel.font = .heading_h5
        priceLabel.textColor = .text_1
        priceLabel.baselineAdjustment = .alignCenters
        textView.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.centerY.equalTo(coin)
            make.leading.equalTo(coin.snp.trailing).offset(2)
            make.trailing.lessThanOrEqualToSuperview()
        }
    }
    
    private func buildOrderView() -> UIView {
        let container = UIView()
        container.isHidden = isMyUid
        container.backgroundColor = .fill.withAlphaComponent(0.2)
        container.layer.cornerRadius = 11
        container.layer.borderWidth = 0.5
        container.layer.borderColor = UIColor.fill.cgColor
        container.snp.makeConstraints { make in
            make.height.equalTo(22)
        }
        
        let label = UILabel()
        label.text = .init(key: "A00041")
        label.font = .heading_h5
        label.textColor = .text_1
        container.addSubview(label)
        label.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(8)
            make.centerY.equalToSuperview()
        }
        
        let arrow = UIImageView.arrowImageView(size: 12)
        arrow.tintColor = .fill
        container.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-8)
            make.leading.equalTo(label.snp.trailing).offset(2)
            make.width.equalTo(9)
            make.height.equalTo(12)
        }
        
        return container
    }
}
