# 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 Butler
    extend T::Sig
    # Butler is the task manager of the Plex Media Server Ecosystem.
    # 

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


    sig { returns(::PlexRubySDK::Operations::GetButlerTasksResponse) }
    def get_butler_tasks
      # get_butler_tasks - Get Butler tasks
      # Returns a list of butler tasks
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/butler"
      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::GetButlerTasksResponse.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::GetButlerTasksResponseBody)
          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::GetButlerTasksBadRequest)
          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::GetButlerTasksUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { returns(::PlexRubySDK::Operations::StartAllTasksResponse) }
    def start_all_tasks
      # start_all_tasks - Start all Butler tasks
      # This endpoint will attempt to start all Butler tasks that are enabled in the settings. Butler tasks normally run automatically during a time window configured on the server's Settings page but can be manually started using this endpoint. Tasks will run with the following criteria:
      # 1. Any tasks not scheduled to run on the current day will be skipped.
      # 2. If a task is configured to run at a random time during the configured window and we are outside that window, the task will start immediately.
      # 3. If a task is configured to run at a random time during the configured window and we are within that window, the task will be scheduled at a random time within the window.
      # 4. If we are outside the configured window, the task will start immediately.
      # 
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/butler"
      headers = {}
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.post(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::StartAllTasksResponse.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::StartAllTasksBadRequest)
          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::StartAllTasksUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { returns(::PlexRubySDK::Operations::StopAllTasksResponse) }
    def stop_all_tasks
      # stop_all_tasks - Stop all Butler tasks
      # This endpoint will stop all currently running tasks and remove any scheduled tasks from the queue.
      # 
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/butler"
      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::StopAllTasksResponse.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::StopAllTasksBadRequest)
          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::StopAllTasksUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(task_name: ::PlexRubySDK::Operations::TaskName).returns(::PlexRubySDK::Operations::StartTaskResponse) }
    def start_task(task_name)
      # start_task - Start a single Butler task
      # This endpoint will attempt to start a single Butler task that is enabled in the settings. Butler tasks normally run automatically during a time window configured on the server's Settings page but can be manually started using this endpoint. Tasks will run with the following criteria:
      # 1. Any tasks not scheduled to run on the current day will be skipped.
      # 2. If a task is configured to run at a random time during the configured window and we are outside that window, the task will start immediately.
      # 3. If a task is configured to run at a random time during the configured window and we are within that window, the task will be scheduled at a random time within the window.
      # 4. If we are outside the configured window, the task will start immediately.
      # 
      request = ::PlexRubySDK::Operations::StartTaskRequest.new(
        
        task_name: task_name
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = Utils.generate_url(
        ::PlexRubySDK::Operations::StartTaskRequest,
        base_url,
        '/butler/{taskName}',
        request,
        @sdk_configuration.globals
      )
      headers = {}
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.post(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::StartTaskResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if [200, 202].include?(r.status)
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::StartTaskBadRequest)
          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::StartTaskUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(task_name: ::PlexRubySDK::Operations::PathParamTaskName).returns(::PlexRubySDK::Operations::StopTaskResponse) }
    def stop_task(task_name)
      # stop_task - Stop a single Butler task
      # This endpoint will stop a currently running task by name, or remove it from the list of scheduled tasks if it exists. See the section above for a list of task names for this endpoint.
      # 
      request = ::PlexRubySDK::Operations::StopTaskRequest.new(
        
        task_name: task_name
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = Utils.generate_url(
        ::PlexRubySDK::Operations::StopTaskRequest,
        base_url,
        '/butler/{taskName}',
        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::StopTaskResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if [200, 404].include?(r.status)
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::StopTaskBadRequest)
          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::StopTaskUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end
  end
end
