/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  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.
 ******************************************************/

#ifndef _GCTL_GNUPLOT_H
#define _GCTL_GNUPLOT_H

#include <vector>
#include <exception>
#include <stdexcept>
#include <ctime>

#include "../io/file_io.h"

namespace gctl
{
    /**
     * @brief 这是一个gnuplot命令管道库
     */
    class gnuplot
    {
    public:
        /**
         * @brief Construct a new gnuplot object
         * 
         * @param persist Gnuplot will hold the window after plot if true.
         */
        gnuplot(bool persist = true);

        // destructor
        virtual ~gnuplot();

        /**
         * @brief Set this to send all lines to buffer.
         */
        void to_buffer();

        /**
         * @brief Send a line to gnuplot and execute it. If to_buffer is called before, then the line will be stored in the buffer.
         * 
         * @param cmd command line.
         * @param next_block If true, an empty line will be added to the buffer after the cmd.
         */
        void send(const std::string& cmd, bool next_block = false);

        /**
         * @brief Send a data block
         * 
         * @param name Name of the data block which could be used later for ploting.
         * @param data data block. Note that each vector object represents a data column.
         * @param next_block If true, an empty line will be added to the buffer after the cmd.
         */
        void send_data(std::string name, const std::vector<std::vector<double> > &data, bool next_block = false);

        /**
         * @brief Send the buffer to gnuplot and execute it at once. Then clear the buffer.
         * 
         * @param repeat How many times to repeat the buffer.
         */
        void send_buffer(size_t repeat = 1);

        /**
         * @brief Save the buffer to a file. This will not clear the buffer.
         *
         * @param filename File name.
         */
        void save_buffer(const std::string& filename);

    private:
        gnuplot(gnuplot const&) = delete;
        void operator=(gnuplot const&) = delete;

        FILE* pipe;
        std::vector<std::string> buffer;
        std::vector<std::string> data_lines;
        bool _2buffer;
    };
};

#endif // _GCTL_GNUPLOT_H