/*
 * Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
 *
 * This file is part of PortaPack.
 *
 * 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 2, 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#include "ch.h"

#include "tuning.hpp"

#include "utility.hpp"

namespace tuning {
namespace config {

namespace {

constexpr rf::Frequency low_band_second_lo_frequency(const rf::Frequency target_frequency) {
	return 2650000000 - (target_frequency / 7);
}

constexpr rf::Frequency high_band_second_lo_regions_2_and_3(const rf::Frequency target_frequency) {
	return (target_frequency < 5100000000)
		? (2350000000 + ((target_frequency - 3600000000) / 5))
		: (2500000000 + ((target_frequency - 5100000000) / 9))
		;
}

constexpr rf::Frequency high_band_second_lo_frequency(const rf::Frequency target_frequency) {
	return (target_frequency < 3600000000)
		? (2150000000 + (((target_frequency - 2750000000) * 60) / 85))
		: high_band_second_lo_regions_2_and_3(target_frequency)
		;
}

Config low_band(const rf::Frequency target_frequency) {
	const rf::Frequency first_lo_frequency = target_frequency + low_band_second_lo_frequency(target_frequency);
	const rf::Frequency second_lo_frequency = first_lo_frequency - target_frequency;
	return {
		.first_lo_frequency = first_lo_frequency,
		.second_lo_frequency = second_lo_frequency,
		.rf_path_band = rf::path::Band::Low,
		.baseband_q_invert = true,
	};
}

Config mid_band(const rf::Frequency target_frequency) {
	return {
		.first_lo_frequency = 0,
		.second_lo_frequency = target_frequency,
		.rf_path_band = rf::path::Band::Mid,
		.baseband_q_invert = false,
	};
}

Config high_band(const rf::Frequency target_frequency) {
	const rf::Frequency first_lo_frequency = target_frequency - high_band_second_lo_frequency(target_frequency);
	const rf::Frequency second_lo_frequency = target_frequency - first_lo_frequency;
	return {
		.first_lo_frequency = first_lo_frequency,
		.second_lo_frequency = second_lo_frequency,
		.rf_path_band = rf::path::Band::High,
		.baseband_q_invert = false,
	};
}

} /* namespace */

Config create(const rf::Frequency target_frequency) {
	/* TODO: This is some lame code. */
	if( target_frequency < rf::path::band_low.min ) {
		return { };
	} else if( target_frequency < rf::path::band_low.max ) {
		return low_band(target_frequency);
	} else if( target_frequency < rf::path::band_mid.max ) {
		return mid_band(target_frequency);
	} else if( target_frequency < rf::path::band_high.max ) {
		return high_band(target_frequency);
	} else {
		return { };
	}
}

} /* namespace config */
} /* namespace tuning */
