import UIKit

// MARK: - PinnedSectionBackgroundView

/// Section-level decoration view for the pinned section.
/// Displays Telegram-exact pinned background color edge-to-edge behind all pinned rows.
/// Registered as a decoration item in UICollectionViewCompositionalLayout.
final class PinnedSectionBackgroundView: UICollectionReusableView {

    static let elementKind = "pinned-section-background"

    override init(frame: CGRect) {
        super.init(frame: frame)
        updateColor()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        if traitCollection.userInterfaceStyle != previousTraitCollection?.userInterfaceStyle {
            updateColor()
        }
    }

    private func updateColor() {
        let isDark = traitCollection.userInterfaceStyle == .dark
        backgroundColor = isDark
            ? UIColor(red: 0x1C/255, green: 0x1C/255, blue: 0x1D/255, alpha: 1)  // Telegram: pinnedItemBackgroundColor
            : UIColor(red: 0xF7/255, green: 0xF7/255, blue: 0xF7/255, alpha: 1)
    }
}
