/*
 * 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
 */

#ifndef BANKPHASE_H
#define BANKPHASE_H
#include "presentation/util/colorgenerator.h"
#include "presentation/tracedrawingproperties.h"
#include "businessObjects/timespan.h"
#include <QString>
#include <QPainter>
#include <qwt_scale_map.h>
#include <vector>
#include <memory>

typedef unsigned int ID;
//enum TextPositioning;
class Transaction;

class Phase
{
public:
    Phase(ID id, Timespan span, const std::shared_ptr<Transaction> &transaction,
          std::vector<Timespan> spansOnCommandBus,
          std::shared_ptr<Timespan> spanOnDataBus):
        id(id), span(span), transaction(transaction),
        spansOnCommandBus(spansOnCommandBus), spanOnDataBus(spanOnDataBus),
        hexagonHeigth(0.6), captionPosition(TextPositioning::bottomRight) {}

    void draw(QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap,
              const QRectF &canvasRect, bool highlight,
              const TraceDrawingProperties &drawingProperties) const;
    bool isSelected(traceTime time, double yVal,
                    const TraceDrawingProperties &drawingproperties) const;
    const Timespan &Span() const
    {
        return span;
    }
    ID Id() const
    {
        return id;
    }
    virtual QString Name() const = 0;
    virtual bool isBankwise() const
    {
        return true;
    }

protected:
    ID id;
    Timespan span;
    std::shared_ptr<Transaction> transaction;
    std::vector<Timespan> spansOnCommandBus;
    std::shared_ptr<Timespan> spanOnDataBus;
    double hexagonHeigth;
    TextPositioning captionPosition;

    enum PhaseSymbol {Hexagon, Rect};
    virtual PhaseSymbol getPhaseSymbol() const;
    virtual Qt::BrushStyle getBrushStyle() const;
    virtual QColor getColor(const TraceDrawingProperties &drawingProperties) const;
    virtual int getYVal(const TraceDrawingProperties &drawingProperties) const;
    virtual QColor getPhaseColor() const = 0;
    virtual void drawPhaseSymbol(traceTime begin, traceTime end, double y,
                                 bool drawtext, PhaseSymbol symbol, QPainter *painter, const QwtScaleMap &xMap,
                                 const QwtScaleMap &yMap) const;

};

class REQ : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(1);
    }
    virtual QString Name() const final
    {
        return "REQ";
    }
    virtual int getYVal(const TraceDrawingProperties &drawingProperties) const
    override
    {
        return drawingProperties.yValRequest;
    }
};


class RESP : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(1);
    }
    virtual QString Name() const override
    {
        return "RESP";
    }
    virtual int getYVal(const TraceDrawingProperties &drawingProperties) const
    override
    {
        return drawingProperties.yValResponse;
    }

};

class PREB: public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(1);
    }
    virtual QString Name() const override
    {
        return "PREB";
    }
};

class PRE : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(1);
    }
    virtual QString Name() const override
    {
        return "PRE";
    }
};

class ACTB : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(3);
    }
    virtual QString Name() const override
    {
        return "ACTB";
    }
};

class ACT : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(3);
    }
    virtual QString Name() const override
    {
        return "ACT";
    }
};

class RD : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(4);
    }
    virtual QString Name() const override
    {
        return "RD";
    }
};

class RDA : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(5);
    }
    virtual QString Name() const override
    {
        return "RDA";
    }
};

class WR : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(6);
    }
    virtual QString Name() const override
    {
        return "WR";
    }
};

class WRA : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(7);
    }
    virtual QString Name() const override
    {
        return "WRA";
    }
};

