/*
   HARK Library
   Copyright (C) 2021 Honda Research Institute Japan Inc.
 */

#include "hark-lib/node/SaveVectorFrames.h"

namespace hark
{
namespace node
{

    FileStreamIO::FileStreamIO()
    {
    }

    FileStreamIO::~FileStreamIO()
    {
        if(this->fin.is_open()){ this->fin.close(); }
        if(this->fout.is_open()){ this->fout.close(); }
    }

    void
    FileStreamIO::reader_close()
    {
        if(this->fin.is_open()){ this->fin.close(); this->rfilename = ""; }
    }

    void
    FileStreamIO::writer_close()
    {
        if(this->fout.is_open()){ this->fout.close(); this->wfilename = ""; }
    }

    void
    FileStreamIO::reader_open(std::string &filename)
    {
    }

    void
    FileStreamIO::writer_open(const std::string &filename, bool force_reopen = false)
    {
        if(!force_reopen && this->wfilename == filename && this->fout.is_open()){ return; }
        if(force_reopen || this->fout.is_open()){ this->writer_close(); }
        this->fout.open(filename, std::ios::binary);
        if(this->fout.fail()){
            std::stringstream ss;
            ss << "FileStreamIO: Can't open file '" << filename << "' : "
               << __FILE__ << ", " << __LINE__;
            throw RuntimeError(ss.str());
        }
        this->wfilename = filename;
    }

    void
    FileStreamIO::writer_open(const std::string &filename, const std::string &type,
                              std::vector<size_t> &shape = std::vector<size_t>(),
                              const std::string &container = "")
    {
        std::ostringstream oss;
        oss << fmt::format(
                   filename,
                   fmt::arg("datatype", type), fmt::arg("containertype", container),
                   fmt::arg("colsize", (shape.size() > 0 ? shape[shape.size()-1] : 0)),
                   fmt::arg("rowsize", (shape.size() > 1 ? shape[shape.size()-2] : 0)),
                   fmt::arg("dim", shape.size()));
        this->writer_open(oss.str());
    }

    void
    FileStreamIO::set_modes(const std::string &output_mode, const std::string &text_mode)
    {
        if(output_mode == "TEXT"){
            this->mode = OutputMode::TEXT;
        }
        else if(output_mode == "RAW"){
            this->mode = OutputMode::RAW;
        }
        else{
            std::stringstream ss;
            ss << "FileStreamIO: Unexpected OutputMode. You must be chose from 'TEXT' or 'RAW'.\n"
               << __FILE__ << ", " << __LINE__;
            throw RuntimeError(ss.str());
        }
        if(text_mode == "FIXED"){
            this->tmode = TextMode::FIXED;
        }
        else if(text_mode == "SCIENTIFIC"){
            this->tmode = TextMode::SCIENTIFIC;
        }
        else{
            std::stringstream ss;
            ss << "FileStreamIO: Unexpected TextMode. You must be chose from 'FIXED' or 'SCIENTIFIC'\n"
               << __FILE__ << ", " << __LINE__;
            throw RuntimeError(ss.str());
        }
    }

    void
    FileStreamIO::_binary_write(const char *ptr, size_t &size)
    {
        if(this->fout.is_open()){ this->fout.write(ptr, size); }
    }



}
}

/* end of file */
