module AssetManagementPost
  def post_asset_v2(body, headers = NO_AUTH_HEADERS)
    res = { code: 0, response: "" }

    begin
      response = HTTParty.post("#{ENV['ASSET_SERVICE']}/v2/asset", headers: headers, body: body)
      res[:code] = response.code.to_i
      res[:response] = response.body
    rescue StandardError => e
      LOG.debug "Module: AssetManagementPost. Method: post_asset_v2. Message: #{e.message}"
      raise e
    end
    res
  end

  def post_asset(body, headers = ALW_USER_SERVICE_HEADERS)
    res = { code: 0, response: "" }

    begin
      response = HTTParty.post("#{ENV['ASSET_SERVICE']}/asset", headers: headers, body: body)
      res[:code] = response.code.to_i
      res[:response] = response.body
    rescue StandardError => e
      LOG.debug "Module: AssetManagementPost. Method: post_asset. Message: #{e.message}"
      raise e
    end
    res
  end

  def copy_product_assets(pid_from, pid_to, headers = ALW_USER_SERVICE_HEADERS)
    res = { code: 0, response: "" }

    begin
      response = HTTParty.post("#{ENV['ASSET_SERVICE']}/product/#{pid_from}/copy/#{pid_to}",
                               headers: headers)
      res[:code] = response.code.to_i
      res[:response] = response.body
    rescue StandardError => e
      LOG.debug "Module: AssetManagementPost. Method: copy_product_assets. Message: #{e.message}"
      raise e
    end
    res
  end

  def copy_track_assets(tuid_from, tuid_to, headers = ALW_USER_SERVICE_HEADERS)
    res = { code: 0, response: "" }

    begin
      response = HTTParty.post("#{ENV['ASSET_SERVICE']}/track/#{tuid_from}/copy/#{tuid_to}",
                               headers: headers)
      res[:code] = response.code.to_i
      res[:response] = response.body
    rescue StandardError => e
      LOG.debug "Module: AssetManagementPost. Method: copy_track_assets. Message: #{e.message}"
      raise e
    end
    res
  end

  def create_post_asset_body(token_details,
                             asset_type = "JPG",
                             product_id = PRODUCT_A_ID,
                             upc = PRODUCT_A_UPC,
                             tuid = PRODUCT_A_TUID)

    tuid = 0 if asset_type == ("JPG" || "TIF")

    { asset_type: asset_type,
      product_id: product_id,
      upc: upc,
      track_unique_id: tuid,
      filename: "#{token_details['filename']}.#{asset_type.downcase}",
      token: token_details["credentials"]["token"] }
  end

  def create_post_asset_body_v2(token_details,
                                asset_type = "JPG",
                                product_id = PRODUCT_A_ID,
                                upc = PRODUCT_A_UPC,
                                tuid = PRODUCT_A_TUID)
    { asset_type: asset_type,
      product_id: product_id,
      upc: upc,
      track_unique_id: tuid,
      filename: "#{token_details['filename']}.#{asset_type.downcase}" }
  end
end
