/***************************************************************************

                            manage queries

    -----------------------------------------------------------------------

    begin          : Sat Jul 11 20:50:53 MET 1999

    copyright      : (C) 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
                     (C) 2001 The KDE-EDU team
                     (C) 2005 Peter Hedlund <peter.hedlund@kdemail.net>

    -----------------------------------------------------------------------

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef QueryManager_included
#define QueryManager_included

#include <vector>
using namespace std;

#include "kvoctraindoc.h"
#include "prefs.h"

class kvoctrainDoc;
class TDEConfig;
class kvoctrainExpr;
class Prefs;

// internal types, indented are subtypes

#define TQM_VERB           "v"    // go
#define   TQM_VERB_IRR     "ir"
#define   TQM_VERB_REG     "re"
#define TQM_NOUN           "n"    // table, coffee
#define   TQM_NOUN_F       "f"
#define   TQM_NOUN_M       "m"
#define   TQM_NOUN_S       "s"
#define TQM_NAME           "nm"
#define TQM_ART            "ar"   // article
#define   TQM_ART_DEF      "def"  // definite    a/an
#define   TQM_ART_IND      "ind"  // indefinite  the
#define TQM_ADJ            "aj"   // adjective   expensive, good
#define TQM_ADV            "av"   // adverb      today, strongly
#define TQM_PRON           "pr"   // pronoun     you, she
#define   TQM_PRON_POS     "pos"  // possessive  my, your
#define   TQM_PRON_PER     "per"  // personal
#define TQM_PHRASE         "ph"
#define TQM_NUM            "num"  // numeral
#define   TQM_NUM_ORD      "ord"  // ordinal     first, second
#define   TQM_NUM_CARD     "crd"  // cardinal    one, two
#define TQM_INFORMAL       "ifm"
#define TQM_FIG            "fig"
#define TQM_CON            "con"  // conjuncton  and, but
#define TQM_PREP           "pre"  // preposition behind, between
#define TQM_QUEST          "qu"   // question    who, what

// type delimiters

#define TQM_USER_TYPE  "#"   // designates number of user type
#define TQM_TYPE_DIV   ":"   // divide main from subtype

//enum kvq_timeout_t { kvq_notimeout, kvq_show, kvq_cont };

enum QueryType  { TQT_Random, TQT_Multiple,
                  TQT_Articles, TQT_Conjugation, TQT_Comparison,
                  TQT_Synonym, TQT_Antonym, TQT_Example, TQT_Paraphrase };


class TypeRelation
{
 public:

  TypeRelation (const TQString & _short, const TQString & _long)
    : shortId (_short), longId(_long) {}

  TQString shortStr() const { return shortId; }
  TQString longStr()  const { return longId;  }

 protected:

  TQString  shortId, longId;
};


struct QueryEntryRef {
 QueryEntryRef (kvoctrainExpr *_exp,
                int            _nr)
     : exp(_exp), nr(_nr) {}


   kvoctrainExpr *exp;
   int            nr;
};

typedef vector<vector<QueryEntryRef> > QuerySelection;

class TDE_EXPORT QueryManager
{
 public:
                // donīt change the order/remove one of these,
                // just APPEND new types
 enum CompType { DontCare,
                  MoreEqThan, MoreThan,
                  Before, Within,
                  WorseThan, WorseEqThan,
                  EqualTo, NotEqual,
                  LessEqThan, LessThan,
                  BetterThan, BetterEqThan,
                  Current, NotAssigned, NotQueried,
                  Auto_Time, Auto_Count,
                  OneOf, NotOneOf
                };

  QueryManager ();

  //void loadConfig (TDEConfig *);
  //void saveConfig (TDEConfig *);

  static vector<TypeRelation> getRelation (bool only_maintypes);
  static void setTypeNames (vector<TQString> names);
  static TQString getSubType (const TQString & type);
  static TQString getMainType (const TQString & type);

  static TQString compStr(Prefs::EnumCompType::type type);
  static TQString gradeStr(int i);
  static TQString typeStr(const TQString id);

  void setLessonItems(vector<int> indices) { lessonitems = indices; }
  void setLessonItemStr(const TQString & indices);
  //void setDateItem (time_t time) { dateitem = time; }
  //void setQueryItem (int query) { queryitem = query; }
  //void setBadItem (int bad) { baditem = bad; }
  //void setTypeItem (const TQString & type) { typeitem = type; }
  //void setGradeItem (grade_t grade) { gradeitem = grade; }
  //void setBlockItem (int item, int grade);
  //void setExpireItem (int item, int grade);

  vector<int> lessonItems() const { return lessonitems; }
  TQString lessonItemStr() const;
  //time_t dateItem () const { return dateitem; }
  //int queryItem () const { return queryitem; }
  //int badItem () const { return baditem; }
  //TQString typeItem () const { return typeitem; }
  //grade_t gradeItem () const { return gradeitem; }
  //int blockItem (int grade) const;
  //int expireItem (int grade) const;

  //void setDateComp (CompType time) { datecomp = time; }
  //void setLessonComp (CompType less) { lessoncomp = less; }
  //void setQueryComp (CompType query) { querycomp = query; }
  //void setBadComp (CompType bad) { badcomp = bad; }
  //void setTypeComp (CompType type) { typecomp = type; }
  //void setGradeComp (CompType grade) { gradecomp = grade; }

  //CompType dateComp () const { return datecomp; }
  //CompType lessonComp () const { return lessoncomp; }
  //CompType queryComp () const { return querycomp; }
  //CompType badComp () const { return badcomp; }
  //CompType typeComp () const { return typecomp; }
  //CompType gradeComp () const { return gradecomp; }

  bool validate(kvoctrainExpr *expr, int act_lesson,
                int oindex, int tindex);

  QuerySelection select(kvoctrainDoc*, int act_lesson,
                        int oindex, int tindex);

  bool validate(kvoctrainExpr *expr, int act_lesson,
                int index, TQString type);

  QuerySelection select(kvoctrainDoc*, int act_lesson,
                        int index, TQString type);

  bool validate(kvoctrainExpr *expr, int act_lesson,
                int index, QueryType type);

  QuerySelection select(kvoctrainDoc*, int act_lesson,
                        int index, QueryType type);

 protected:

  bool compareBlocking (int grade, int limit, bool use_it);
  bool compareExpiring (int grade, int limit, bool use_it);
  bool compareDate (CompType, time_t, time_t);
  bool compareQuery (CompType, int, int);
  bool compareBad (CompType, int, int);
  bool compareGrade (CompType, grade_t, grade_t);
  bool compareType (CompType, const TQString &, const TQString &);
  bool compareLesson (CompType type, int less, const vector<int> &limit, int current);

 /* CompType    typecomp,
              querycomp,
              badcomp,
              gradecomp,
              lessoncomp,
              datecomp;*/

  //time_t      dateitem;
  //int         queryitem,
  //            baditem;
  //TQString     typeitem;
  //grade_t     gradeitem;
  vector<int> lessonitems;

  //vector<int> blockItems,
  //            expireItems;

 private:
  static vector<TQString> userTypes;
};

#endif // QueryManager_included
