/*
  Copyright 2010-2012 Grégory Soutadé

  This file is part of KissCount.

  KissCount 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 3 of the License, or
  (at your option) any later version.

  KissCount 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 KissCount.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef USER_H
#define USER_H

#include <map>
#include <vector>

#include <QFont>

#include "Category.hpp"
#include "Account.hpp"
#include "Operation.hpp"
#include "Database.hpp"
#include "import/ImportEngine.hpp"

class Database;
class ImportPattern;

class User
{
public:
    User(Database* db);
    ~User();
    void InvalidateOperations();

    int _id;
    QString _name;
    QString _password;
    std::vector<Account> _accounts;
    std::map<unsigned int, std::map<unsigned int, std::vector<Operation> >* > _operations;
    std::vector<Category> _categories;
    std::vector<QFont> _categoriesFonts;
    std::map<QString, QString> _preferences;
    std::map<QString, ImportPattern> _importPatterns;

    class AccountNotFound {};
    class CategoryNotFound {};

    Category GetCategory(int catId);
    QString GetCategoryName(int catId);
    int GetCategoryId(const QString& catName) throw (CategoryNotFound);
    const QFont GetCategoryFont(int catId);
    void AddCategory(const Category& cat);
    void UpdateCategory(const Category& cat);
    void DeleteCategory(const Category& cat);
    

    Account GetAccount(int accountId) throw (AccountNotFound);
    QString GetAccountName(int accountId);
    int GetAccountId(const QString& accountName) throw (AccountNotFound);
    int GetAccountIdFromAccountNumber(const QString& accountNumber) throw (AccountNotFound);
    void AddAccount(Account& ac);
    void UpdateAccount(Account& ac);
    void DeleteAccount(Account& ac);


    int GetCategoriesNumber();
    int GetAccountsNumber();
    int GetOperationsNumber(int month, int year);

    QString GetLanguage();

    void LinkOrUnlinkOperation(Operation& op);

    void Group(const Operation& op);
    bool Group(std::vector<Operation>* ops, const Operation& op);
    void UnGroup(const Operation& op);
    void ResolveGroups(int year);

    void UpdateImportPattern(User* user);
private:
    Database* _db;
};

#endif