#pragma once

#include "data/pattern.h"

#include <QUndoCommand>

namespace Xybrid::Editing {
    class CompositeCommand;

    class PatternCommand : public QUndoCommand {
        friend class CompositeCommand;
    protected:
        std::shared_ptr<Data::Pattern> pattern;
        bool composed = false;

    public:
        bool commit();
        bool cancel();
    };

    class PatternDeltaCommand : public PatternCommand {
        int ch, rw;
        Data::Pattern::Row oldRow;
    public:
        Data::Pattern::Row row;

        PatternDeltaCommand(const std::shared_ptr<Data::Pattern>& pattern, int channel, int row);
        ~PatternDeltaCommand() override = default;

        int id() const override { return 2000; }

        bool mergeWith(const QUndoCommand*) override;
        void redo() override;
        void undo() override;
    };

    class PatternRenameCommand : public PatternCommand {
        std::string from, to;

    public:
        PatternRenameCommand(const std::shared_ptr<Data::Pattern>& pattern, const std::string& to);
        ~PatternRenameCommand() override = default;

        int id() const override { return 2070; }

        bool mergeWith(const QUndoCommand*) override;
        void redo() override;
        void undo() override;
    };

    class PatternChannelMoveCommand : public PatternCommand {
        int from, to;

    public:
        PatternChannelMoveCommand(const std::shared_ptr<Data::Pattern>& pattern, int from, int to);
        ~PatternChannelMoveCommand() override = default;

        int id() const override { return 2100; }

        bool mergeWith(const QUndoCommand*) override;
        void redo() override;
        void undo() override;
    };

    class PatternChannelRenameCommand : public PatternCommand {
        int idx;
        std::string from, to;

    public:
        PatternChannelRenameCommand(const std::shared_ptr<Data::Pattern>& pattern, int channel, const std::string& to);
        ~PatternChannelRenameCommand() override = default;

        int id() const override { return 2101; }

        bool mergeWith(const QUndoCommand*) override;
        void redo() override;
        void undo() override;
    };

    class PatternChannelAddCommand : public PatternCommand {
        int idx;

    public:
        PatternChannelAddCommand(const std::shared_ptr<Data::Pattern>& pattern, int channel);
        ~PatternChannelAddCommand() override = default;

        void redo() override;
        void undo() override;
    };

    class PatternChannelDeleteCommand : public PatternCommand {
        int idx;
        Data::Pattern::Channel ch;

    public:
        PatternChannelDeleteCommand(const std::shared_ptr<Data::Pattern>& pattern, int channel);
        ~PatternChannelDeleteCommand() override = default;

        void redo() override;
        void undo() override;
    };
}
