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

import Foundation
import UIKit
import SnapKit


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

private class LNProfileUserInfoCell: UITableViewCell {
    private let languageLabel = UILabel()
    private let descLabel = UILabel()
    private let tagView = LNSkillTagView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }
    
    func update(_ detail: LNUserProfileVO) {
        languageLabel.text = detail.languageNames.joined(separator: "，")
        languageLabel.superview?.isHidden = detail.languageNames.isEmpty

        descLabel.superview?.isHidden = detail.intro.isEmpty
        descLabel.text = detail.intro

        tagView.update(detail.interests.compactMap({ $0.name }))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNProfileUserInfoCell {
    private func setupViews() {
        let textView = UIStackView()
        textView.axis = .vertical
        textView.spacing = 10
        contentView.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(8)
            make.bottom.equalToSuperview().offset(-28).priority(.low)
        }

        let languageView = UIView()
        textView.addArrangedSubview(languageView)

        let languageIc = UIImageView()
        languageIc.image = .icLanguage.withRenderingMode(.alwaysTemplate)
        languageIc.tintColor = .text_3
        languageView.addSubview(languageIc)
        languageIc.snp.makeConstraints { make in
            make.leading.verticalEdges.equalToSuperview()
        }

        languageLabel.font = .body_m
        languageLabel.textColor = .text_4
        languageView.addSubview(languageLabel)
        languageLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
            make.leading.equalTo(languageIc.snp.trailing).offset(4)
        }

        let bioView = UIView()
        textView.addArrangedSubview(bioView)

        let faceIc = UIImageView()
        faceIc.image = .icProfileBio
        bioView.addSubview(faceIc)
        faceIc.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
            make.width.height.equalTo(16)
        }

        descLabel.font = .body_m
        descLabel.textColor = .text_4
        descLabel.numberOfLines = 0
        bioView.addSubview(descLabel)
        descLabel.snp.makeConstraints { make in
            make.leading.equalTo(faceIc.snp.trailing).offset(4)
            make.top.trailing.equalToSuperview()
            make.bottom.equalToSuperview()
        }

        textView.addArrangedSubview(tagView)
    }
}
