//
//  LNSkillTagView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/28.
//

import Foundation
import UIKit
import SnapKit


class LNSkillTagView: UIView {
    private let stackView = LNAutoFillStackView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ tags: [String]) {
        isHidden = tags.isEmpty
        
        var itemViews: [UIView] = []
        tags.forEach {
            let container = UIView()
            container.layer.cornerRadius = 15.5
            container.layer.borderColor = UIColor.fill_4.cgColor
            container.layer.borderWidth = 1
            container.snp.makeConstraints { make in
                make.height.equalTo(31)
            }
            
            let titleLabel = UILabel()
            titleLabel.text = $0
            titleLabel.font = .body_s
            titleLabel.textColor = .text_4
            container.addSubview(titleLabel)
            titleLabel.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.horizontalEdges.equalToSuperview().inset(10)
            }
            container.layoutIfNeeded()
            itemViews.append(container)
        }
        stackView.update(itemViews)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNSkillTagView {
    private func setupViews() {
        snp.makeConstraints { make in
            make.height.equalTo(0).priority(.low)
        }
        
        stackView.spacing = 8
        stackView.itemSpacing = 10
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}
