import UIKit

enum MobileSkipButtonDirection {
    case skipBackward
    case backwards15
    case forward15
    case skipForward
}

final class MobileSkipButton: UIControl {
    private let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterialDark))
    private let imageView = UIImageView()

    var direction: MobileSkipButtonDirection = .forward15 {
        didSet {
            applyDirection()
        }
    }

    override var isHighlighted: Bool {
        didSet {
            alpha = isHighlighted ? 0.72 : 1.0
        }
    }

    override var intrinsicContentSize: CGSize {
        CGSize(width: 66, height: 66)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        configureView()
        applyDirection()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        configureView()
        applyDirection()
    }

    private func configureView() {
        backgroundColor = .clear
        accessibilityTraits = [.button]

        blurView.translatesAutoresizingMaskIntoConstraints = false
        blurView.isUserInteractionEnabled = false
        blurView.clipsToBounds = true
        addSubview(blurView)

        imageView.contentMode = .scaleAspectFit
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.isUserInteractionEnabled = false
        addSubview(imageView)

        NSLayoutConstraint.activate([
            blurView.leadingAnchor.constraint(equalTo: leadingAnchor),
            blurView.trailingAnchor.constraint(equalTo: trailingAnchor),
            blurView.topAnchor.constraint(equalTo: topAnchor),
            blurView.bottomAnchor.constraint(equalTo: bottomAnchor),

            imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
            imageView.trailingAnchor.constraint(equalTo: trailingAnchor),
            imageView.topAnchor.constraint(equalTo: topAnchor),
            imageView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        blurView.layer.cornerRadius = min(bounds.width, bounds.height) / 2
    }

    private func applyDirection() {
        switch direction {
        case .skipBackward:
            imageView.image = UIImage(named: "player_button_skip_backward")
            accessibilityLabel = "Previous video"
        case .backwards15:
            imageView.image = UIImage(named: "player_button_go_backwards_15")
            accessibilityLabel = "Back 15 seconds"
        case .forward15:
            imageView.image = UIImage(named: "player_button_go_forward_15")
            accessibilityLabel = "Forward 15 seconds"
        case .skipForward:
            imageView.image = UIImage(named: "player_button_skip_forward")
            accessibilityLabel = "Next video"
        }
    }
}
