/**************************************************************************************
# 076 License

Copyright (c) 2026 テクニカル諏訪子

Permission is hereby granted to any person obtaining a copy of the software
Hexagon (the "Software") to use, modify, merge, copy, publish, distribute,
sublicense, and/or sell copies of the Software, subject to the following conditions:

    1. **Origin Attribution**:
       - You must not misrepresent the origin of the Software; you must not claim
         you created the original Software.
       - If the Software is used in a product, you must either:
         a. Provide clear attribution in the product's documentation, user interface,
            or other visible areas, **OR**
         b. Pay the original developers a fee they specify in writing.
    2. **Usage Restriction**:
       - The Software, or any derivative works, dependencies, or libraries
         incorporating it, must not be used for censorship or to suppress freedom of
         speech, expression, or creativity. Prohibited uses include, but are not
         limited to:
         - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
           ideas, or objective reality.
         - Tools or systems designed to restrict access to information or
           artistic works.
    3. **Notice Preservation**:
       - This license and the above copyright notice must remain intact in all copies
         of the source code.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************/
#pragma once

#ifdef _WIN32
#include <pdcurses.h>
#else
#include <ncurses.h>
#endif

#include <string>
#include <vector>

class HexEditor {
  private:
    enum StatusMode : uint8_t {
      Status_Normal,
      Status_Command,
      Status_Search,
      Status_Replace,
      Status_Error,
    };

    enum SearchDirection : uint8_t {
      Direction_Forward,
      Direction_Reverse,
    };

    struct Edit {
      size_t offset;
      uint8_t oldByte;
      uint8_t newByte;
    };
    
    struct FileSignature {
      std::vector<uint8_t> signature;
      std::string type;
    };

    size_t curPos;
    size_t dpOffset;
    size_t bpr;
    StatusMode statusMode;
    bool modified;
    bool running;
    std::string lastSearch;
    bool isMouse = false;
    bool hasFile = false;

    WINDOW *hexPanel;
    WINDOW *asciiPanel;
    std::vector<uint8_t> buf;
    std::string fname;
    int rows, cols;
    std::string statusText;
    std::vector<uint8_t> lastHexSearch;
    SearchDirection lastSearchDir;
    std::vector<size_t> matchOff;
    std::vector<Edit> undoStack;
    std::vector<Edit> redoStack;
    
    size_t headerLen;
    std::string headerType;
    static const std::vector<FileSignature> signatures;

    void render();
    void input();
    void highlightcol(size_t i, size_t row, uint8_t byte);
    void topbar();
    void statusbar();
    void handleCommand();
    void handleOpen(const std::string &path);
    void handleSave();
    void handleQuit(bool force);
    void handleSearch();
    void handleReverseSearch();
    void findNextMatch();
    void findPrevMatch();
    void handleReplace();
    void handleMouse();
    void undo();
    void redo();

  public:
    HexEditor(const std::string &filename);
    ~HexEditor();

    void run();
};
