#pragma once

#include <string>

#include <glm/glm.hpp>

#include "anm2/anm2_type.h"
#include "render.h"
#include "types.h"

namespace anm2ed
{
#ifdef _WIN32
  constexpr auto FFMPEG_PATH_DEFAULT = "C:\\ffmpeg\\bin\\ffmpeg.exe";
#else
  constexpr auto FFMPEG_PATH_DEFAULT = "/usr/bin/ffmpeg";
#endif

#define SETTINGS_TYPES                                                                                                 \
  X(INT, int)                                                                                                          \
  X(BOOL, bool)                                                                                                        \
  X(FLOAT, float)                                                                                                      \
  X(STRING, std::string)                                                                                               \
  X(IVEC2, glm::ivec2)                                                                                                 \
  X(IVEC2_WH, glm::ivec2)                                                                                              \
  X(VEC2, glm::vec2)                                                                                                   \
  X(VEC2_WH, glm::vec2)                                                                                                \
  X(VEC3, glm::vec3)                                                                                                   \
  X(VEC4, glm::vec4)

  enum Type
  {
#define X(name, type) name,
    SETTINGS_TYPES
#undef X
  };

#define X(name, type) using TYPE_##name = type;
  SETTINGS_TYPES
#undef X

#define SETTINGS_MEMBERS                                                                                               \
  /* Symbol / Name / String / Type / Default */                                                                        \
  X(WINDOW_SIZE, windowSize, "Window Size", IVEC2_WH, {1600, 900})                                                     \
  X(IS_VSYNC, isVsync, "Vsync", BOOL, true)                                                                            \
  X(UI_SCALE, uiScale, "UI Scale", FLOAT, 1.0f)                                                                        \
  X(THEME, theme, "Theme", INT, types::theme::DARK)                                                                    \
                                                                                                                       \
  X(FILE_IS_AUTOSAVE, fileIsAutosave, "Autosave", BOOL, true)                                                          \
  X(FILE_AUTOSAVE_TIME, fileAutosaveTime, "Autosave Time", INT, 1)                                                     \
  X(FILE_IS_WARN_OVERWRITE, fileIsWarnOverwrite, "Warn on Overwrite", BOOL, true)                                      \
  X(FILE_SNAPSHOT_STACK_SIZE, fileSnapshotStackSize, "Snapshot Stack Size", INT, 50)                                   \
                                                                                                                       \
  X(KEYBOARD_REPEAT_DELAY, keyboardRepeatDelay, "Repeat Delay", FLOAT, 0.300f)                                         \
  X(KEYBOARD_REPEAT_RATE, keyboardRepeatRate, "Repeat Rate", FLOAT, 0.050f)                                            \
                                                                                                                       \
  X(INPUT_ZOOM_STEP, inputZoomStep, "Zoom Step", FLOAT, 50.0f)                                                         \
  X(INPUT_IS_MOVE_TOOL_SNAP_TO_MOUSE, inputIsMoveToolSnapToMouse, "Move Tool: Snap to Mouse", BOOL, false)             \
                                                                                                                       \
  X(PLAYBACK_IS_LOOP, playbackIsLoop, "Loop", BOOL, true)                                                              \
  X(PLAYBACK_IS_CLAMP, playbackIsClamp, "Clamp", BOOL, true)                                                           \
                                                                                                                       \
  X(CHANGE_IS_CROP, changeIsCrop, "##Is Crop", BOOL, false)                                                            \
  X(CHANGE_IS_SIZE, changeIsSize, "##Is Size", BOOL, false)                                                            \
  X(CHANGE_IS_POSITION, changeIsPosition, "##Is Position", BOOL, false)                                                \
  X(CHANGE_IS_PIVOT, changeIsPivot, "##Is Pivot", BOOL, false)                                                         \
  X(CHANGE_IS_SCALE, changeIsScale, "##Is Scale", BOOL, false)                                                         \
  X(CHANGE_IS_ROTATION, changeIsRotation, "##Is Rotation", BOOL, false)                                                \
  X(CHANGE_IS_DELAY, changeIsDelay, "##Is Delay", BOOL, false)                                                         \
  X(CHANGE_IS_TINT, changeIsTint, "##Is Tint", BOOL, false)                                                            \
  X(CHANGE_IS_COLOR_OFFSET, changeIsColorOffset, "##Is Color Offset", BOOL, false)                                     \
  X(CHANGE_IS_VISIBLE_SET, changeIsVisibleSet, "##Is Visible", BOOL, false)                                            \
  X(CHANGE_IS_INTERPOLATED_SET, changeIsInterpolatedSet, "##Is Interpolated", BOOL, false)                             \
  X(CHANGE_CROP, changeCrop, "Crop", VEC2, {})                                                                         \
  X(CHANGE_SIZE, changeSize, "Size", VEC2, {})                                                                         \
  X(CHANGE_POSITION, changePosition, "Position", VEC2, {})                                                             \
  X(CHANGE_PIVOT, changePivot, "Pivot", VEC2, {})                                                                      \
  X(CHANGE_SCALE, changeScale, "Scale", VEC2, {})                                                                      \
  X(CHANGE_ROTATION, changeRotation, "Rotation", FLOAT, 0.0f)                                                          \
  X(CHANGE_DELAY, changeDelay, "Delay", INT, 0)                                                                        \
  X(CHANGE_TINT, changeTint, "Tint", VEC4, {})                                                                         \
  X(CHANGE_COLOR_OFFSET, changeColorOffset, "Color Offset", VEC3, {})                                                  \
  X(CHANGE_IS_VISIBLE, changeIsVisible, "Visible", BOOL, false)                                                        \
  X(CHANGE_IS_INTERPOLATED, changeIsInterpolated, "Interpolated", BOOL, false)                                         \
  X(CHANGE_NUMBER_FRAMES, changeNumberFrames, "Frame Count", INT, 1)                                                   \
  X(CHANGE_IS_FROM_SELECTED_FRAME, changeIsFromSelectedFrame, "From Selected Frame", BOOL, false)                      \
                                                                                                                       \
  X(SCALE_VALUE, scaleValue, "Scale", FLOAT, 1.0f)                                                                     \
                                                                                                                       \
  X(PREVIEW_IS_AXES, previewIsAxes, "Axes", BOOL, true)                                                                \
  X(PREVIEW_IS_GRID, previewIsGrid, "Grid", BOOL, true)                                                                \
  X(PREVIEW_IS_ROOT_TRANSFORM, previewIsRootTransform, "Root Transform", BOOL, true)                                   \
  X(PREVIEW_IS_PIVOTS, previewIsPivots, "Pivots", BOOL, false)                                                         \
  X(PREVIEW_IS_BORDER, previewIsBorder, "Border", BOOL, false)                                                         \
  X(PREVIEW_IS_ALT_ICONS, previewIsAltIcons, "Alt Icons", BOOL, false)                                                 \
  X(PREVIEW_OVERLAY_TRANSPARENCY, previewOverlayTransparency, "Alpha", FLOAT, 255)                                     \
  X(PREVIEW_START_ZOOM, previewStartZoom, "Start Zoom", FLOAT, 200.0f)                                                 \
  X(PREVIEW_GRID_SIZE, previewGridSize, "Size", IVEC2, {32, 32})                                                       \
  X(PREVIEW_GRID_OFFSET, previewGridOffset, "Offset", IVEC2, {})                                                       \
  X(PREVIEW_GRID_COLOR, previewGridColor, "Color", VEC4, {1.0f, 1.0f, 1.0f, 0.125f})                                   \
  X(PREVIEW_AXES_COLOR, previewAxesColor, "Color", VEC4, {1.0f, 1.0f, 1.0f, 0.125f})                                   \
  X(PREVIEW_BACKGROUND_COLOR, previewBackgroundColor, "Background Color", VEC3, {0.113f, 0.184f, 0.286f})              \
                                                                                                                       \
  X(PROPERTIES_IS_ROUND, propertiesIsRound, "Round", BOOL, false)                                                      \
                                                                                                                       \
  X(GENERATE_START_POSITION, generateStartPosition, "Start Position", IVEC2, {})                                       \
  X(GENERATE_SIZE, generateSize, "Size", IVEC2, {64, 64})                                                              \
  X(GENERATE_PIVOT, generatePivot, "Pivot", IVEC2, {32, 32})                                                           \
  X(GENERATE_ROWS, generateRows, "Rows", INT, 4)                                                                       \
  X(GENERATE_COLUMNS, generateColumns, "Columns", INT, 4)                                                              \
  X(GENERATE_COUNT, generateCount, "Count", INT, 16)                                                                   \
  X(GENERATE_DELAY, generateDelay, "Delay", INT, 1)                                                                    \
  X(GENERATE_ZOOM, generateZoom, "Zoom", FLOAT, 100.0f)                                                                \
                                                                                                                       \
  X(EDITOR_IS_GRID, editorIsGrid, "Grid", BOOL, true)                                                                  \
  X(EDITOR_IS_GRID_SNAP, editorIsGridSnap, "Snap", BOOL, true)                                                         \
  X(EDITOR_IS_BORDER, editorIsBorder, "Border", BOOL, true)                                                            \
  X(EDITOR_IS_TRANSPARENT, editorIsTransparent, "Transparent", BOOL, true)                                             \
  X(EDITOR_START_ZOOM, editorStartZoom, "Zoom", FLOAT, 200.0f)                                                         \
  X(EDITOR_SIZE, editorSize, "Size", IVEC2_WH, {1200, 600})                                                            \
  X(EDITOR_GRID_SIZE, editorGridSize, "Grid Size", IVEC2, {32, 32})                                                    \
  X(EDITOR_GRID_OFFSET, editorGridOffset, "Offset", IVEC2, {32, 32})                                                   \
  X(EDITOR_GRID_COLOR, editorGridColor, "Color", VEC4, {1.0, 1.0, 1.0, 0.125})                                         \
  X(EDITOR_BACKGROUND_COLOR, editorBackgroundColor, "Background Color", VEC3, {0.113, 0.184, 0.286})                   \
                                                                                                                       \
  X(MERGE_TYPE, mergeType, "Type", INT, 0)                                                                             \
  X(MERGE_IS_DELETE_ANIMATIONS_AFTER, mergeIsDeleteAnimationsAfter, "Delete Animations After", BOOL, false)            \
                                                                                                                       \
  X(BAKE_INTERVAL, bakeInterval, "Interval", INT, 1)                                                                   \
  X(BAKE_IS_ROUND_SCALE, bakeIsRoundScale, "Round Scale", BOOL, true)                                                  \
  X(BAKE_IS_ROUND_ROTATION, bakeIsRoundRotation, "Round Rotation", BOOL, true)                                         \
                                                                                                                       \
  X(TIMELINE_ADD_ITEM_TYPE, timelineAddItemType, "Add Item Type", INT, anm2::LAYER)                                    \
  X(TIMELINE_ADD_ITEM_LOCALITY, timelineAddItemLocale, "Add Item Locale", INT, types::locale::GLOBAL)                  \
  X(TIMELINE_ADD_ITEM_SOURCE, timelineAddItemSource, "Add Item Source", INT, types::source::NEW)                       \
  X(TIMELINE_IS_SHOW_UNUSED, timelineIsShowUnused, "##Show Unused", BOOL, true)                                        \
  X(TIMELINE_IS_ONLY_SHOW_LAYERS, timelineIsOnlyShowLayers, "##Only Show Layers", BOOL, false)                         \
  X(TIMELINE_IS_SOUND, timelineIsSound, "Sound", BOOL, true)                                                           \
                                                                                                                       \
  X(ONIONSKIN_IS_ENABLED, onionskinIsEnabled, "Enabled", BOOL, false)                                                  \
  X(ONIONSKIN_BEFORE_COUNT, onionskinBeforeCount, "Frames", INT, 0)                                                    \
  X(ONIONSKIN_AFTER_COUNT, onionskinAfterCount, "Frames", INT, 0)                                                      \
  X(ONIONSKIN_BEFORE_COLOR, onionskinBeforeColor, "Color", VEC3, types::color::RED)                                    \
  X(ONIONSKIN_AFTER_COLOR, onionskinAfterColor, "Color", VEC3, types::color::BLUE)                                     \
  X(ONIONSKIN_MODE, onionskinMode, "Mode", INT, (int)types::OnionskinMode::TIME)                                       \
                                                                                                                       \
  X(TOOL, tool, "##Tool", INT, 0)                                                                                      \
  X(TOOL_COLOR, toolColor, "##Color", VEC4, {1.0, 1.0, 1.0, 1.0})                                                      \
                                                                                                                       \
  X(RENDER_TYPE, renderType, "Output", INT, render::GIF)                                                               \
  X(RENDER_PATH, renderPath, "Path", STRING, "./output.gif")                                                           \
  X(RENDER_ROWS, renderRows, "Rows", INT, 0)                                                                           \
  X(RENDER_COLUMNS, renderColumns, "Columns", INT, 0)                                                                  \
  X(RENDER_FORMAT, renderFormat, "Format", STRING, "{}.png")                                                           \
  X(RENDER_IS_RAW_ANIMATION, renderIsRawAnimation, "Raw Animation", BOOL, true)                                        \
  X(RENDER_SCALE, renderScale, "Scale", FLOAT, 1.0f)                                                                   \
  X(RENDER_FFMPEG_PATH, renderFFmpegPath, "FFmpeg Path", STRING, FFMPEG_PATH_DEFAULT)

#define SETTINGS_SHORTCUTS                                                                                             \
  /* Symbol / Name / String / Type / Default */                                                                        \
  /* File */                                                                                                           \
  X(SHORTCUT_NEW, shortcutNew, "New", STRING, "Ctrl+N")                                                                \
  X(SHORTCUT_OPEN, shortcutOpen, "Open", STRING, "Ctrl+O")                                                             \
  X(SHORTCUT_CLOSE, shortcutClose, "Close", STRING, "Ctrl+W")                                                          \
  X(SHORTCUT_SAVE, shortcutSave, "Save", STRING, "Ctrl+S")                                                             \
  X(SHORTCUT_SAVE_AS, shortcutSaveAs, "Save As", STRING, "Ctrl+Shift+S")                                               \
  X(SHORTCUT_EXIT, shortcutExit, "Exit", STRING, "Alt+F4")                                                             \
  /* Edit */                                                                                                           \
  X(SHORTCUT_UNDO, shortcutUndo, "Undo", STRING, "Ctrl+Z")                                                             \
  X(SHORTCUT_REDO, shortcutRedo, "Redo", STRING, "Ctrl+Shift+Z")                                                       \
  X(SHORTCUT_CUT, shortcutCut, "Cut", STRING, "Ctrl+X")                                                                \
  X(SHORTCUT_COPY, shortcutCopy, "Copy", STRING, "Ctrl+C")                                                             \
  X(SHORTCUT_PASTE, shortcutPaste, "Paste", STRING, "Ctrl+V")                                                          \
  X(SHORTCUT_DUPLICATE, shortcutDuplicate, "Duplicate", STRING, "Ctrl+J")                                              \
  X(SHORTCUT_ADD, shortcutAdd, "Add", STRING, "Insert")                                                                \
  X(SHORTCUT_REMOVE, shortcutRemove, "Remove", STRING, "Delete")                                                       \
  X(SHORTCUT_DEFAULT, shortcutDefault, "Default", STRING, "Home")                                                      \
  X(SHORTCUT_MERGE, shortcutMerge, "Merge", STRING, "Ctrl+E")                                                          \
  /* Tools */                                                                                                          \
  X(SHORTCUT_PAN, shortcutPan, "Pan", STRING, "P")                                                                     \
  X(SHORTCUT_MOVE, shortcutMove, "Move", STRING, "V")                                                                  \
  X(SHORTCUT_ROTATE, shortcutRotate, "Rotate", STRING, "R")                                                            \
  X(SHORTCUT_SCALE, shortcutScale, "Scale", STRING, "S")                                                               \
  X(SHORTCUT_CROP, shortcutCrop, "Crop", STRING, "C")                                                                  \
  X(SHORTCUT_DRAW, shortcutDraw, "Draw", STRING, "B")                                                                  \
  X(SHORTCUT_ERASE, shortcutErase, "Erase", STRING, "E")                                                               \
  X(SHORTCUT_COLOR_PICKER, shortcutColorPicker, "Color Picker", STRING, "I")                                           \
  X(SHORTCUT_COLOR, shortcutColor, "Color", STRING, "X")                                                               \
  /* View */                                                                                                           \
  X(SHORTCUT_CENTER_VIEW, shortcutCenterView, "Center View", STRING, "Home")                                           \
  X(SHORTCUT_FIT, shortcutFit, "Fit", STRING, "F")                                                                     \
  X(SHORTCUT_ZOOM_IN, shortcutZoomIn, "Zoom In", STRING, "Ctrl+Equal")                                                 \
  X(SHORTCUT_ZOOM_OUT, shortcutZoomOut, "Zoom Out", STRING, "Ctrl+Minus")                                              \
  /* Timeline / Playback */                                                                                            \
  X(SHORTCUT_PLAY_PAUSE, shortcutPlayPause, "Play/Pause", STRING, "Space")                                             \
  X(SHORTCUT_MOVE_PLAYHEAD_BACK, shortcutMovePlayheadBack, "Playhead Back", STRING, "Comma")                           \
  X(SHORTCUT_MOVE_PLAYHEAD_FORWARD, shortcutMovePlayheadForward, "Playhead Forward", STRING, "Period")                 \
  X(SHORTCUT_SHORTEN_FRAME, shortcutShortenFrame, "Shorten Frame", STRING, "F4")                                       \
  X(SHORTCUT_EXTEND_FRAME, shortcutExtendFrame, "Extend Frame", STRING, "F5")                                          \
  X(SHORTCUT_INSERT_FRAME, shortcutInsertFrame, "Insert Frame", STRING, "F6")                                          \
  X(SHORTCUT_PREVIOUS_FRAME, shortcutPreviousFrame, "Previous Frame", STRING, "F7")                                    \
  X(SHORTCUT_NEXT_FRAME, shortcutNextFrame, "Next Frame", STRING, "F8")                                                \
  /* Toggles */                                                                                                        \
  X(SHORTCUT_ONIONSKIN, shortcutOnionskin, "Onionskin", STRING, "O")

#define SETTINGS_WINDOWS                                                                                               \
  /* Symbol / Name / String / Type / Default */                                                                        \
  X(WINDOW_ANIMATIONS, windowIsAnimations, "Animations", BOOL, true)                                                   \
  X(WINDOW_ANIMATION_PREVIEW, windowIsAnimationPreview, "Animation Preview", BOOL, true)                               \
  X(WINDOW_EVENTS, windowIsEvents, "Events", BOOL, true)                                                               \
  X(WINDOW_FRAME_PROPERTIES, windowIsFrameProperties, "Frame Properties", BOOL, true)                                  \
  X(WINDOW_LAYERS, windowIsLayers, "Layers", BOOL, true)                                                               \
  X(WINDOW_NULLS, windowIsNulls, "Nulls", BOOL, true)                                                                  \
  X(WINDOW_ONIONSKIN, windowIsOnionskin, "Onionskin", BOOL, true)                                                      \
  X(WINDOW_PREVIEW, windowIsSpritesheets, "Spritesheets", BOOL, true)                                                  \
  X(WINDOW_SOUNDS, windowIsSounds, "Sounds", BOOL, true)                                                               \
  X(WINDOW_SPRITESHEET_EDITOR, windowIsSpritesheetEditor, "Spritesheet Editor", BOOL, true)                            \
  X(WINDOW_TIMELINE, windowIsTimeline, "Timeline", BOOL, true)                                                         \
  X(WINDOW_TOOLS, windowIsTools, "Tools", BOOL, true)

