#pragma once

#include "PvApp.h"
#include "Fields.h"

extern const std::string POP_OPER_NEW;
extern const std::string POP_OPER_EDIT;
extern const std::string POP_OPER_DEL;

class PvPopWidget : public PvObject
{
public:
    struct ParamLine
    {
        std::string key;
        std::string type;
        std::string val;
        int widget;
        std::vector<std::string> items;
        ParamLine(std::string type, std::string key) : type(type), key(key) {}
    };

    PvPopWidget(PARAM* p, int width, int height, std::string name);

    std::shared_ptr<ParamLine> addParamLine(std::string type, std::string key, std::string title, int x, int y, bool editable = true);

    void addParamLineEdit(std::string key, std::string title, int x, int y, bool editable=true);

    void addParamTextEdit(std::string key, std::string title, int x, int y, bool editable = true);

    void addParamCombox(std::string key, std::string title, int x, int y, std::vector<std::string> items);

    void setParamText(std::shared_ptr<ParamLine> line, std::string text);
    void setParamText(std::string key, std::string text);
    
    void setCallbackConfirm(std::function<void()> callback) { callbackConfirm = callback; };

    void setCallbackTextEvent(std::function<void(std::string, std::string)> callback) { callbackTextEvent = callback; };

    void setStatus(std::string text);

    void setMsg(std::string msg);

    void setData(const Fields& fields);
    Fields getData();
    Fields getChangedData();
    void checkChangedData(Fields& fields);

    void setLineGeometry(int wKey, int wVal, int h);

    void setPrimaryKeys(std::vector<std::string> keys);

    int widget() { return ui.widget; }

    void resize(int width, int height);

    std::string name;
    std::string status;

    int width {800};
    int height {600};

    int lineKeyWidth {70};
    int lineValWidth {180};
    int lineHeight {30};

    struct {
        int widget;
        int bkg;
        int bkgL;
        int bkgR;
        int btnOK;
        int btnCancel;
        int title;
        int msg;
    } ui;

    std::map<std::string, std::shared_ptr<ParamLine>> mapLines;

    std::function<void()> callbackConfirm = nullptr;
    std::function<void(std::string, std::string)> callbackTextEvent = nullptr;

    Fields dataOrigin;
    Fields primaryKeys;
};