# frozen_string_literal: true

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Appium
  module Core
    class Base
      module Device
        class ScreenRecord
          #
          # @api private
          #

          attr_reader :upload_option

          METHOD = %w(POST PUT).freeze

          def initialize(remote_path: nil, user: nil, pass: nil, method: 'PUT',
                         file_field_name: nil, form_fields: nil, headers: nil, force_restart: nil)
            @upload_option = if remote_path.nil?
                               {}
                             else
                               unless METHOD.member?(method.to_s.upcase)
                                 raise ::Appium::Core::Error::ArgumentError,
                                       'method should be POST or PUT'
                               end

                               option = {}
                               option[:remotePath] = remote_path
                               option[:user] = user unless user.nil?
                               option[:pass] = pass unless pass.nil?
                               option[:method] = method
                               option[:fileFieldName] = file_field_name unless file_field_name.nil?
                               option[:formFields] = form_fields unless form_fields.nil?
                               option[:headers] = headers unless headers.nil?
                               option
                             end

            return if force_restart.nil?

            unless [true, false].member?(force_restart)
              raise ::Appium::Core::Error::ArgumentError,
                    'force_restart should be true or false'
            end

            @upload_option[:forceRestart] = force_restart
          end

          module Command
            def stop_recording_screen(remote_path: nil, user: nil, pass: nil, method: 'PUT',
                                      file_field_name: nil, form_fields: nil, headers: nil)
              option = ::Appium::Core::Base::Device::ScreenRecord.new(
                remote_path: remote_path, user: user, pass: pass, method: method,
                file_field_name: file_field_name, form_fields: form_fields, headers: headers
              ).upload_option

              params = option.empty? ? {} : { options: option }

              execute(:stop_recording_screen, {}, params)
            end

            def stop_and_save_recording_screen(file_path)
              base64data = execute(:stop_recording_screen, {}, {})
              File.open(file_path, 'wb') { |f| f << Base64.decode64(base64data) }
            end
          end # module Command
        end # class ScreenRecord
      end # module Device
    end # class Base
  end # module Core
end # module Appium