/*
 * Copyright (c) 2015, University of Kaiserslautern
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors:
 *    Janik Schlemminger
 *    Robert Gernhardt
 *    Matthias Jung
 */

#include "phasefactory.h"
#include <exception>
#include "phase.h"
#include "businessObjects/transaction.h"
#include "data/tracedb.h"
#include "businessObjects/timespan.h"

using namespace std;

shared_ptr<Phase> PhaseFactory::CreatePhase(ID id, const QString &dbPhaseName,
                                            const Timespan &span, const shared_ptr<Transaction> &trans, TraceDB &database)
{
    traceTime clk = database.getGeneralInfo().clkPeriod;

    if (dbPhaseName == "REQ")
        return shared_ptr<Phase>(new REQ(id, span, trans, {},
                                         std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "RESP")
        return shared_ptr<Phase>(new RESP(id, span, trans, {},
                                          std::shared_ptr<Timespan>()));

    else if (dbPhaseName == "PREB") return shared_ptr<Phase>(new PREB(id, span,
                                                                          trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                                                          std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "PRE")
        return shared_ptr<Phase>(new PRE(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                         std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "ACTB") return shared_ptr<Phase>(new ACTB(id, span,
                                                                          trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                                                          std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "ACT")
        return shared_ptr<Phase>(new ACT(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                         std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "PRE_ALL")
        return shared_ptr<Phase>(new PRECHARGE_ALL(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                                   std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "REFA")
        return shared_ptr<Phase>(new REFA(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                          std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "REFB")
        return shared_ptr<Phase>(new REFB(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                          std::shared_ptr<Timespan>()));

    else if (dbPhaseName == "RD")
        return shared_ptr<Phase>(new RD(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                        std::shared_ptr<Timespan>
                                        (new Timespan(trans->SpanOnDataStrobe()))));
    else if (dbPhaseName == "RDA")
        return shared_ptr<Phase>(new RDA(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                         std::shared_ptr<Timespan>
                                         (new Timespan(trans->SpanOnDataStrobe()))));
    else if (dbPhaseName == "WR")
        return shared_ptr<Phase>(new WR(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                        std::shared_ptr<Timespan>
                                        (new Timespan(trans->SpanOnDataStrobe()))));
    else if (dbPhaseName == "WRA")
        return shared_ptr<Phase>(new WRA(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk)},
                                         std::shared_ptr<Timespan>
                                         (new Timespan(trans->SpanOnDataStrobe()))));
    else if (dbPhaseName == "PDNA")
        return shared_ptr<Phase>(new PDNA(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk), Timespan(span.End() - clk, span.End())},
                                          std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "PDNAB")
        return shared_ptr<Phase>(new PDNAB(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk), Timespan(span.End() - clk, span.End())},
                                           std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "PDNP")
        return shared_ptr<Phase>(new PDNP(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk), Timespan(span.End() - clk, span.End())},
                                          std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "PDNPB")
        return shared_ptr<Phase>(new PDNPB(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk), Timespan(span.End() - clk, span.End())},
                                           std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "SREF")
        return shared_ptr<Phase>(new SREF(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk), Timespan(span.End() - clk, span.End())},
                                          std::shared_ptr<Timespan>()));
    else if (dbPhaseName == "SREFB")
        return shared_ptr<Phase>(new SREFB(id, span, trans, {Timespan(span.Begin(), span.Begin() + clk), Timespan(span.End() - clk, span.End())},
                                           std::shared_ptr<Timespan>()));
    else
        throw std::runtime_error("DB phasename " + dbPhaseName.toStdString() +
                                 " unkown to phasefactory");
}
