// Copyright (c) 1997 by Jim Lynch.
// Copyright (c) 2010-2012 by Philipp Schafft.
// This software comes with NO WARRANTY WHATSOEVER.
//
// This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; version 2 dated June, 1991, or, at your
//    option, any LATER version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this software; see the file COPYING.  If not, write to
//    the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//    Boston, MA 02110-1301, USA.

#ifndef Question_h
#define Question_h

#include "Record.h"

#include "str-conversions.h"

#include <sstream>

class Question : public Record
{
private:
  std::string *question;
  std::string *yesKey;
  std::string *noKey;

  void Set
    (
      const char *aQuestion,
      const char *aYesKey,
      const char *aNoKey
    )
    {
      question = new std::string(aQuestion);
      yesKey = new std::string(aYesKey);
      noKey = new std::string(aNoKey);
    }

  void Copy(const Question &orig)
    {
      question = yesKey = noKey = 0;

      if(orig.question)
	question = new std::string(*orig.question);

      if(orig.yesKey)
	yesKey = new std::string(*orig.yesKey);

      if(orig.noKey)
	noKey = new std::string(*orig.noKey);
    }

  void Default(void)
    {
      question = new std::string;
      yesKey = new std::string;
      noKey = new std::string;
    }

protected:
  virtual void Destroy(void)
    {
      if(question)    delete question;
      if(yesKey)      delete yesKey;
      if(noKey)       delete noKey;

      question = yesKey = noKey = 0;
    }

  // Warning:  You must call Destroy() before calling 
  // any method starting with "Set" in this class,
  // -unless- you are initializing the object.
  virtual void SetNull(void)
    {
      question = new std::string;
      noKey = new std::string;
      yesKey = new std::string;
    }
  
  virtual void SetFromDataString(const char *theDataString)
    {
      const char *data = theDataString;

      if(data[0] == 'q')
      {
	std::istringstream it(theDataString + 2);
	std::ostringstream theQuestion;
	std::ostringstream theYesKey;
	std::ostringstream theNoKey;

	// grab the question text
	char nextCh = it.get();
	
	while(nextCh != '|')
	{
	  theQuestion << nextCh;

	  nextCh = it.get();
	}

	question = new std::string(MakeCStringCopy(theQuestion));

	// grab the yesKey text
	nextCh = it.get();
	
	while(nextCh != '|')
	{
	  theYesKey << nextCh;

	  nextCh = it.get();
	}

	yesKey = new std::string(MakeCStringCopy(theYesKey));
	
	// grab the noKey text
	nextCh = it.get();
	
	while(nextCh != '|')
	{
	  theNoKey << nextCh;

	  nextCh = it.get();
	}

	noKey = new std::string(MakeCStringCopy(theNoKey));
      }
      else
      {
	SetNull();
      }
    }
  
public:
  Question()
    {
      Default();
    }
  
  Question(const Question &orig)
    {
      Copy(orig);
    }

  Question
    (
      const char *aQuestion,
      const char *aYesKey,
      const char *aNoKey
    )
    {
      Set(aQuestion, aYesKey, aNoKey);
    }

  virtual ~Question() 
    {
      Destroy();
    }

  Question &operator=(const Question &orig)
    {
      Destroy();
      Copy(orig);

      return *this;
    }
  
  Question(Db &handle, const std::string &theKey)
    {
      Default();
      
      Read(handle, theKey);
    }

  std::string GetYesKey(void)
    {
      return *yesKey;
    }

  std::string GetNoKey(void)
    {
      return *noKey;
    }
  
  virtual const char *FormNewDataCString() const
    {
      std::string temp = "q|";

      if(question)
	temp += (*question);

      temp += "|";

      if(yesKey)
	temp += (*yesKey);

      temp += "|";

      if(noKey)
	temp += (*noKey);

      temp += "|";

      return MakeCStringCopy(temp);
    }

  virtual void Print(std::ostream &out) const
    {
      out << (question ? (*question) : std::string());
    }
};

#endif
