import UIKit

enum MobilePlaybackSideControlsMode {
    case skip15
    case playlist
    case hidden
}

final class MobilePlaybackControlsView: UIView {
    let leftSideButton = MobileSkipButton()
    let mainControllerButton = MobileMainControllerButton()
    let rightSideButton = MobileSkipButton()

    var sideControlsMode: MobilePlaybackSideControlsMode = .skip15 {
        didSet {
            applySideControlsMode()
        }
    }

    var controllerState: MobileMainControllerState {
        get { mainControllerButton.controllerState }
        set { mainControllerButton.controllerState = newValue }
    }

    override var intrinsicContentSize: CGSize {
        switch sideControlsMode {
        case .skip15, .playlist:
            return CGSize(width: 262, height: 98)
        case .hidden:
            return CGSize(width: 98, height: 98)
        }
    }

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

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

    private func configureHierarchy() {
        let stack = UIStackView(arrangedSubviews: [
            leftSideButton,
            mainControllerButton,
            rightSideButton
        ])
        stack.axis = .horizontal
        stack.alignment = .center
        stack.spacing = 16
        stack.setCustomSpacing(16, after: mainControllerButton)
        stack.translatesAutoresizingMaskIntoConstraints = false
        addSubview(stack)

        NSLayoutConstraint.activate([
            leftSideButton.widthAnchor.constraint(equalToConstant: 66),
            leftSideButton.heightAnchor.constraint(equalToConstant: 66),
            mainControllerButton.widthAnchor.constraint(equalToConstant: 98),
            mainControllerButton.heightAnchor.constraint(equalToConstant: 98),
            rightSideButton.widthAnchor.constraint(equalToConstant: 66),
            rightSideButton.heightAnchor.constraint(equalToConstant: 66),
            stack.centerXAnchor.constraint(equalTo: centerXAnchor),
            stack.centerYAnchor.constraint(equalTo: centerYAnchor),
            stack.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor),
            stack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor),
            stack.topAnchor.constraint(equalTo: topAnchor),
            stack.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])

        applySideControlsMode()
    }

    private func applySideControlsMode() {
        switch sideControlsMode {
        case .skip15:
            leftSideButton.direction = .backwards15
            rightSideButton.direction = .forward15
            leftSideButton.isHidden = false
            rightSideButton.isHidden = false
        case .playlist:
            leftSideButton.direction = .skipBackward
            rightSideButton.direction = .skipForward
            leftSideButton.isHidden = false
            rightSideButton.isHidden = false
        case .hidden:
            leftSideButton.isHidden = true
            rightSideButton.isHidden = true
        }

        invalidateIntrinsicContentSize()
    }
}
