#include "PvApp.h"

///////////////////////////////////////////////////////////////////////////////////////////////////
/// === PvChartBar 
class PvChartBar : public PvObject
{
public:
    static std::shared_ptr<PvChartBar> create(PARAM* p, int parentId, int x, int y, int w, int h)
    {
        return std::make_shared<PvChartBar>(p, parentId, x, y, w, h);
    }

    PvChartBar(PARAM* p, int parentId, int x, int y, int w, int h);

    void setTickX(int n);
    void setTickY(int n);

    void insertItem(std::string text);
    void updateItem(int index, std::vector<std::pair<std::string, double>> vd);

    void setRangeY(double vmin, double vmax);
    void setLabelYLeft(std::string name);
    void setLabelYRight(std::string name);

    int pvid_;

    struct {
        int x;
        int y;
        int w;
        int h;
        int id;
    } boxBar_;

    struct AxisTick {
        int widget = PV_ID_NUL;
        std::vector<int> vecTick_;
    };

    AxisTick yAxisLeft_;
    AxisTick yAxisRight_;
    AxisTick xAxis_;

    int yLabelLeft_ = PV_ID_NUL;
    int yLabelRight_ = PV_ID_NUL;

    std::vector<int> vecTickX_;

    struct BarItem {
        std::string name;
        int idItem;
        std::vector<int> bar_;
    };

    std::vector<BarItem> vecItems_;
    int itemLabelPos = 100;


    int yLeftMin = 0;
    int yLeftMax = 0;
    int yMinRight = 0;
    int yMaxRight = 0;

    // 绘制区域的间距、柱体的间距
    int margin = 5;
};

//****************************************************************************************************
// 曲线图
//
class PvChartCurve : public PvObject
{
public:
    PvChartCurve(PARAM* p, int parent, int x, int y, int w, int h);

    void setBackground(PARAM* p, int r, int g, int b);

    void setLabelYLeft(std::string name, float min = 0, float max = 100, float step = 20);
    void setLabelYRight(std::string name, float min = 0, float max = 100, float step = 20);

    void setXBottom(float min, float max, float step);
    void setXBottomFormat(std::string fmt);

    int insertItem(int pos, std::string name, int x, int y, int w);
    void updateItem(int index, const std::vector<std::pair<double, double>>& data);

    int64_t set_range_x_days(int n);
    int64_t set_range_x_day_by_hour(int n);

    void update_days_test(int n);
    void mark(PARAM* p, int index, float x, float y, std::string text);
    void reviseYMax(int pos, double v);

private:
    PvRect rect;
    int pvid_;
    int plotId_;
    int start_num_ = 0;

    int labTimeLine_;
    std::vector<std::pair<int, int>> vecTimeAxisId_;

    struct CurveItem
    {
        std::string name;
        int pos;
    };
    std::vector<CurveItem> vecItems_;

    int yLabelLeft = PV_ID_NUL;
    int yLabelRight = PV_ID_NUL;

    int yLeftMin_ = 0;
    int yLeftMax_ = 0;
    int yLeftStep_ = 0;
    int yRightMin_ = 0;
    int yRightMax_ = 0;
    int yRightStep_ = 0;
};