/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2022  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "mathgl_plot.h"

#ifdef GCTL_GRAPHIC_MATHGL

gctl::mathgl_plot::mathgl_plot()
{
    xnum_ = ynum_ = 0;
}

gctl::mathgl_plot::~mathgl_plot(){}

void gctl::mathgl_plot::init_axis(const array<double> &in_x, std::string x_label)
{
    x_ = in_x;
    xnum_ = x_.size();
    x_ll_ = x_label;
    return;
}

void gctl::mathgl_plot::add_profile(const array<double> &in_y, std::string y_label)
{
    if (xnum_ != in_y.size())
        throw std::runtime_error("[gctl::mathgl_plot] Invalid profile data size.");

    ys_.concat(in_y);
    y_ll_ = y_label;
    ynum_++;
    return;
}

int gctl::mathgl_plot::Draw(mglGraph *gr)
{
    mglData x_d, y_d;
    x_d.Set(x_.get(), x_.size());
    y_d.Set(ys_.get(), ys_.size());

    double mean = ys_.mean();
    double mini = ys_.front(), maxi = ys_.front();
    for (size_t i = 1; i < ys_.size(); i++)
    {
        mini = GCTL_MIN(mini, ys_[i]);
        maxi = GCTL_MAX(maxi, ys_[i]);
    }

    gr->SetSize(800, 400);
    gr->SetRanges(x_.front(), x_.back(), mini - 0.5*(mean - mini), maxi + 0.5*(maxi - mean));
    //gr->Title("Untitled");
    gr->Box();
    gr->Axis("x");
    gr->Axis("y");
    gr->Grid("xy", "h:");
    gr->Label('x', x_ll_.c_str(), 0.0);
    gr->Label('y', y_ll_.c_str(), 0.0);
    gr->Plot(x_d, y_d, "B");

    //if (file != "null")
    //{
    //    std::string tp = file.substr(file.find_last_of('.') + 1);
    //    if (tp == "eps") gr->WriteEPS(file.c_str());
    //    if (tp == "png") gr->WritePNG(file.c_str());
    //    if (tp == "jpg") gr->WriteJPEG(file.c_str());
    //}
    return 0;
}

int gctl::mathgl_plot::plot()
{
    mglFLTK gr(this, "GCTL Viewer");
    return gr.Run();
}

#endif // GCTL_GRAPHIC_MATHGL