/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Author: pgess <v.melnychenko@xreate.org>
 * Created on June 25, 2018, 12:14 PM
 *
 * \file    latex.cpp
 * \brief   latex
 */

#include "query/latex.h"
using namespace std;

namespace xreate{
namespace latex{

void
LatexQuery::init(TranscendLayer* transcend) {
    __transcend = transcend;

    //schema: latex_fn_demand(Fn, Subject)
    StaticModel data = __transcend->query("latex_fn_demand");
    for(const auto& entry : data) {
        string fnName, subject;
        tie(fnName, subject) = __transcend->parse<string, string>(entry.second);
        __demand[fnName].push_back(subject);
    }

    //schema: Scope, Subject, Decision
    data = __transcend->query("latex_decision");
    for(const auto& entry : data) {
        ScopePacked scope;
        string subject;
        Gringo::Symbol decision;

        tie(scope, subject, decision) = __transcend->parse<ScopePacked, string, Gringo::Symbol>(entry.second);
        __decisions[make_pair(scope, subject)] = decision;
    }

    //schema: Subject, Decision
    data = __transcend->query("latex_registered_subjects");
    for(const auto& entry : data) {
        string subject;
        Gringo::Symbol decision;

        tie(subject, decision) = __transcend->parse<string, Gringo::Symbol>(entry.second);
        __domains[subject].push_back(decision);
    }
}

Demand
LatexQuery::getFnDemand(const std::string& fnName) {
    if (!__demand.count(fnName)) return Demand();
    return __demand.at(fnName);
}

Gringo::Symbol
LatexQuery::getDecision(const std::string& subject, const CodeScope* scopeCaller) {
    ScopePacked scopeP = __transcend->pack(scopeCaller);

    if(__decisions.count(make_pair(scopeP, subject))){
        //found static decision
        return __decisions.at(make_pair(scopeP, subject));
    }

    //... <- вытащить late decision
}

std::list<Gringo::Symbol>
LatexQuery::getSubjectDomain(const std::string& subject) {
    assert(__domains.count(subject));
    return __domains.at(subject);
}

}
}