// This may look like C code, but it's really -*- C++ -*-
/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */
#ifndef WINTVALIDATOR_H_
#define WINTVALIDATOR_H_

#include <limits>

#include <Wt/WValidator>

namespace Wt {

/*! \class WIntValidator Wt/WIntValidator Wt/WIntValidator
 *  \brief A validator that validates integer user input.
 *
 * This validator checks whether user input is an integer number in a
 * pre-defined range.
 */
class WT_API WIntValidator : public WValidator
{
public:
  /*! \brief Create a new integer validator that accepts any integer.
   */
  WIntValidator(WObject *parent = 0);

  /*! \brief Create a new integer validator that accepts integer input
   *         within the given range.
   */
  WIntValidator(int minimum, int maximum, WObject *parent = 0);

  /*! \brief Return the bottom of the valid integer range.
   */
  int bottom() const { return bottom_; }

  /*! \brief Set the bottom of the valid integer range.
   *
   * The default value is std::numeric_limits<int>::min().
   */
  void setBottom(int bottom);

  /*! \brief Return the top of the valid integer range.
   */
  int top() const { return top_; }

  /*! \brief Set the top of the valid integer range.
   *
   * The default value is std::numeric_limits<int>::max().
   */
  void setTop(int top);

  /*! \brief Set the range of valid integers.
   */
  virtual void setRange(int bottom, int top);

  /*! \brief Validate the given input.
   *
   * The input is considered valid only when it is blank for a non-mandatory
   * field, or represents an integer within the valid range.
   */
  virtual State validate(WString& input, int& pos) const;

  virtual void createExtConfig(std::ostream& config) const;

  /*! \brief Set the message to display when the input is not a number.
   *
   * The default value is "Must be an integer number."
   */
  void setInvalidNotANumberText(const WString& text);

  /*! \brief Returns the message displayed when the input is not a number.
   *
   * \sa setInvalidNotANumberText(const WString&)
   */
  WString invalidNotANumberText() const;

  /*! \brief Set message to display when the number is too small
   *
   * Depending on whether bottom() and top() are real bounds, the
   * default message is "The number must be between {1} and {2}" or
   * "The number must be larger than {1}".
   */
  void setInvalidTooSmallText(const WString& text);

  /*! \brief Returns the message displayed when the number is too small.
   *
   * \sa setInvalidTooSmallText(const WString&)
   */
  WString invalidTooSmallText() const;

  /*! \brief Set message to display when the number is too large
   *
   * Depending on whether bottom() and top() are real bounds, the
   * default message is "The number must be between {1} and {2}" or
   * "The number must be smaller than {2}".
   */
  void setInvalidTooLargeText(const WString& text);

  /*! \brief Returns the message displayed when the number is too large.
   *
   * \sa setInvalidTooLargeText(const WString&)
   */
  WString invalidTooLargeText() const;

protected:
  std::string javaScriptValidate(const std::string& jsRef) const;

  std::string inputFilter() const;

private:
  int bottom_;
  int top_;

  WString tooSmallText_;
  WString tooLargeText_;
  WString nanText_;
};

}

#endif // WINTVALIDATOR_H_
