// Aseprite
// Copyright (C) 2017  David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "app/app.h"
#include "app/cmd/remove_slice.h"
#include "app/cmd/set_slice_key.h"
#include "app/commands/command.h"
#include "app/context_access.h"
#include "app/document_api.h"
#include "app/modules/gui.h"
#include "app/ui/status_bar.h"
#include "app/transaction.h"
#include "base/convert_to.h"
#include "doc/slice.h"
#include "doc/sprite.h"
#include "ui/alert.h"
#include "ui/widget.h"

namespace app {

class RemoveSliceCommand : public Command {
public:
  RemoveSliceCommand();
  Command* clone() const override { return new RemoveSliceCommand(*this); }

protected:
  void onLoadParams(const Params& params) override;
  bool onEnabled(Context* context) override;
  void onExecute(Context* context) override;

private:
  std::string m_sliceName;
  ObjectId m_sliceId;
};

RemoveSliceCommand::RemoveSliceCommand()
  : Command("RemoveSlice",
            "Remove Slice",
            CmdRecordableFlag)
{
}

void RemoveSliceCommand::onLoadParams(const Params& params)
{
  m_sliceName = params.get("name");

  std::string id = params.get("id");
  if (!id.empty())
    m_sliceId = ObjectId(base::convert_to<doc::ObjectId>(id));
  else
    m_sliceId = NullId;
}

bool RemoveSliceCommand::onEnabled(Context* context)
{
  return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
                             ContextFlags::HasActiveSprite |
                             ContextFlags::HasActiveLayer);
}

void RemoveSliceCommand::onExecute(Context* context)
{
  const ContextReader reader(context);
  const Sprite* sprite = reader.sprite();
  frame_t frame = reader.frame();
  const Slice* foundSlice = nullptr;

  if (!m_sliceName.empty())
    foundSlice = sprite->slices().getByName(m_sliceName);
  else if (m_sliceId != NullId)
    foundSlice = sprite->slices().getById(m_sliceId);

  if (!foundSlice)
    return;

  std::string sliceName = foundSlice->name();

  {
    ContextWriter writer(reader, 500);
    Document* document(writer.document());
    Sprite* sprite(writer.sprite());
    Transaction transaction(writer.context(), "Remove Slice");
    Slice* slice = const_cast<Slice*>(foundSlice);
    
    if (slice->size() > 1) {
      transaction.execute(new cmd::SetSliceKey(slice, frame, SliceKey()));
    }
    else {
      transaction.execute(new cmd::RemoveSlice(sprite, slice));
    }
    transaction.commit();
    document->notifyGeneralUpdate();
  }

  StatusBar::instance()->invalidate();
  if (!sliceName.empty())
    StatusBar::instance()->showTip(1000, "Slice '%s' removed", sliceName.c_str());
  else
    StatusBar::instance()->showTip(1000, "Slice removed");
}

Command* CommandFactory::createRemoveSliceCommand()
{
  return new RemoveSliceCommand;
}

} // namespace app