  enum ShortcutType
  {
#define X(symbol, name, string, type, ...) symbol,
    SETTINGS_SHORTCUTS
#undef X
        SHORTCUT_COUNT
  };

  class Settings
  {
  public:
#define X(symbol, name, string, type, ...) TYPE_##type name = __VA_ARGS__;
    SETTINGS_MEMBERS SETTINGS_SHORTCUTS SETTINGS_WINDOWS
#undef X

        bool isDefault{};

    Settings() = default;

    Settings(const std::string&);
    void save(const std::string&, const std::string&);
  };

  constexpr const char* SHORTCUT_STRINGS[] = {
#define X(symbol, name, string, type, ...) string,
      SETTINGS_SHORTCUTS
#undef X
  };

  using ShortcutMember = std::string Settings::*;
  constexpr ShortcutMember SHORTCUT_MEMBERS[] = {
#define X(symbol, name, string, type, ...) &Settings::name,
      SETTINGS_SHORTCUTS
#undef X
  };

  enum WindowType
  {
#define X(symbol, name, string, type, ...) symbol,
    SETTINGS_WINDOWS
#undef X
        WINDOW_COUNT
  };

  constexpr const char* WINDOW_STRINGS[] = {
#define X(symbol, name, string, type, ...) string,
      SETTINGS_WINDOWS
#undef X
  };

  using WindowMember = bool Settings::*;
  static constexpr WindowMember WINDOW_MEMBERS[] = {
#define X(symbol, name, string, type, ...) &Settings::name,
      SETTINGS_WINDOWS
#undef X
  };
}
