#pragma once

#include <array>
#include <string>

#include <frostbite2D/2d/actor.h>
#include <frostbite2D/2d/sprite.h>

namespace frostbite2D {

/**
 * @brief Composes 9 consecutive NPK frames into a resizable nine-slice panel.
 */
class NineSliceActor : public Actor {
public:
  NineSliceActor();
  ~NineSliceActor() override = default;

  static Ptr<NineSliceActor> createFromNpk(const std::string& imgPath,
                                           size_t startIndex = 0);

  bool SetSlices(const std::string& imgPath, size_t startIndex);
  void SetSize(const Vec2& size);
  void SetSize(float width, float height);

  const std::string& GetImgPath() const { return imgPath_; }
  size_t GetStartIndex() const { return startIndex_; }

private:
  enum SliceIndex : size_t {
    TopLeft = 0,
    Top = 1,
    TopRight = 2,
    Left = 3,
    Center = 4,
    Right = 5,
    BottomLeft = 6,
    Bottom = 7,
    BottomRight = 8,
    SliceCount = 9
  };

  void updateLayout();
  void applySliceLayout(Ptr<Sprite> sprite, const Vec2& position,
                        const Vec2& size);
  Vec2 getNaturalSize() const;

  std::string imgPath_;
  size_t startIndex_ = 0;
  std::array<Ptr<Sprite>, SliceCount> sliceSprites_;
  std::array<Vec2, SliceCount> sliceNaturalSizes_ = {};
};

} // namespace frostbite2D
