/************************************************************************
 **
 **  @file   vmeasurements.h
 **  @author Roman Telezhynskyi <dismine(at)gmail.com>
 **  @date   14 7, 2015
 **
 **  @brief
 **  @copyright
 **  This source code is part of the Valentina project, a pattern making
 **  program, whose allow create and modeling patterns of clothing.
 **  Copyright (C) 2015 Valentina project
 **  <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
 **
 **  Valentina is free software: you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License as published by
 **  the Free Software Foundation, either version 3 of the License, or
 **  (at your option) any later version.
 **
 **  Valentina is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License
 **  along with Valentina.  If not, see <http://www.gnu.org/licenses/>.
 **
 *************************************************************************/

#ifndef VMEASUREMENTS_H
#define VMEASUREMENTS_H

#include <qcompilerdetection.h>
#include <QCoreApplication>
#include <QDomElement>
#include <QString>
#include <QStringList>
#include <QtGlobal>

#include "../ifc/xml/vdomdocument.h"
#include "../vmisc/def.h"
#include "vdimensions.h"

class VContainer;

enum class GenderType : qint8 { Male, Female, Unknown };

class VMeasurements : public VDomDocument
{
    Q_DECLARE_TR_FUNCTIONS(VMeasurements)
public:
    explicit VMeasurements(VContainer *data);
    VMeasurements(Unit unit, VContainer *data);
    VMeasurements(Unit unit, const QVector<MeasurementDimension_p > &dimensions,
                  VContainer *data);
    virtual ~VMeasurements() Q_DECL_EQ_DEFAULT;

    virtual void setXMLContent(const QString &fileName) override;
    virtual bool SaveDocument(const QString &fileName, QString &error) override;

    void AddEmpty(const QString &name, const QString &formula = QString());
    void AddEmptyAfter(const QString &after, const QString &name, const QString &formula = QString());
    void Remove(const QString &name);
    void MoveTop(const QString &name);
    void MoveUp(const QString &name);
    void MoveDown(const QString &name);
    void MoveBottom(const QString &name);

    void StoreNames(bool store);

    void ReadMeasurements(qreal baseA, qreal baseB=0, qreal baseC=0) const;
    void ClearForExport();

    MeasurementsType Type() const;
    qreal DimensionABase() const;
    qreal DimensionBBase() const;
    qreal DimensionCBase() const;

    qreal DimensionAStep() const;
    qreal DimensionBStep() const;
    qreal DimensionCStep() const;

    QString Notes() const;
    void    SetNotes(const QString &text);

    QString Customer() const;
    void    SetCustomer(const QString &text);

    QDate   BirthDate() const;
    void    SetBirthDate(const QDate &date);

    GenderType Gender() const;
    void       SetGender(const GenderType &gender);

    QString PMSystem() const;
    void    SetPMSystem(const QString &system);

    QString Email() const;
    void    SetEmail(const QString &text);

    bool    IsReadOnly() const;
    void    SetReadOnly(bool ro);

    bool    IsFullCircumference() const;
    void    SetFullCircumference(bool fc);

    void SetMName(const QString &name, const QString &text);
    void SetMValue(const QString &name, const QString &text);
    void SetMBaseValue(const QString &name, double value);
    void SetMShiftA(const QString &name, double value);
    void SetMShiftB(const QString &name, double value);
    void SetMShiftC(const QString &name, double value);
    void SetMSpecialUnits(const QString &name, bool special);
    void SetMCorrectionValue(const QString &name, qreal baseA, qreal baseB, qreal baseC, double value);
    void SetMDescription(const QString &name, const QString &text);
    void SetMFullName(const QString &name, const QString &text);
    void SetMDimension(const QString &name, IMD type);

    QString MeasurementForDimension(IMD type) const;

    auto Dimensions() const -> QMap<MeasurementDimension, MeasurementDimension_p >;

    auto GetRestrictions() const -> QMap<QString, VDimensionRestriction >;
    void SetRestrictions(const QMap<QString, VDimensionRestriction > &restrictions);
    auto Restriction(qreal base, qreal base2=0) const -> VDimensionRestriction;

    void SetDimensionLabels(const QMap<MeasurementDimension, DimesionLabels> &labels);

    static const QString TagVST;
    static const QString TagVIT;
    static const QString TagBodyMeasurements;
    static const QString TagNotes;
    static const QString TagPersonal;
    static const QString TagCustomer;
    static const QString TagBirthDate;
    static const QString TagGender;
    static const QString TagPMSystem;
    static const QString TagEmail;
    static const QString TagReadOnly;
    static const QString TagMeasurement;
    static const QString TagDimensions;
    static const QString TagDimension;
    static const QString TagRestrictions;
    static const QString TagRestriction;
    static const QString TagCorrections;
    static const QString TagCorrection;
    static const QString TagLabels;
    static const QString TagLabel;

    static const QString AttrBase;
    static const QString AttrValue;
    static const QString AttrShiftA;
    static const QString AttrShiftB;
    static const QString AttrShiftC;
    static const QString AttrCorrection;
    static const QString AttrCoordinates;
    static const QString AttrExclude;
    static const QString AttrSpecialUnits;
    static const QString AttrDescription;
    static const QString AttrName;
    static const QString AttrFullName;
    static const QString AttrMin;
    static const QString AttrMax;
    static const QString AttrStep;
    static const QString AttrCircumference;
    static const QString AttrFullCircumference;
    static const QString AttrLabel;
    static const QString AttrDimension;

    static const QString GenderMale;
    static const QString GenderFemale;
    static const QString GenderUnknown;

    static const QString DimensionN;
    static const QString DimensionX;
    static const QString DimensionY;
    static const QString DimensionW;
    static const QString DimensionZ;

    static QString GenderToStr(const GenderType &sex);
    static GenderType StrToGender(const QString &sex);

    static QString DimensionTypeToStr(const MeasurementDimension &type);
    static MeasurementDimension StrToDimensionType(const QString &type);

    static QString IMDToStr(const IMD &type);
    static IMD StrToIMD(const QString &type);

    static QString IMDName(IMD type);

    QStringList ListAll() const;
    QStringList ListKnown() const;

    bool IsDefinedKnownNamesValid() const;

    VContainer *GetData() const;

private:
    Q_DISABLE_COPY(VMeasurements)

    /** @brief data container with data. */
    VContainer     *data;
    MeasurementsType type;

    /** @brief m_keepNames store names in container to check uniqueness. */
    bool m_keepNames{true};

    void CreateEmptyMultisizeFile(Unit unit, const QVector<MeasurementDimension_p > &dimensions);
    void CreateEmptyIndividualFile(Unit unit);

    QDomElement CreateDimensions(const QVector<MeasurementDimension_p > &dimensions);

    qreal UniqueTagAttr(const QString &tag, const QString &attr, qreal defValue) const;

    QDomElement MakeEmpty(const QString &name, const QString &formula);
    QDomElement FindM(const QString &name) const;
    MeasurementsType ReadType() const;

    qreal EvalFormula(VContainer *data, const QString &formula, bool *ok) const;

    QString ClearPMCode(const QString &code) const;

    QMap<QString, qreal> ReadCorrections(const QDomElement &mElement) const;
    void                 WriteCorrections(QDomElement &mElement, const QMap<QString, qreal> &corrections);

    void           SaveDimesionLabels(QDomElement &dElement, const DimesionLabels &labels);
    DimesionLabels ReadDimensionLabels(const QDomElement &dElement) const;

    void ClearDimension(IMD type);
};

#endif // VMEASUREMENTS_H
