//
//  LNGenderView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/14.
//

import Foundation
import UIKit
import SnapKit


class LNGenderView: UIView {
    private let genderIc = UIImageView()
    private let ageLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ gender: LNUserGender, _ age: Int) {
        genderIc.image = switch gender {
        case .unknow: nil
        case .male: .icGenderMaleWithBg
        case .female: .icGenderFemaleWithBg
        }
        
        if gender == .unknow {
            ageLabel.text = nil
            return
        }
        ageLabel.text = "\(age)"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNGenderView {
    private func setupViews() {
        addSubview(genderIc)
        genderIc.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalTo(0).priority(.low)
            make.height.equalTo(0).priority(.low)
        }
        
        ageLabel.font = .body_xs
        ageLabel.textColor = .text_1
        addSubview(ageLabel)
        ageLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.centerX.equalTo(snp.trailing).offset(-11)
        }
    }
}

#if DEBUG

import SwiftUI

struct LNGenderViewPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNGenderView()
        container.addSubview(view)
        view.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        view.update(.female, 36)
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNGenderViewPreview()
})
#endif

