//
//  LNJoinUsInputFieldGroupView.swift
//  Gami
//
//  Created by OneeChan on 2026/1/20.
//

import Foundation
import UIKit
import SnapKit

class LNJoinUsInputFieldGroupView: UIStackView {
    let titleLabel = UILabel()
    let descLabel = UILabel()
    private let exampleButton = UIButton()
    
    let container = UIView()
    
    private var handler: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        axis = .vertical
        spacing = 8
        
        addArrangedSubview(buildHeader())
        
        addArrangedSubview(container)
        container.snp.makeConstraints { make in
            make.height.equalTo(0).priority(.low)
        }
    }
    
    func showExample(_ handler: (() -> Void)?) {
        exampleButton.isHidden = false
        self.handler = handler
    }
    
    private func buildHeader() -> UIView {
        let stackView = UIStackView()
        stackView.distribution = .fillProportionally
        stackView.spacing = 16
        stackView.alignment = .center
        
        let headerStack = UIStackView()
        headerStack.axis = .vertical
        stackView.addArrangedSubview(headerStack)
        
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        headerStack.addArrangedSubview(titleLabel)
        
        descLabel.font = .body_xs
        descLabel.textColor = .text_3
        descLabel.numberOfLines = 0
        headerStack.addArrangedSubview(descLabel)
        
        exampleButton.layer.cornerRadius = 11
        exampleButton.setBackgroundImage(.primary_7, for: .normal)
        exampleButton.clipsToBounds = true
        exampleButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            handler?()
        }), for: .touchUpInside)
        exampleButton.isHidden = true
        exampleButton.snp.makeConstraints { make in
            make.height.equalTo(22)
        }
        stackView.addArrangedSubview(exampleButton)
        
        let cover = UIView()
        cover.backgroundColor = .fill
        cover.layer.cornerRadius = 10
        cover.isUserInteractionEnabled = false
        exampleButton.addSubview(cover)
        cover.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(1)
        }
        
        let exampleLabel = UILabel()
        exampleLabel.font = .body_xs
        exampleLabel.textColor = .text_6
        exampleLabel.text = .init(key: "B00046")
        exampleLabel.setContentHuggingPriority(.required, for: .horizontal)
        exampleLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        exampleButton.addSubview(exampleLabel)
        exampleLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(6)
            make.centerY.equalToSuperview()
        }
        
        return stackView
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