class AUTO_REFRESH : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QString Name() const override
    {
        return "REF";
    }
    virtual std::vector<traceTime> getTimesOnCommandBus() const
    {
        return {span.Begin()};
    }
    virtual QColor getColor(const TraceDrawingProperties &drawingProperties) const
    override
    {
        Q_UNUSED(drawingProperties) return getPhaseColor();
    }
    virtual QColor getPhaseColor() const override
    {
        QColor phaseColor = QColor(Qt::darkCyan);
        phaseColor.setAlpha(130);
        return phaseColor;
    }
};

class REFA : public AUTO_REFRESH
{
public:
    using AUTO_REFRESH::AUTO_REFRESH;
protected:
    virtual QString Name() const override
    {
        return "REFA";
    }
    virtual bool isBankwise() const override
    {
        return false;
    }
};

class REFB : public AUTO_REFRESH
{
public:
    using AUTO_REFRESH::AUTO_REFRESH;
protected:
    virtual QString Name() const override
    {
        return "REFB";
    }
};


class PRECHARGE_ALL : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QString Name() const override
    {
        return "PREA";
    }
    virtual std::vector<traceTime> getTimesOnCommandBus() const
    {
        return {span.Begin()};
    }
    virtual QColor getColor(const TraceDrawingProperties &drawingProperties) const
    override
    {
        Q_UNUSED(drawingProperties) return getPhaseColor();
    }
    virtual QColor getPhaseColor() const override
    {
        return ColorGenerator::getColor(10);
    }
    virtual bool isBankwise() const override
    {
        return false;
    }
};

class PDNAB : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QString Name() const override
    {
        return "PDNAB";
    }
    virtual Qt::BrushStyle getBrushStyle() const override
    {
        return Qt::Dense6Pattern;
    }
    virtual QColor getColor(const TraceDrawingProperties &drawingProperties) const
    override
    {
        Q_UNUSED(drawingProperties) return getPhaseColor();
    }
    virtual QColor getPhaseColor() const override
    {
        return QColor(Qt::black);
    }
    virtual Phase::PhaseSymbol getPhaseSymbol() const override
    {
        return PhaseSymbol::Rect;
    }
};

class PDNA : public PDNAB
{
public:
    using PDNAB::PDNAB;
protected:
    virtual QString Name() const override
    {
        return "PDNA";
    }
    virtual bool isBankwise() const override
    {
        return false;
    }
};

class PDNPB : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QString Name() const override
    {
        return "PDNPB";
    }
    virtual Qt::BrushStyle getBrushStyle() const override
    {
        return Qt::Dense4Pattern;
    }
    virtual QColor getColor(const TraceDrawingProperties &drawingProperties) const
    override
    {
        Q_UNUSED(drawingProperties) return getPhaseColor();
    }
    virtual QColor getPhaseColor() const override
    {
        return QColor(Qt::black);
    }
    virtual Phase::PhaseSymbol getPhaseSymbol() const override
    {
        return PhaseSymbol::Rect;
    }
};

class PDNP : public PDNPB
{
public:
    using PDNPB::PDNPB;
protected:
    virtual QString Name() const override
    {
        return "PDNP";
    }
    virtual bool isBankwise() const override
    {
        return false;
    }
};

class SREFB : public Phase
{
public:
    using Phase::Phase;
protected:
    virtual QString Name() const override
    {
        return "SREFB";
    }
    virtual Qt::BrushStyle getBrushStyle() const override
    {
        return Qt::Dense1Pattern;
    }
    virtual QColor getColor(const TraceDrawingProperties &drawingProperties) const
    override
    {
        Q_UNUSED(drawingProperties) return getPhaseColor();
    }
    virtual QColor getPhaseColor() const override
    {
        return QColor(Qt::black);
    }
    virtual Phase::PhaseSymbol getPhaseSymbol() const override
    {
        return PhaseSymbol::Rect;
    }
};

class SREF : public SREFB
{
public:
    using SREFB::SREFB;
protected:
    virtual QString Name() const override
    {
        return "SREF";
    }
    virtual bool isBankwise() const override
    {
        return false;
    }
};


#endif // BANKPHASE_H
