/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef DOM_MEDIA_WEBRTC_SDP_SDPPARSER_H_
#define DOM_MEDIA_WEBRTC_SDP_SDPPARSER_H_

#include <string>
#include <vector>

#include "sdp/Sdp.h"
#include "sdp/SdpLog.h"

namespace mozilla {

class SdpParser {
 public:
  SdpParser() = default;
  virtual ~SdpParser() = default;

  class Results {
   public:
    typedef std::pair<size_t, std::string> Anomaly;
    typedef std::vector<Anomaly> AnomalyVec;
    virtual ~Results() = default;
    UniquePtr<mozilla::Sdp>& Sdp() { return mSdp; }
    const UniquePtr<mozilla::Sdp>& Sdp() const { return mSdp; }
    AnomalyVec& Errors() { return mErrors; }
    const AnomalyVec& Errors() const { return mErrors; }
    AnomalyVec& Warnings() { return mWarnings; }
    const AnomalyVec& Warnings() const { return mWarnings; }
    virtual const std::string& ParserName() const = 0;
    bool Ok() const { return mErrors.empty(); }

   protected:
    UniquePtr<mozilla::Sdp> mSdp;
    AnomalyVec mErrors;
    AnomalyVec mWarnings;
  };

  // The name of the parser implementation
  virtual const std::string& Name() const = 0;

  /**
   * This parses the provided text into an SDP object.
   * This returns a nullptr-valued pointer if things go poorly.
   */
  virtual UniquePtr<SdpParser::Results> Parse(const std::string& aText) = 0;

  class InternalResults : public Results {
   public:
    explicit InternalResults(const std::string& aParserName)
        : mParserName(aParserName) {}
    virtual ~InternalResults() = default;

    void SetSdp(UniquePtr<mozilla::Sdp>&& aSdp) { mSdp = std::move(aSdp); }

    void AddParseError(const size_t line, const std::string& message) {
      MOZ_LOG_FMT(SdpLog, LogLevel::Error, "{}: parser error {}, at line {}",
                  mParserName.c_str(), message.c_str(), line);
      mErrors.push_back(std::make_pair(line, message));
    }

    void AddParseWarning(const size_t line, const std::string& message) {
      MOZ_LOG_FMT(SdpLog, LogLevel::Warning,
                  "{}: parser warning {}, at line {}", mParserName.c_str(),
                  message.c_str(), line);
      mWarnings.push_back(std::make_pair(line, message));
    }

    const std::string& ParserName() const override { return mParserName; }

   private:
    const std::string mParserName;
  };
};

}  // namespace mozilla

#endif
