#include "step.h"
#include <QDebug>


class PcbCamStepData : public QSharedData
{
public:
    PcbCamStepData() { }
    PcbCamStepData(const PcbCamStepData &other): QSharedData(other),
        stepName(other.stepName),
        stepAttrMap(other.stepAttrMap),
        origin(other.origin),
        datum(other.datum),
        repeats(other.repeats),
        activeMargin(other.activeMargin),
        profile(other.profile),
        edaPackageSymbols(other.edaPackageSymbols) { }

    ~PcbCamStepData() {
        qDeleteAll(edaPackageSymbols);
    }

    //Step名称
    QString stepName;

    //Step属性
    QVariantMap stepAttrMap;

    //原点
    QPointF origin;

    //datum点
    QPointF datum;

    //Step Repeat
    QList<PcbCamStepRepeat> repeats;

    //Active Margin;
    QMarginsF activeMargin;

    //Profile
    QPainterPath profile;


    //EDA Package Symbols;
    QList<PcbCamSymbolUser*> edaPackageSymbols;

};

PcbCamStep::PcbCamStep() : d(new PcbCamStepData)
{

}

PcbCamStep::PcbCamStep(const PcbCamStep &rhs) : d(rhs.d)
{

}

PcbCamStep &PcbCamStep::operator=(const PcbCamStep &rhs)
{
    if (this != &rhs)
        d.operator=(rhs.d);
    return *this;
}

PcbCamStep::~PcbCamStep()
{

}

QString PcbCamStep::name() const
{
    return d->stepName;
}

void PcbCamStep::setName(const QString &iName)
{
    d->stepName = iName.toLower();
}

QVariant PcbCamStep::attr(const QString &iName) const
{
    return d->stepAttrMap.value(iName.toLower());
}

bool PcbCamStep::hasAttr(const QString &iName) const
{
    return d->stepAttrMap.contains(iName.toLower());
}

void PcbCamStep::initAttr(const QVariantMap &iAttrMap)
{
    d->stepAttrMap = iAttrMap;
}

void PcbCamStep::setAttr(const QString &iName, const QVariant &iValue)
{
    d->stepAttrMap.insert(iName.toLower(), iValue);
}

QPointF PcbCamStep::origin() const
{
    return d->origin;
}

void PcbCamStep::setOrigin(const QPointF &iOrigin)
{
    d->origin = iOrigin;
}

QPointF PcbCamStep::datum() const
{
    return d->datum;
}

void PcbCamStep::setDatum(const QPointF &iDatum)
{
    d->datum = iDatum;
}

QMarginsF PcbCamStep::activeMargin() const
{
    return d->activeMargin;
}

void PcbCamStep::setActiveMargin(const QMarginsF &iMargin)
{
    d->activeMargin = iMargin;
}

QPainterPath PcbCamStep::profile() const
{
    return d->profile;
}

void PcbCamStep::setProfile(const QPainterPath &iProfile)
{
    d->profile = iProfile;
}

QList<PcbCamStepRepeat> PcbCamStep::repeats() const
{
    return d->repeats;
}

PcbCamStepRepeat PcbCamStep::repeat(int iIndex) const
{
    return d->repeats.value(iIndex);
}

void PcbCamStep::addRepeat(const PcbCamStepRepeat &iRepeat)
{
    d->repeats.append(iRepeat);
}

void PcbCamStep::clearRepeats()
{
    d->repeats.clear();
}

QList<PcbCamStepRepeat> PcbCamStep::getBreakedRepeats() const
{
    QList<PcbCamStepRepeat> res;
    for (const PcbCamStepRepeat &sr : d->repeats) {
        for (uint i = 0; i < sr.nx(); i++) {
            for (uint j = 0; j < sr.ny(); j++) {
                PcbCamStepRepeat rpt;
                rpt.setId(sr.id());
                rpt.setStep(sr.step());
                rpt.setX(sr.x() +  sr.dx() * i);
                rpt.setY(sr.y()  + sr.dy() * j);
                rpt.setDx(0.0);
                rpt.setDy(0.0);
                rpt.setNx(1);
                rpt.setNy(1);
                rpt.setAngle(sr.angle());
                rpt.setMirror(sr.mirror());
                rpt.setSelected(sr.isSelected());
                res.append(rpt);
            }
        }
    }
    return res;
}

void PcbCamStep::setEdaPackageSymbols(const QList<PcbCamSymbolUser *> iPkgSymbols)
{
    d->edaPackageSymbols = iPkgSymbols;
}

QList<PcbCamSymbolUser *> PcbCamStep::edaPackageSymbols() const
{
    return d->edaPackageSymbols;
}

