
extension VideoCommand {

    /// HTML video options
    enum Option {

        /// Specifies that video controls should be displayed (such as a play/pause button etc).
        case controls

        /// Specifies that the video will start playing as soon as it is ready
        case autoplay

        /// Specifies that the video will start over again, every time it is finished
        case loop

        /// Specifies that the audio output of the video should be muted
        case muted

        /// Mobile browsers will play the video right where it is instead of the default, which is to open it up fullscreen while it plays
        case playsinline

        /// Sets the height of the video player
        case height(Int)

        /// Sets the width of the video player
        case width(Int)

        /// Specifies if and how the author thinks the video should be loaded when the page loads
        case preload(Preload)

        /// Specifies an image to be shown while the video is downloading, or until the user hits the play button
        case poster(image: String)

        /// Specifies the URL of the video file
        case src(String)

        init?(rawValue: String) {
            switch rawValue {
            case "controls":
                self = .controls
                return
            case "autoplay":
                self = .autoplay
                return
            case "muted":
                self = .muted
                return
            case "loop":
                self = .loop
                return
            case "playsinline":
                self = .playsinline
                return
            default: break
            }

            let parts = rawValue.components(separatedBy: "=")
            guard parts.count == 2 else {
                return nil
            }

            let optionName = parts[0]
            let value = parts[1].removingSurroundingQuotes

            switch optionName {
            case "height":
                guard let height = Int(value) else {
                    return nil
                }
                self = .height(height)
            case "width":
                guard let width = Int(value) else {
                    return nil
                }
                self = .width(width)
            case "preload":
                guard let preloadOption = Preload(rawValue: value) else {
                    return nil
                }
                self = .preload(preloadOption)
            case "poster":
                self = .poster(image: value)
            case "src":
                self = .src(value)
            default:
                return nil
            }
            return
        }

        var rawValue: String {
            switch self {
            case .controls: return "controls"
            case .autoplay: return "autoplay"
            case .muted: return "muted"
            case .loop: return "loop"
            case .playsinline: return "playsinline"
            case .height(let height): return "height='\(height)'"
            case .width(let width): return "width='\(width)'"
            case .preload(let option): return "preload='\(option)'"
            case .poster(let image): return "poster='\(image)'"
            case .src(let url): return "src='\(url)'"
            }
        }
    }
}

extension VideoCommand.Option {

    /**
     The `preload` attribute specifies if and how the author thinks that the video should be loaded when the page loads.

     The `preload` attribute allows the author to provide a hint to the browser about what he/she thinks will lead to the best user experience.
     This attribute may be ignored in some instances.

     Note: The `preload` attribute is ignored if `autoplay` is present.
     */
    enum Preload: String {

        /// The author thinks that the browser should load the entire video when the page loads
        case auto

        /// The author thinks that the browser should load only metadata when the page loads
        case metadata

        /// The author thinks that the browser should NOT load the video when the page loads
        case none
    }
}
