# Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.

# typed: true
# frozen_string_literal: true

require 'faraday'
require 'faraday/multipart'
require 'sorbet-runtime'

module PlexRubySDK
  extend T::Sig
  class Playlists
    extend T::Sig
    # Playlists are ordered collections of media. They can be dumb (just a list of media) or smart (based on a media query, such as "all albums from 2017"). 
    # They can be organized in (optionally nesting) folders.
    # Retrieving a playlist, or its items, will trigger a refresh of its metadata. 
    # This may cause the duration and number of items to change.
    # 

    sig { params(sdk_config: SDKConfiguration).void }
    def initialize(sdk_config)
      @sdk_configuration = sdk_config
    end


    sig { params(request: T.nilable(::PlexRubySDK::Operations::CreatePlaylistRequest)).returns(::PlexRubySDK::Operations::CreatePlaylistResponse) }
    def create_playlist(request)
      # create_playlist - Create a Playlist
      # Create a new playlist. By default the playlist is blank. To create a playlist along with a first item, pass:
      # - `uri` - The content URI for what we're playing (e.g. `server://1234/com.plexapp.plugins.library/library/metadata/1`).
      # - `playQueueID` - To create a playlist from an existing play queue.
      # 
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/playlists"
      headers = {}
      query_params = Utils.get_query_params(::PlexRubySDK::Operations::CreatePlaylistRequest, request, @sdk_configuration.globals)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.post(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::CreatePlaylistResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::CreatePlaylistResponseBody)
          res.object = out
        end
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::CreatePlaylistBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::CreatePlaylistUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(playlist_type: T.nilable(::PlexRubySDK::Operations::PlaylistType), smart: T.nilable(::PlexRubySDK::Operations::QueryParamSmart)).returns(::PlexRubySDK::Operations::GetPlaylistsResponse) }
    def get_playlists(playlist_type = nil, smart = nil)
      # get_playlists - Get All Playlists
      # Get All Playlists given the specified filters.
      request = ::PlexRubySDK::Operations::GetPlaylistsRequest.new(
        
        playlist_type: playlist_type,
        smart: smart
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/playlists"
      headers = {}
      query_params = Utils.get_query_params(::PlexRubySDK::Operations::GetPlaylistsRequest, request, @sdk_configuration.globals)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.get(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::GetPlaylistsResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetPlaylistsResponseBody)
          res.object = out
        end
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetPlaylistsBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetPlaylistsUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(playlist_id: ::Float).returns(::PlexRubySDK::Operations::GetPlaylistResponse) }
    def get_playlist(playlist_id)
      # get_playlist - Retrieve Playlist
      # Gets detailed metadata for a playlist. A playlist for many purposes (rating, editing metadata, tagging), can be treated like a regular metadata item:
      # Smart playlist details contain the `content` attribute. This is the content URI for the generator. This can then be parsed by a client to provide smart playlist editing.
      # 
      request = ::PlexRubySDK::Operations::GetPlaylistRequest.new(
        
        playlist_id: playlist_id
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = Utils.generate_url(
        ::PlexRubySDK::Operations::GetPlaylistRequest,
        base_url,
        '/playlists/{playlistID}',
        request,
        @sdk_configuration.globals
      )
      headers = {}
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.get(url) do |req|
        req.headers = headers
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::GetPlaylistResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetPlaylistResponseBody)
          res.object = out
        end
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetPlaylistBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetPlaylistUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(playlist_id: ::Float).returns(::PlexRubySDK::Operations::DeletePlaylistResponse) }
    def delete_playlist(playlist_id)
      # delete_playlist - Deletes a Playlist
      # This endpoint will delete a playlist
      # 
      request = ::PlexRubySDK::Operations::DeletePlaylistRequest.new(
        
        playlist_id: playlist_id
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = Utils.generate_url(
        ::PlexRubySDK::Operations::DeletePlaylistRequest,
        base_url,
        '/playlists/{playlistID}',
        request,
        @sdk_configuration.globals
      )
      headers = {}
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.delete(url) do |req|
        req.headers = headers
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::DeletePlaylistResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 204
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::DeletePlaylistBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::DeletePlaylistUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(playlist_id: ::Float, title: T.nilable(::String), summary: T.nilable(::String)).returns(::PlexRubySDK::Operations::UpdatePlaylistResponse) }
    def update_playlist(playlist_id, title = nil, summary = nil)
      # update_playlist - Update a Playlist
      # From PMS version 1.9.1 clients can also edit playlist metadata using this endpoint as they would via `PUT /library/metadata/{playlistID}`
      # 
      request = ::PlexRubySDK::Operations::UpdatePlaylistRequest.new(
        
        playlist_id: playlist_id,
        title: title,
        summary: summary
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = Utils.generate_url(
        ::PlexRubySDK::Operations::UpdatePlaylistRequest,
        base_url,
        '/playlists/{playlistID}',
        request,
        @sdk_configuration.globals
      )
      headers = {}
      query_params = Utils.get_query_params(::PlexRubySDK::Operations::UpdatePlaylistRequest, request, @sdk_configuration.globals)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.put(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::UpdatePlaylistResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::UpdatePlaylistBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::UpdatePlaylistUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(playlist_id: ::Float, type: ::PlexRubySDK::Operations::GetPlaylistContentsQueryParamType).returns(::PlexRubySDK::Operations::GetPlaylistContentsResponse) }
    def get_playlist_contents(playlist_id, type)
      # get_playlist_contents - Retrieve Playlist Contents
      # Gets the contents of a playlist. Should be paged by clients via standard mechanisms. 
      # By default leaves are returned (e.g. episodes, movies). In order to return other types you can use the `type` parameter. 
      # For example, you could use this to display a list of recently added albums vis a smart playlist. 
      # Note that for dumb playlists, items have a `playlistItemID` attribute which is used for deleting or moving items.
      # 
      request = ::PlexRubySDK::Operations::GetPlaylistContentsRequest.new(
        
        playlist_id: playlist_id,
        type: type
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = Utils.generate_url(
        ::PlexRubySDK::Operations::GetPlaylistContentsRequest,
        base_url,
        '/playlists/{playlistID}/items',
        request,
        @sdk_configuration.globals
      )
      headers = {}
      query_params = Utils.get_query_params(::PlexRubySDK::Operations::GetPlaylistContentsRequest, request, @sdk_configuration.globals)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.get(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::GetPlaylistContentsResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetPlaylistContentsResponseBody)
          res.object = out
        end
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetPlaylistContentsBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetPlaylistContentsUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(playlist_id: ::Float).returns(::PlexRubySDK::Operations::ClearPlaylistContentsResponse) }
    def clear_playlist_contents(playlist_id)
      # clear_playlist_contents - Delete Playlist Contents
      # Clears a playlist, only works with dumb playlists. Returns the playlist.
      # 
      request = ::PlexRubySDK::Operations::ClearPlaylistContentsRequest.new(
        
        playlist_id: playlist_id
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = Utils.generate_url(
        ::PlexRubySDK::Operations::ClearPlaylistContentsRequest,
        base_url,
        '/playlists/{playlistID}/items',
        request,
        @sdk_configuration.globals
      )
      headers = {}
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.delete(url) do |req|
        req.headers = headers
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::ClearPlaylistContentsResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::ClearPlaylistContentsBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::ClearPlaylistContentsUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(playlist_id: ::Float, uri: ::String, play_queue_id: T.nilable(::Float)).returns(::PlexRubySDK::Operations::AddPlaylistContentsResponse) }
    def add_playlist_contents(playlist_id, uri, play_queue_id = nil)
      # add_playlist_contents - Adding to a Playlist
      # Adds a generator to a playlist, same parameters as the POST to create. With a dumb playlist, this adds the specified items to the playlist.
      # With a smart playlist, passing a new `uri` parameter replaces the rules for the playlist. Returns the playlist.
      # 
      request = ::PlexRubySDK::Operations::AddPlaylistContentsRequest.new(
        
        playlist_id: playlist_id,
        uri: uri,
        play_queue_id: play_queue_id
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = Utils.generate_url(
        ::PlexRubySDK::Operations::AddPlaylistContentsRequest,
        base_url,
        '/playlists/{playlistID}/items',
        request,
        @sdk_configuration.globals
      )
      headers = {}
      query_params = Utils.get_query_params(::PlexRubySDK::Operations::AddPlaylistContentsRequest, request, @sdk_configuration.globals)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.put(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::AddPlaylistContentsResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::AddPlaylistContentsResponseBody)
          res.object = out
        end
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::AddPlaylistContentsBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::AddPlaylistContentsUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(path: ::String, force: ::PlexRubySDK::Operations::QueryParamForce).returns(::PlexRubySDK::Operations::UploadPlaylistResponse) }
    def upload_playlist(path, force)
      # upload_playlist - Upload Playlist
      # Imports m3u playlists by passing a path on the server to scan for m3u-formatted playlist files, or a path to a single playlist file.
      # 
      request = ::PlexRubySDK::Operations::UploadPlaylistRequest.new(
        
        path: path,
        force: force
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/playlists/upload"
      headers = {}
      query_params = Utils.get_query_params(::PlexRubySDK::Operations::UploadPlaylistRequest, request, @sdk_configuration.globals)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.post(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::UploadPlaylistResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::UploadPlaylistBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::UploadPlaylistUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end
  end
end
