/*
    libmaus2
    Copyright (C) 2021 German Tischler-Höhle

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <libmaus2/parallel/threadpool/cram/ThreadPoolCramEnqueueControl.hpp>
#include <libmaus2/util/ArgParser.hpp>
#include <libmaus2/aio/StreamLock.hpp>
#include <libmaus2/util/GetFileSize.hpp>
#include <libmaus2/parallel/NumCpus.hpp>
#include <regex>

int main(int argc, char * argv[])
{
	try
	{
		libmaus2::util::ArgParser arg(argc,argv);

		if ( ! arg.uniqueArgPresent("r") )
		{
			std::cerr << "[E] argument -r (reference) missing" << std::endl;
			return EXIT_FAILURE;
		}

		std::string const ref = arg["r"];

		std::string const profile = "normal";

		std::regex fareg("(\\.fa|\\.fasta)$");

		std::string const dictfn = std::regex_replace(ref,fareg,std::string()) + ".dict";

		if ( ! libmaus2::util::GetFileSize::fileExists(dictfn) )
		{
			std::cerr << "[E] sequence dictionary " << dictfn << " does not exist, please create it using CreateSequenceDictionary (GATK)" << std::endl;
			return EXIT_FAILURE;
		}

		char * ref_cache = getenv("REF_CACHE");

		if ( ! ref_cache )
		{
			std::cerr << "[E] REF_CACHE variable is not set" << std::endl;
			return EXIT_FAILURE;
		}

		libmaus2::parallel::threadpool::bam::BamHeaderSeqData seqdata(dictfn);

		std::size_t const hwthreads = libmaus2::parallel::NumCpus::getNumLogicalProcessors();

		std::size_t argthreads = arg.getParsedArgOrDefault<std::size_t>("t", hwthreads);
		if ( ! argthreads )
			argthreads = hwthreads;

		std::size_t const inputblocks = 64;
		std::size_t const inputblocksize = 256*1024;
		std::size_t const cramblockaccum = 64;
		std::size_t const numdecompressedblocks = argthreads*cramblockaccum;
		std::size_t const seqs_per_slice = 0;
		std::size_t const bases_per_slice = 0;
		std::string const cramversion; // = "4.0";

		libmaus2::parallel::threadpool::ThreadPool TP(argthreads);

		std::shared_ptr<std::istream> ptr(&std::cin, []([[maybe_unused]]auto p){});

		libmaus2::parallel::threadpool::cram::ThreadPoolCramEnqueueControl TPBDC(
			TP,
			std::vector< std::shared_ptr<std::istream> >(1,ptr),
			seqdata,inputblocks,inputblocksize,numdecompressedblocks,cramblockaccum,profile,
			seqs_per_slice,
			bases_per_slice,
			cramversion
		);

		TPBDC.start();

		TP.join();

		std::cerr << "[V] finished and joined" << std::endl;
	}
	catch(std::exception const & ex)
	{
		std::cerr << ex.what() << std::endl;
		return EXIT_FAILURE;
	}
}
