import UIKit

/// Telegram-parity "Unread Messages" separator bar.
/// 25pt tall bar with centered label, full width, 1px borders top/bottom.
/// Exact specs from Telegram iOS ChatUnreadItem.swift.
final class UnreadSeparatorCell: UICollectionViewCell {

    static let totalHeight: CGFloat = 36 // 6pt top + 25pt bar + 5pt bottom

    private let backgroundBar = UIView()
    private let topBorder = UIView()
    private let bottomBorder = UIView()
    private let label = UILabel()

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

        // Flip for inverted collection view
        contentView.transform = CGAffineTransform(scaleX: 1, y: -1)

        backgroundBar.translatesAutoresizingMaskIntoConstraints = false
        topBorder.translatesAutoresizingMaskIntoConstraints = false
        bottomBorder.translatesAutoresizingMaskIntoConstraints = false
        label.translatesAutoresizingMaskIntoConstraints = false

        label.font = .systemFont(ofSize: 13)
        label.textAlignment = .center
        label.text = "Unread Messages"

        contentView.addSubview(backgroundBar)
        contentView.addSubview(topBorder)
        contentView.addSubview(bottomBorder)
        contentView.addSubview(label)

        let pixel = 1.0 / UIScreen.main.scale

        NSLayoutConstraint.activate([
            // Bar: 25pt tall, centered with 6pt top + 5pt bottom
            backgroundBar.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: -10),
            backgroundBar.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 10),
            backgroundBar.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 6),
            backgroundBar.heightAnchor.constraint(equalToConstant: 25),

            // Top 1px border
            topBorder.leadingAnchor.constraint(equalTo: backgroundBar.leadingAnchor),
            topBorder.trailingAnchor.constraint(equalTo: backgroundBar.trailingAnchor),
            topBorder.topAnchor.constraint(equalTo: backgroundBar.topAnchor),
            topBorder.heightAnchor.constraint(equalToConstant: pixel),

            // Bottom 1px border
            bottomBorder.leadingAnchor.constraint(equalTo: backgroundBar.leadingAnchor),
            bottomBorder.trailingAnchor.constraint(equalTo: backgroundBar.trailingAnchor),
            bottomBorder.bottomAnchor.constraint(equalTo: backgroundBar.bottomAnchor),
            bottomBorder.heightAnchor.constraint(equalToConstant: pixel),

            // Label centered in bar
            label.centerXAnchor.constraint(equalTo: backgroundBar.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: backgroundBar.centerYAnchor),
        ])

        backgroundColor = .clear
        contentView.backgroundColor = .clear
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) { fatalError() }

    override func preferredLayoutAttributesFitting(
        _ layoutAttributes: UICollectionViewLayoutAttributes
    ) -> UICollectionViewLayoutAttributes {
        let attrs = layoutAttributes.copy() as! UICollectionViewLayoutAttributes
        attrs.size.height = Self.totalHeight
        return attrs
    }

    func configure(isDarkMode: Bool) {
        if isDarkMode {
            backgroundBar.backgroundColor = UIColor(red: 0x1b/255, green: 0x1b/255, blue: 0x1b/255, alpha: 1)
            topBorder.backgroundColor = UIColor(red: 0x1b/255, green: 0x1b/255, blue: 0x1b/255, alpha: 1)
            bottomBorder.backgroundColor = UIColor(red: 0x1b/255, green: 0x1b/255, blue: 0x1b/255, alpha: 1)
            label.textColor = .white
        } else {
            backgroundBar.backgroundColor = .white
            topBorder.backgroundColor = .white
            bottomBorder.backgroundColor = .white
            label.textColor = UIColor(red: 0x8d/255, green: 0x8e/255, blue: 0x93/255, alpha: 1)
        }
    }
}
