/* Blinker
   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
   a blinkenarea.org project */

#ifndef BLINKER_GAME_H
#define BLINKER_GAME_H

#include <string>
#include <vector>

#include <BlinkenLib/BlinkenFrame.h>

#include "Color.h"
#include "ColorFile.h"
#include "File.h"
#include "Format.h"
#include "FormatFile.h"
#include "Mgrs.h"
#include "Module.h"
#include "OutStreamFile.h"
#include "UIntFile.h"

namespace Blinker {

/// base class for games
class Game: public Module
{
protected:
  /// raw color data matching image buffer
  typedef std::vector<unsigned char> ColorData;

  /// descriptor of value (e.g. a setting for the game)
  struct ValueDescr {
    unsigned int default_;
    unsigned int minimum;
    unsigned int maximum;
  };

public:
  /**
   * @brief constructor
   * @param[in] name module name
   * @param[in] mgrs managers
   * @param[in] dirBase base directory
   */
  Game(const std::string &name, Mgrs &mgrs, const Directory &dirBase);

  /// virtual destructor
  virtual ~Game();

private:
  /// copy constructor disabled
  Game(const Game &that);

  /// assignment operator disabled
  const Game & operator=(const Game &that);

public:
  /// check for update of configuration
  virtual void updateConfig() final;

  /// check for update of configuration (derived game), return true on update
  virtual bool updateConfigGame() = 0;

protected:
  /// re-initialize game (e.g. due to config change)
  virtual void reinitialize() = 0;

  /// redraw current game image, expected to call sendFrame() at end
  virtual void redraw() = 0;

  /// activate game: set up image buffer, call redraw()
  void activate();

  /// deactivate game: tear down image buffer, deactivate output
  void deactivate();

  /// check if game is active
  bool isActive() const;

  /// check if integer is between minimum and maximum values
  static bool checkLimitInt(int i, int min, int max)
  {
    return i >= min && i <= max;
  }

  /// limit integer to minimum and maximum values
  static void limitInt(int &i, int min, int max)
  {
    if (i < min) {
      i = min;
    }
    if (i > max) {
      i = max;
    }
  }

  /// check if range of two integers is nonEmpty and limit the integers
  static bool checkIntRangeLimit(int &i1, int &i2, int min, int max)
  {
    if (i1 > i2) {
      return false;
    }
    limitInt(i1, min, max);
    limitInt(i2, min, max);
    return true;
  }

  /// set pixel in image buffer
  void pixel(int y, int x, ColorData const &cd);

  /// draw horizontal line to image buffer
  void lineHor(int y, int x1, int x2, ColorData const &cd);

  /// draw vertical line to image buffer
  void lineVert(int y1, int y2, int x, ColorData const &cd);

  /// draw non-filled rectangle to image buffer
  void rect(int y1, int y2, int x1, int x2, ColorData const &cd);

  /// draw filled rectangle to image buffer
  void rectFill(int y1, int y2, int x1, int x2, ColorData const &cd);

  /**
   * @brief draw a small digit (3x5 pixels) to image buffer
   * @param[in] topY y coordinate of top of digit
   * @param[in] leftX x coordinate of left of digit
   * @param[in] digit to draw ('0'..'9')
   * @param[in] cd color to use for drawing
   */
  void digit3x5(int topY, int leftX, char digit, ColorData const &cd);

  /**
   * @brief draw small digits (3x5 each, 1 pixel gap) to image buffer
   * @param[in] topY y coordinate of top of digits
   * @param[in] leftX x coordinate of left of digits
   * @param[in] digits to draw (string of '0'..'9')
   * @param[in] cd color to use for drawing
   */
  void digits3x5(int topY, int leftX, std::string const &digits,
                 ColorData const &cd);

  /**
   * @brief draw number using 3x5 digits to image buffer
   * @param[in] anchorY y coordinate of anchor point
   * @param[in] anchorX x coordinate of anchor point
   * @param[in] alignY y alignment, -1 for top, 0 for center, 1 for bottom
   * @param[in] alignX x alignment, -1 for left, 0 for center, 1 for right
   * @param[in] number non-negative number to draw
   * @param[in] cd color to use for drawing
   */
  void number3x5(int anchorY, int anchorX, int alignY, int alignX,
                 int number, ColorData const &cd);

  /// process update of color file, return true on update
  bool colorUpdate(ColorFile &colorFile, ColorData &data) const;

  /// convert color to raw color data
  void color2data(ColorFile const &colorFile, ColorData &data) const;

  /// process update of value file, return true on update
  static bool valueUpdate(UIntFile &valueFile, ValueDescr const &descr,
                          unsigned int &value);

  /// get value from value file
  static void valueFromFile(UIntFile &valueFile, ValueDescr const &descr,
                            unsigned int &value);

  /// send current image buffer as frame to output stream
  void sendFrame();

private:
  /// (re-)create image buffer
  void createImgBuf();

  /// tear down image buffer
  void destroyImgBuf();

protected:
  FormatFile    m_fileFormat;          ///< format file for output
  ColorFile     m_fileBackgroundColor; ///< color file for background color
  OutStreamFile m_fileOutStream;       ///< output stream name file
  int           m_height;              ///< height of image buffer
  int           m_width;               ///< width of image buffer
  int           m_channels;            ///< number of channels of image buffer
  ColorData     m_imgBuf;              ///< image buffer (empty if none)
  ColorData     m_backgroundColor;     ///< background color
}; // class Canvas

} // namespace Blinker

#endif // #ifndef BLINKER_GAME_H

