#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <thread>
#include <chrono>
#include <numeric>

bool readFile(const std::string& filename, std::vector<int>& data) {
    std::ifstream file(filename);

    if (!file.is_open()) {
        std::cerr << "FEHLER: Konnte Datei nicht oeffnen!\n";
        std::cerr << "Pfad: " << filename << "\n";
        return false;
    }

    int value;
    std::cout << "Lese Datei ein... (Pfad: " << filename << ")\n";

    while (file >> value) {
        data.push_back(value);
    }

    file.close();
    return true;
}

int main() {
    std::vector<int> messungen;
    std::string pfad = "C:\\Users\\Bruno\\Desktop\\BME\\BME 3\\PROG B\\praktikum\\thread\\measurements.txt";

    if (!readFile(pfad, messungen)) {
        std::cout << "\nDruecke ENTER zum Beenden...";
        std::cin.get();
        return 1;
    }

    if (messungen.size() < 2) {
        std::cout << "Datei ist leer oder hat zu wenig Werte!\n";
        std::cin.get();
        return 0;
    }

    std::cout << "Erfolg! " << messungen.size() << " Messwerte geladen.\n";
    std::cout << "-----------------------------------\n";

    {
        std::cout << "Starte serielle Berechnung...\n";
        auto start = std::chrono::steady_clock::now();

        long long increases = 0;
        for (size_t i = 1; i < messungen.size(); i++) {
            if (messungen[i] > messungen[i - 1]) {
                increases++;
            }
        }

        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff = end - start;

        std::cout << "[SERIELL]  Anstiege: " << increases << "\n";
        std::cout << "           Zeit:     " << diff.count() << " s\n\n";
    }

    {
        std::cout << "Starte parallele Berechnung...\n";
        auto start = std::chrono::steady_clock::now();

        unsigned int numThreads = std::thread::hardware_concurrency();
        if (numThreads == 0) numThreads = 4;

        std::vector<std::thread> threads;
        std::vector<long long> results(numThreads, 0);

        size_t totalComparisons = messungen.size() - 1;
        size_t base = totalComparisons / numThreads;
        size_t remainder = totalComparisons % numThreads;

        size_t idx = 1;

        for (unsigned int t = 0; t < numThreads; t++)
        {
            size_t startIdx = idx;
            size_t count = base + (t < remainder ? 1 : 0);
            size_t endIdx = idx + count - 1;

            idx = endIdx + 1;

            threads.emplace_back([&, t, startIdx, endIdx]() {
                long long localCount = 0;
                if (startIdx < messungen.size()) {
                    for (size_t i = startIdx; i <= endIdx; ++i) {
                        if (messungen[i] > messungen[i - 1]) {
                            ++localCount;
                        }
                    }
                }
                results[t] = localCount;
            });
        }

        for (auto& th : threads) {
            if (th.joinable())
                th.join();
        }

        long long totalParallel = 0;
        for (long long r : results) {
            totalParallel += r;
        }

        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff = end - start;

        std::cout << "[PARALLEL] Anstiege: " << totalParallel << " (Threads: " << numThreads << ")\n";
        std::cout << "           Zeit:     " << diff.count() << " s\n";
    }

    std::cout << "\n-----------------------------------\n";
    std::cout << "Druecke ENTER zum Beenden...";
    std::cin.get();

    return 0;
}