#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE_LEN 1024
#define MAX_SECTION_NAME_LEN 128
#define MAX_KEY_NAME_LEN 128
#define MAX_VALUE_LEN 1024

typedef struct {
    int tracking_mode;
} bullet_tracking_settings_t;

typedef struct {
    int bullet;
    int bullet_tracking;
} bullet_t;

typedef struct {
    int range;
} range_t;

typedef struct {
    int stage_a_tracking_strength;
    int stage_a_turn_speed;
    int stage_a_distance_threshold;
} flight_path_adjustments_t;

typedef struct {
    int stage_b_algorithm;
    float stage_b_prediction;
    int stage_b_max_angle;
} flight_path_b_t;

typedef struct {
    int stage_c_suction_strength;
    int stage_c_ignore_obstacle_chance;
} final_stage_c_t;

typedef struct {
    int priority_type;
    float weight_distance;
    float weight_health;
    float weight_threat;
} target_priority_t;

typedef struct {
    int max_calculations_per_frame;
    int distance_decay;
} performance_limits_t;

typedef struct {
    float base_speed;
    float air_resistance;
    float gravity_effect;
} bullet_physics_t;

typedef struct {
    float active_time;
    int target_filter;
    char collision_layer[128];
} flight_stage_a_t;

typedef struct {
    int waypoint_count;
    int waypoint_algorithm;
    int obstacle_avoidance_chance;
} stage_b_advanced_t;

typedef struct {
    float self_destruct_delay;
    float explosion_radius;
    int miss_scatter_angle;
} final_stage_c_t;

typedef struct {
    int relock_strategy;
    float occlusion_stay_time;
    int friendly_fire_protection;
} multi_target_handling_t;

typedef struct {
    int max_lifespan;
    int far_distance_update_interval;
    int batch_process_threshold;
} performance_tuning_t;

typedef struct {
    int effect_type;
    char tracking_color[16];
    char locked_color[16];
    float impact_effect_delay;
} visual_effects_t;

typedef struct {
    int sync_method;
    float prediction_tolerance;
    int anti_cheat_check_rate;
} network_logic_t;

typedef struct {
    bullet_tracking_settings_t bullet_tracking_settings;
    bullet_t bullet;
    bullet_t bulletsettings;
    range_t range;
    range_t rangesettings;
    flight_path_adjustments_t flight_path_adjustments;
    flight_path_b_t flight_path_b;
    final_stage_c_t final_stage_c;
    target_priority_t target_priority;
    performance_limits_t performance_limits;
    bullet_physics_t bullet_physics;
    flight_stage_a_t flight_stage_a;
    stage_b_advanced_t stage_b_advanced;
    final_stage_c_t final_stage_c_2;
    multi_target_handling_t multi_target_handling;
    performance_tuning_t performance_tuning;
    visual_effects_t visual_effects;
    network_logic_t network_logic;
} config_t;

