#include "transmission.h"

#include <iostream>
#include <cmath>

static std::vector<bool> message;
const int MAX_M = 100000;

std::vector<bool> transmit_1(const std::vector<bool>& data, int d) {
#ifdef STJO
	std::cout << "to send:" << std::endl;
	for(auto x : data) std::cout << x;
	std::cout << std::endl;
#endif

	const int N = data.size();
	//const int coef = (d * N > MAX_M ? MAX_M / N : d);
	const int coef = 3; //(d * N > MAX_M ? MAX_M / N : d);

	message.resize(N * coef);

	for(int i = 0;i < message.size();i ++) {
		message[i] = data[i / coef] ;
	}

#ifdef STJO
	std::cout << "sending:" << std::endl;
	for(auto x : message) std::cout << x;
	std::cout << std::endl;
#endif

    return message;
}

std::vector<bool> transmit_2(const std::vector<bool>& data, int d)
{
#ifdef STJO
	//std::cout << "to send:" << std::endl;
	//for(auto x : data) std::cout << x;
	//std::cout << std::endl;
#endif

	const int N = data.size();

	int blockSize = 15;
	int padSize   = d / 10;

	if(d == 1) {
#ifdef STJO
		std::cout << "d is one" << std::endl;
#endif
		blockSize = 40;
		padSize   = 15;
	} else if(d <= 1.1 * std::sqrt(N)) {
#ifdef STJO
		std::cout << "sqrt N" << std::endl;
#endif
		blockSize = 30;
		padSize   = 15;
	}

	int idx = 0;

	message.resize(MAX_M);
	int padCnt = 0;
	for(int i = 0;i < N;i ++) {
		message[idx ++] = data[i];
		// todo break if no more
		if((i+1) % blockSize == 0) {
			padCnt ++;
#ifdef STJO
			//for(int _ = i - blockSize + 1;_ <= i;_ ++) std::cout << data[_];
			//std::cout << std::endl;
			//std::cout << "adding pad " << (padCnt++) << ' ' << idx << std::endl;
#endif
			for(int p = 0;p < padSize;p ++) message[idx ++] = !data[i];
			//std::cout << idx << std::endl;
			//std::cout << "----\n";
		}
	}

	std::cout << "padCnt: " << padCnt << std::endl;
	message.resize(idx);

#ifdef STJO
	//std::cout << "sending:" << std::endl;
	//for(auto x : message) std::cout << x;
	//std::cout << std::endl;
#endif

    return message;
}

std::vector<bool> transmit(const std::vector<bool>& data, int d) {
	return transmit_2(data, d);
}
