#ifndef _Climate1546_h_
#define _Climate1546_h_


#include <iostream>
#include <vector>
#include <cmath>
#include <map>


const vector<double> Heff_values = {10, 20, 37.5, 75, 150, 300, 600, 1200};
const vector<double> Distances = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 550, 575, 600, 625, 650, 675, 700, 725, 750, 775, 800, 825, 850, 875, 900, 925, 950, 975, 1000};


/**
 * Calculates field strength value for land path propagation based on ITU-R P.1546 recommendation.
 * Provides basic field strength prediction without geographical coordinates consideration.
 * 
 * @param Figure_frequency Operating frequency in MHz (100, 600, or 2000)
 * @param Figure_Heff Effective antenna height in meters
 * @param Figure_distance Propagation distance in kilometers
 * @param Time_percentage Time percentage for reliability (1, 10, or 50)
 * @param Sea_temperature Sea temperature character (not used in basic version)
 * @return Field strength value in dB(μV/m) or error code (2000+ for invalid inputs)
 */
double Get_figure_FS_value(double Figure_frequency, double Figure_Heff, double Figure_distance, int Time_percentage, char Sea_temperature) {
    if (Figure_frequency != 100 && Figure_frequency != 600 && Figure_frequency != 2000) {
        return 2000; 
    }
    if (Time_percentage != 1 && Time_percentage != 10 && Time_percentage != 50) {
        return 2001; 
    }
    bool FoundHeff = false;
    for (size_t I = 0; I < Heff_values.size(); ++I) {
        if (Figure_Heff == Heff_values[I]) {
            FoundHeff = true;
            break;
        }
    }
    if (!FoundHeff) {
        return 2003; 
    }
    bool FoundDistance = false;
    for (size_t J = 0; J < Distances.size(); ++J) {
        if (Figure_distance == Distances[J]) {
            FoundDistance = true;
            break;
        }
    }
    if (!FoundDistance) {
        return 2004; 
    }

 
    const vector<vector<double>>* F;
    if (Figure_frequency == 100) {
        if (Time_percentage == 50) F = &F01;
        else if (Time_percentage == 10) F = &F02;
        else F = &F03;
    } else if (Figure_frequency == 600) {
        if (Time_percentage == 50) F = &F09;
        else if (Time_percentage == 10) F = &F10;
        else F = &F11;
    } else { // Figure_frequency == 2000
        if (Time_percentage == 50) F = &F17;
        else if (Time_percentage == 10) F = &F18;
        else F = &F19;
    }

    
    size_t J = find(Distances.begin(), Distances.end(), Figure_distance) - Distances.begin();
    size_t I = find(Heff_values.begin(), Heff_values.end(), Figure_Heff) - Heff_values.begin();
    double Land_figure_FS = (*F)[J][I];

    double E1 = (*F)[J][0];
    double E1_ = E1 + dE1;
    double En = Land_figure_FS;
    if (I == 0) {
        Land_figure_FS = E1_;
    } else {
        Land_figure_FS = E1_ + (En - E1) * (EmaxL_ - E1_) / (EmaxL - E1);
    }

    return Land_figure_FS;
}


/**
 * Calculates field strength value for sea path propagation with enhanced geographical correction.
 * Includes latitude/longitude-based climate correction and sea temperature adjustment.
 * 
 * @param Figure_frequency Operating frequency in MHz (100, 600, or 2000)
 * @param Figure_Heff Effective antenna height in meters
 * @param Figure_distance Propagation distance in kilometers
 * @param Time_percentage Time percentage for reliability (1, 10, or 50)
 * @param Sea_temperature Sea temperature character ('C' for cold, 'W' for warm)
 * @param LatM Latitude of the receiving point in degrees
 * @param LonM Longitude of the receiving point in degrees
 * @return Field strength value in dB(μV/m) or error code (2000+ for invalid inputs)
 */
double Get_figure_FS_value_plus(double Figure_frequency, double Figure_Heff, double Figure_distance, int Time_percentage, char Sea_temperature, double LatM, double LonM) {

    if (Figure_frequency != 100 && Figure_frequency != 600 && Figure_frequency != 2000) {
        return 2000; 
    }
    if (Time_percentage != 1 && Time_percentage != 10 && Time_percentage != 50) {
        return 2001; 
    }
    bool FoundHeff = false;
    for (size_t I = 0; I < Heff_values.size(); ++I) {
        if (Figure_Heff == Heff_values[I]) {
            FoundHeff = true;
            break;
        }
    }
    if (!FoundHeff) {
        return 2003; 
    }
    bool FoundDistance = false;
    for (size_t J = 0; J < Distances.size(); ++J) {
        if (Figure_distance == Distances[J]) {
            FoundDistance = true;
            break;
        }
    }
    if (!FoundDistance) {
        return 2004; 
    }


    const vector<vector<double>>* F;
    if (Figure_frequency == 100) {
        if (Time_percentage == 50) F = &F04;
        else if (Time_percentage == 10) F = &F05;
        else F = &F06;
    } else if (Figure_frequency == 600) {
        if (Time_percentage == 50) F = &F12;
        else if (Time_percentage == 10) F = &F13;
        else F = &F14;
    } else { // Figure_frequency == 2000
        if (Time_percentage == 50) F = &F20;
        else if (Time_percentage == 10) F = &F21;
        else F = &F22;
    }

    size_t J = find(Distances.begin(), Distances.end(), Figure_distance) - Distances.begin();
    size_t I = find(Heff_values.begin(), Heff_values.end(), Figure_Heff) - Heff_values.begin();
    double Sea_figure_FS = (*F)[J][I];

    double E1 = (*F)[J][0];
    double E1_ = E1 + dE1;
    double En = Sea_figure_FS;
    if (I == 0) {
        Sea_figure_FS = E1_;
    } else {
        Sea_figure_FS = E1_ + (En - E1) * (EmaxS_ - E1_) / (EmaxS - E1);
    }

    if (Sea_temperature == 'C') {
        Sea_figure_FS = SF_C;
    } else if (Sea_temperature == 'W') {
        Sea_figure_FS = SF_W;
    }

    return Sea_figure_FS;
}

#endif
