//
// Created by Иван_Архипов on 17.11.2017.
//

#ifndef LOTRO_DAT_PATCHER_DATABASE_H
#define LOTRO_DAT_PATCHER_DATABASE_H

#include "sqlite/sqlite3.h"
#include <string>

extern  "C++"
{
namespace LOTRO_DAT
{
    class BinaryData;

    class Database {
    public:
        Database();
        explicit Database(const char *filename);
        explicit Database(const std::string &filename);

        ~Database();

        void InitDatabase(const char* filename);

        void PushTextFile(long long file_id, long long gossip_id, char* text, char* args, int dat_id = 0);
        //BinaryData& GetTextFile(long long file_id);

        void PushBinaryFile(long long file_id, const BinaryData& data);
        //BinaryData& GetBinaryFile(long long file_id);

        void AddMetadata();

    private:
        void ExecSql(const char *sql);

        sqlite3* db_;
        sqlite3_stmt* insert_text_;
        sqlite3_stmt* insert_binary_;

        sqlite3_stmt* get_text_;
        sqlite3_stmt* get_binary_;

        unsigned long long query_size_;
        const unsigned long long QUERY_MAX_SIZE = 256000;

        const char* CreateBinaryTableCommand_ = "CREATE TABLE IF NOT EXISTS `binary_data` ( "
            "`file_id` INTEGER NOT NULL DEFAULT '0', "
            "`data` BLOB, "
            "PRIMARY KEY (`file_id`));";

        const char* CreateTextTableCommand_ = "CREATE TABLE IF NOT EXISTS `text_data` ( "
            "`file_id` INTEGER NOT NULL DEFAULT '0', "
            "`gossip_id` INTEGER NOT NULL DEFAULT '0', "
            "`content` TEXT, "
            "`args` TEXT, "
            "PRIMARY KEY (`file_id`, `gossip_id`));";

        const char* CreateMetadataTableCommand_ = "CREATE TABLE IF NOT EXISTS `meta_data` ( "
            "`name` TEXT NOT NULL, "
            "`description` TEXT NOT NULL, "
            "`date` TEXT NOT NULL, "
            "`author` TEXT NOT NULL, "
            "`version` TEXT NOT NULL, "
            "`link` TEXT NOT NULL, "
            "`content_type` TEXT NOT NULL, "
            "PRIMARY KEY (`version`));";

        const char* InsertTextCommand_ = "INSERT INTO text_data "
                "(file_id, gossip_id, content, args) "
                "VALUES (?, ?, ?, ?); ";

        const char* InsertBinaryCommand_ = "INSERT INTO binary_data "
                "(file_id, data) "
                "VALUES (?, ?); ";

    };
}
}
#endif