import UIKit

/// Telegram-style banner shown at top of chat list when a new device login is detected.
/// Accept/Decline buttons let the user authorize or reject the device.
final class DeviceAlertBannerView: UIView {

    var onAccept: (() -> Void)?
    var onDecline: (() -> Void)?

    private let glass = TelegramGlassUIView()
    private let shieldIcon = UIImageView()
    private let titleLabel = UILabel()
    private let subtitleLabel = UILabel()
    private let acceptButton = UIButton(type: .system)
    private let declineButton = UIButton(type: .system)

    static let bannerHeight: CGFloat = 80

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

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

    private func setupSubviews() {
        // Glass background
        glass.fixedCornerRadius = 14
        glass.clipsToBounds = true
        addSubview(glass)

        // Shield icon
        let config = UIImage.SymbolConfiguration(pointSize: 24, weight: .medium)
        shieldIcon.image = UIImage(systemName: "shield.lefthalf.filled", withConfiguration: config)
        shieldIcon.tintColor = UIColor(red: 0.14, green: 0.54, blue: 0.9, alpha: 1) // primaryBlue
        shieldIcon.contentMode = .center
        addSubview(shieldIcon)

        // Title
        titleLabel.text = "New device login"
        titleLabel.font = .systemFont(ofSize: 15, weight: .semibold)
        titleLabel.textColor = UIColor { $0.userInterfaceStyle == .dark ? .white : .black }
        addSubview(titleLabel)

        // Subtitle (device name)
        subtitleLabel.font = .systemFont(ofSize: 13, weight: .regular)
        subtitleLabel.textColor = UIColor { $0.userInterfaceStyle == .dark
            ? UIColor.white.withAlphaComponent(0.6)
            : UIColor.black.withAlphaComponent(0.5)
        }
        subtitleLabel.lineBreakMode = .byTruncatingTail
        addSubview(subtitleLabel)

        // Accept button
        acceptButton.setTitle("Accept", for: .normal)
        acceptButton.titleLabel?.font = .systemFont(ofSize: 14, weight: .semibold)
        acceptButton.setTitleColor(.white, for: .normal)
        acceptButton.backgroundColor = UIColor(red: 0.14, green: 0.54, blue: 0.9, alpha: 1)
        acceptButton.layer.cornerRadius = 14
        acceptButton.addTarget(self, action: #selector(acceptTapped), for: .touchUpInside)
        addSubview(acceptButton)

        // Decline button
        declineButton.setTitle("Decline", for: .normal)
        declineButton.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
        declineButton.setTitleColor(UIColor(red: 0.9, green: 0.25, blue: 0.25, alpha: 1), for: .normal)
        declineButton.addTarget(self, action: #selector(declineTapped), for: .touchUpInside)
        addSubview(declineButton)
    }

    func configure(deviceName: String, deviceOs: String) {
        subtitleLabel.text = "\(deviceName) · \(deviceOs)"
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        glass.frame = bounds

        let leftPad: CGFloat = 14
        let iconSize: CGFloat = 36

        shieldIcon.frame = CGRect(
            x: leftPad,
            y: (bounds.height - iconSize) / 2,
            width: iconSize,
            height: iconSize
        )

        let textX = shieldIcon.frame.maxX + 10
        let buttonW: CGFloat = 70
        let declineW: CGFloat = 65
        let rightPad: CGFloat = 12
        let buttonsW = buttonW + 8 + declineW + rightPad
        let textW = bounds.width - textX - buttonsW

        titleLabel.frame = CGRect(x: textX, y: 18, width: textW, height: 20)
        subtitleLabel.frame = CGRect(x: textX, y: 40, width: textW, height: 18)

        let btnY: CGFloat = (bounds.height - 28) / 2
        acceptButton.frame = CGRect(
            x: bounds.width - rightPad - declineW - 8 - buttonW,
            y: btnY, width: buttonW, height: 28
        )
        declineButton.frame = CGRect(
            x: bounds.width - rightPad - declineW,
            y: btnY, width: declineW, height: 28
        )
    }

    @objc private func acceptTapped() { onAccept?() }
    @objc private func declineTapped() { onDecline?() }
}