int read_config(const char* file_path, config_t* config) {
    FILE* file = fopen(file_path, "r");
    if (!file) {
        perror("Error opening config file");
        return -1;
    }

    char line[MAX_LINE_LEN];
    char current_section[MAX_SECTION_NAME_LEN] = "";

    while (fgets(line, sizeof(line), file)) {
        // Remove newline character
        line[strcspn(line, "\n")] = 0;

        // Skip comments and empty lines
        if (line[0] == ';' || line[0] == '#' || strlen(line) == 0) {
            continue;
        }

        // Check if it's a section
        if (line[0] == '[') {
            sscanf(line, "[^]]", current_section);
            continue;
        }

        // Parse key-value pair
        char key[MAX_KEY_NAME_LEN];
        char value[MAX_VALUE_LEN];
        sscanf(line, "%[^=]=%[^\n]", key, value);

        if (strcmp(current_section, "BulletTrackingSettings") == 0) {
            if (strcmp(key, "TrackingMode") == 0) {
                config->bullet_tracking_settings.tracking_mode = atoi(value);
            }
        } else if (strcmp(current_section, "Bullet") == 0) {
            if (strcmp(key, "Bullet") == 0) {
                config->bullet.bullet = atoi(value);
            } else if (strcmp(key, "BulletTracking") == 0) {
                config->bullet.bullet_tracking = atoi(value);
            }
        } else if (strcmp(current_section, "Bulletsettings") == 0) {
            if (strcmp(key, "Bullet") == 0) {
                config->bulletsettings.bullet = atoi(value);
            } else if (strcmp(key, "BulletTracking") == 0) {
                config->bulletsettings.bullet_tracking = atoi(value);
            }
        } else if (strcmp(current_section, "Range") == 0) {
            if (strcmp(key, "Range") == 0) {
                config->range.range = atoi(value);
            }
        } else if (strcmp(current_section, "Rangesettings") == 0) {
            if (strcmp(key, "Range") == 0) {
                config->rangesettings.range = atoi(value);
            }
        } else if (strcmp(current_section, "FlightPathAdjustments") == 0) {
            if (strcmp(key, "StageA_TrackingStrength") == 0) {
                config->flight_path_adjustments.stage_a_tracking_strength = atoi(value);
            } else if (strcmp(key, "StageA_TurnSpeed") == 0) {
                config->flight_path_adjustments.stage_a_turn_speed = atoi(value);
            } else if (strcmp(key, "StageA_DistanceThreshold") == 0) {
                config->flight_path_adjustments.stage_a_distance_threshold = atoi(value);
            }
        } else if (strcmp(current_section, "FlightPathB") == 0) {
            if (strcmp(key, "StageB_Algorithm") == 0) {
                config->flight_path_b.stage_b_algorithm = atoi(value);
            } else if (strcmp(key, "StageB_Prediction") == 0) {
                config->flight_path_b.stage_b_prediction = atof(value);
            } else if (strcmp(key, "StageB_MaxAngle") == 0) {
                config->flight_path_b.stage_b_max_angle = atoi(value);
            }
        } else if (strcmp(current_section, "FinalStageC") == 0) {
            if (strcmp(key, "StageC_SuctionStrength") == 0) {
                config->final_stage_c.stage_c_suction_strength = atoi(value);
            } else if (strcmp(key, "StageC_IgnoreObstacleChance") == 0) {
                config->final_stage_c.stage_c_ignore_obstacle_chance = atoi(value);
            }
        } else if (strcmp(current_section, "TargetPriority") == 0) {
            if (strcmp(key, "PriorityType") == 0) {
                config->target_priority.priority_type = atoi(value);
            } else if (strcmp(key, "Weight_Distance") == 0) {
                config->target_priority.weight_distance = atof(value);
            } else if (strcmp(key, "Weight_Health") == 0) {
                config->target_priority.weight_health = atof(value);
            } else if (strcmp(key, "Weight_Threat") == 0) {
                config->target_priority.weight_threat = atof(value);
            }
        } else if (strcmp(current_section, "PerformanceLimits") == 0) {
            if (strcmp(key, "MaxCalculationsPerFrame") == 0) {
                config->performance_limits.max_calculations_per_frame = atoi(value);
            } else if (strcmp(key, "DistanceDecay") == 0) {
                config->performance_limits.distance_decay = atoi(value);
            }
        } else if (strcmp(current_section, "BulletPhysics") == 0) {
            if (strcmp(key, "BaseSpeed") == 0) {
                config->bullet_physics.base_speed = atof(value);
            } else if (strcmp(key, "AirResistance") == 0) {
                config->bullet_physics.air_resistance = atof(value);
            } else if (strcmp(key, "GravityEffect") == 0) {
                config->bullet_physics.gravity_effect = atof(value);
            }
        } else if (strcmp(current_section, "FlightStageA") == 0) {
            if (strcmp(key, "ActiveTime") == 0) {
                config->flight_stage_a.active_time = atof(value);
            } else if (strcmp(key, "TargetFilter") == 0) {
                config->flight_stage_a.target_filter = atoi(value);
            } else if (strcmp(key, "CollisionLayer") == 0) {
                strncpy(config->flight_stage_a.collision_layer, value, sizeof(config->flight_stage_a.collision_layer) - 1);
            }
        } else if (strcmp(current_section, "StageBAdvanced") == 0) {
            if (strcmp(key, "WaypointCount") == 0) {
                config->stage_b_advanced.waypoint_count = atoi(value);
            } else if (strcmp(key, "WaypointAlgorithm") == 0) {
                config->stage_b_advanced.waypoint_algorithm = atoi(value);
            } else if (strcmp(key, "ObstacleAvoidanceChance") == 0) {
                config->stage_b_advanced.obstacle_avoidance_chance = atoi(value);
            }
        } else if (strcmp(current_section, "FinalStageC") == 0) {
            if (strcmp(key, "SelfDestructDelay") == 0) {
                config->final_stage_c_2.self_destruct_delay = atof(value);
            } else if (strcmp(key, "ExplosionRadius") == 0) {
                config->final_stage_c_2.explosion_radius = atof(value);
            } else if (strcmp(key, "MissScatterAngle") == 0) {
                config->final_stage_c_2.miss_scatter_angle = atoi(value);
            }
        } else if (strcmp(current_section, "MultiTargetHandling") == 0) {
            if (strcmp(key, "RelockStrategy") == 0) {
                config->multi_target_handling.relock_strategy = atoi(value);
            } else if (strcmp(key, "OcclusionStayTime") == 0) {
                config->multi_target_handling.occlusion_stay_time = atof(value);
            } else if (strcmp(key, "FriendlyFireProtection") == 0) {
                config->multi_target_handling.friendly_fire_protection = atoi(value);
            }
        } else if (strcmp(current_section, "PerformanceTuning") == 0) {
            if (strcmp(key, "MaxLifespan") == 0) {
                config->performance_tuning.max_lifespan = atoi(value);
            } else if (strcmp(key, "FarDistanceUpdateInterval") == 0) {
                config->performance_tuning.far_distance_update_interval = atoi(value);
            } else if (strcmp(key, "BatchProcessThreshold") == 0) {
                config->performance_tuning.batch_process_threshold = atoi(value);
            }
        } else if (strcmp(current_section, "VisualEffects") == 0) {
            if (strcmp(key, "EffectType") == 0) {
                config->visual_effects.effect_type = atoi(value);
            } else if (strcmp(key, "TrackingColor") == 0) {
                strncpy(config->visual_effects.tracking_color, value, sizeof(config->visual_effects.tracking_color) - 1);
            } else if (strcmp(key, "LockedColor") == 0) {
                strncpy(config->visual_effects.locked_color, value, sizeof(config->visual_effects.locked_color) - 1);
            } else if (strcmp(key, "ImpactEffectDelay") == 0) {
                config->visual_effects.impact_effect_delay = atof(value);
            }
        } else if (strcmp(current_section, "NetworkLogic") == 0) {
            if (strcmp(key, "SyncMethod") == 0) {
                config->network_logic.sync_method = atoi(value);
            } else if (strcmp(key, "PredictionTolerance") == 0) {
                config->network_logic.prediction_tolerance = atof(value);
            } else if (strcmp(key, "AntiCheatCheckRate") == 0) {
                config->network_logic.anti_cheat_check_rate = atoi(value);
            }
        }
    }

    fclose(file);
    return 0;
}

