/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    This program is free software; 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 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 LICENSE and COPYING files for more details.
*/

#include "matrices_all.h"
#include <boost/filesystem.hpp>

void GetIndicesToRemove(std::set<size_t> &indices, Matrices::FeatureVectorPtr features);
void GetMatrixDirName(const std::string mpath_in, std::string &mdir, std::string &mname);
void WriteRemoved(const std::string &filename, std::set<size_t> &row_indices, std::set<size_t> &column_indices,
				  Matrices::SuperMatrixPtr pMatrix);

int main(int ac, char **av)
{
	Matrices::init_logging();
	std::string mpath_in = av[1];
	std::string mname_out = av[2];
	std::string removed_file = av[3];

	std::string mdir, mname;
	std::set<size_t> rows_to_remove, columns_to_remove;

	GetMatrixDirName(mpath_in, mdir, mname);


	Matrices::SuperMatrixPtr pMatrix = Matrices::IO::LoadSuperMatrix(mdir, mname, false, true);

	GetIndicesToRemove(rows_to_remove, pMatrix->GetRowFeatures());
	GetIndicesToRemove(columns_to_remove, pMatrix->GetColumnFeatures());

	std::cout << "Will remove " << rows_to_remove.size() << " rows and " << columns_to_remove.size() << " columns\n";
	WriteRemoved(removed_file, rows_to_remove, columns_to_remove, pMatrix);

	Matrices::SuperMatrixPtr pOutMatrix = Matrices::FilterSuperMatrix(pMatrix, rows_to_remove, columns_to_remove,
																	  true, true, true);

	std::cout << "Setting new name and saving..." << std::endl;
	pOutMatrix->SetNewName(mname_out);
	Matrices::IO::WriteSuperMatrix(pOutMatrix, "/home/boban/tmp");

	return 0;
}

void WriteRemoved(const std::string &filename, std::set<size_t> &row_indices, std::set<size_t> &column_indices,
				  Matrices::SuperMatrixPtr pMatrix)
{
	 Matrices::FeatureVectorPtr row_features = pMatrix->GetRowFeatures();
	 Matrices::FeatureVectorPtr column_features = pMatrix->GetColumnFeatures();

	 std::ofstream out( filename.c_str() );

	 out << "Rows:\n";
	 for(std::set<size_t>::iterator iter = row_indices.begin(); iter != row_indices.end(); ++iter)
		 out << (*row_features)[*iter].GetLabel() << std::endl;

	 out << "\nColumns:\n";
	 for(std::set<size_t>::iterator iter = column_indices.begin(); iter != column_indices.end(); ++iter)
		 out << (*column_features)[*iter].GetLabel() << std::endl;
}
void GetMatrixDirName(const std::string mpath_in, std::string &mdir, std::string &mname)
{
	std::string mpath;
	if( mpath_in[ mpath_in.size() - 1] == '\\' ||
		mpath_in[ mpath_in.size() - 1] == '/')
		mpath = mpath_in.substr(0, mpath_in.size() - 1);
	else
		mpath = mpath_in;

	boost::filesystem::path p (mpath);
	mdir = p.parent_path().string();
	mname = p.filename().c_str();
}
void GetIndicesToRemove(std::set<size_t> &indices, Matrices::FeatureVectorPtr features)
{
	for(size_t i = 0; i < features->size(); i++)
		if ( (*features)[i].GetNZ() < 5 )
			indices.insert( i );
}
