#include "AdbUtils.h"
#include <QProcess>
#include <QString>
#include <QDir>
#include <fstream>

AdbUtils::AdbUtils(QObject *parent) : QObject(parent) {
}

bool AdbUtils::checkDevices() {
    QProcess process;
    process.start("adb", QStringList() << "devices");
    process.waitForFinished();
    const QString output = process.readAllStandardOutput();
    qDebug() << "ADB devices output:" << output;

    // Проверка на наличие строки "\tdevice" в выводе
    return output.contains("\tdevice");
}

bool AdbUtils::startApp(const QString &packageName) {
    QProcess process;
    process.start("adb", QStringList() << "shell" << "monkey" << "-p" << packageName << "-c" << "android.intent.category.LAUNCHER" << "1");
    process.waitForFinished();
    const QString output = process.readAllStandardOutput();
    qDebug() << "Start app output:" << output;

    return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
}

bool AdbUtils::dumpAndPullUiXml(const QString &localPath) {
    QProcess process;
    process.start("adb", QStringList() << "shell" << "uiautomator" << "dump" << "/sdcard/ui_dump.xml");
    process.waitForFinished();
    if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) {
        qDebug() << "Ошибка в получении дампа UI на устройстве";
        return false;
    }

    process.start("adb", QStringList() << "pull" << "/sdcard/ui_dump.xml" << localPath);
    process.waitForFinished();
    if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) {
        qDebug() << "Ошибка в передаче xml файла на компьютер";
        return false;
    }

    return true;
}

bool AdbUtils::tap(int x, int y) {
    QProcess process;
    process.start("adb", QStringList() << "shell" << "input" << "tap" << QString::number(x) << QString::number(y));
    process.waitForFinished();
    const QString output = process.readAllStandardOutput();
    qDebug() << "Tap output:" << output;
    return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
}

bool AdbUtils::swipe(const int x1, const int y1, const int x2, const int y2) {
    QProcess process;
    process.start("adb", QStringList() << "shell" << "input" << "swipe" << QString::number(x1) << QString::number(y1) << QString::number(x2) << QString::number(y2));
    process.waitForFinished();
    const QString output = process.readAllStandardOutput();
    qDebug() << "Swipe output:" << output;
    return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
}

bool AdbUtils::inputText(const QString &text) {
    QProcess process;
    process.start("adb", QStringList() << "shell" << "input" << "text" << text);
    process.waitForFinished();
    const QString output = process.readAllStandardOutput();
    qDebug() << "Input text output:" << output;
    return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
}

// bool AdbUtils::takeScreenshot(const QString& filename) {
//     QProcess process;
//     QDir().mkdir("screens");  // Создаем папку, если её нет
//     process.start("adb", QStringList() << "exec-out" << "screencap" << "-p" << ">" << "screens/" + filename);
//     process.waitForFinished();
//     return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
// }

// void AdbUtils::log(const std::string& message) {
//     std::ofstream out("log.txt", std::ios::app);
//     std::time_t t = std::time(nullptr);
//     out << "[" << std::put_time(std::localtime(&t), "%H:%M:%S") << "] " << message << std::endl;
//     std::cout << message << std::endl;
// }