int main() {
    config_t config;

    if (read_config("config.ini", &config) == -1) {
        return 1;
    }

    // Example: Print some values to verify
    printf("BulletTrackingSettings.TrackingMode: %d\n", config.bullet_tracking_settings.tracking_mode);
    printf("Bullet.Bullet: %d\n", config.bullet.bullet);
    printf("Bulletsettings.Bullet: %d\n", config.bulletsettings.bullet);
    printf("Range.Range: %d\n", config.range.range);
    printf("Rangesettings.Range: %d\n", config.rangesettings.range);
    printf("FlightPathAdjustments.StageA_TrackingStrength: %d\n", config.flight_path_adjustments.stage_a_tracking_strength);
    printf("FlightPathB.StageB_Algorithm: %d\n", config.flight_path_b.stage_b_algorithm);
    printf("FinalStageC.StageC_SuctionStrength: %d\n", config.final_stage_c.stage_c_suction_strength);
    printf("TargetPriority.PriorityType: %d\n", config.target_priority.priority_type);
    printf("PerformanceLimits.MaxCalculationsPerFrame: %d\n", config.performance_limits.max_calculations_per_frame);
    printf("BulletPhysics.BaseSpeed: %.2f\n", config.bullet_physics.base_speed);
    printf("FlightStageA.ActiveTime: %.2f\n", config.flight_stage_a.active_time);
    printf("StageBAdvanced.WaypointCount: %d\n", config.stage_b_advanced.waypoint_count);
    printf("FinalStageC.SelfDestructDelay: %.2f\n", config.final_stage_c_2.self_destruct_delay);
    printf("MultiTargetHandling.RelockStrategy: %d\n", config.multi_target_handling.relock_strategy);
    printf("PerformanceTuning.MaxLifespan: %d\n", config.performance_tuning.max_lifespan);
    printf("VisualEffects.EffectType: %d\n", config.visual_effects.effect_type);
    printf("NetworkLogic.SyncMethod: %d\n", config.network_logic.sync_method);

    return 0;
}
