#include "BaseUiTool.h"
#include <QTextStream>
#include <stdlib.h>
#include <stdio.h>
#include <QString>
#include <iostream>
#include <vector>
#include <algorithm>
#include <QDebug>


QString TopAbs_ShapeEnum2QString(TopAbs_ShapeEnum v)

{
    switch (v)
    {
    case TopAbs_COMPOUND:
        return QString("TopAbs_COMPOUND");
        break;
    case TopAbs_COMPSOLID:
        return QString("TopAbs_COMPSOLID");
        break;
    case TopAbs_SOLID:
        return QString("TopAbs_SOLID");
        break;
    case TopAbs_SHELL:
        return QString("TopAbs_SHELL");
        break;
    case TopAbs_FACE:
        return QString("TopAbs_FACE");
        break;
    case TopAbs_WIRE:
        return QString("TopAbs_WIRE");
        break;
    case TopAbs_EDGE:
        return QString("TopAbs_EDGE");
        break;
    case TopAbs_VERTEX:
        return QString("TopAbs_VERTEX");
        break;
    case TopAbs_SHAPE:
        return QString("TopAbs_SHAPE");
        break;
    default:
        return QString("");
        break;
    }
}

int findStdVectordouble(const std::vector<double>& sortedVector, double target, double precision)
{
    int low = 0;
    int high = sortedVector.size() - 1;

    while (low <= high) {
        int mid = low + (high - low) / 2;
        double midValue = sortedVector[mid];

        // Check if the difference between target and midValue is smaller than precision
        if (std::abs(target - midValue) <= precision) {
            return mid; // Found a match
        }

        // Decide whether to search in the left half or right half of the vector
        if (target < midValue) {
            high = mid - 1; // Target is in the left half
        }
        else {
            low = mid + 1; // Target is in the right half
        }
    }

    return -1; // Target not found
}





int messageLog(QString logtext, int value)
{
	QMessageBox msgBox;
	QString messagestr = logtext;
	msgBox.setText(messagestr);
	msgBox.exec();
	return -1;
}


QString writeUTF8StringFile(QString path,QString unicodeString) {
    QFile fileOut(path);
    if (!fileOut.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        return "";
    }

    QTextStream streamFileOut(&fileOut);
    streamFileOut.setCodec("UTF-8");
    streamFileOut << unicodeString;
    streamFileOut.flush();
    fileOut.close();
    return path;
}

bool isNumeric(const QString& str)
{
    bool isNumber = false;
    str.toDouble(&isNumber);
    return isNumber;
}

std::complex<double> QVariant2Complex(const QVariant& variant)
{
    QVariantList list = variant.toList();
    if (list.size() >= 2) {
        double real = list[0].toDouble();
        double imag = list[1].toDouble();
        return std::complex<double>(real, imag);
    }
    else {
        // 默认返回值或错误处理
        return std::complex<double>();
    }
}

QVariant Complex2QVariant(const std::complex<double>& value)
{
    QVariantList list;
    list << value.real() << value.imag();
    return QVariant::fromValue(list);
}

QMap<QString, int> QStringVector2QMapIndex(std::vector<QString>& vlist)
{
    QMap<QString, int> result;
    for (size_t i = 0; i < vlist.size(); i++) {
        result.insert(vlist[i], i);
    }

    return result;
}

int QAbstracmodel2CsvFile(QString filepath, const QAbstractItemModel* model)
{
    qDebug() << u8"正在保存为 csv文件中";
    QFile file(filepath);
    if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
    {
        QTextStream stream(&file);
        size_t columnCount = model->columnCount();
        size_t rowCount = model->rowCount();
        // 获取文件头
        for (size_t column = 0; column < columnCount; ++column) {
            QString title = model->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
            stream << title;
            if (column < columnCount - 1) {
                stream << ",";
            }
            else {}
        }
        stream << "\n";


        // 
        for (size_t i = 0; i < rowCount; ++i) {
            for (size_t j = 0; j < columnCount; j++) {
                QString title = model->index(i, j).data().toString();
                stream << title;
                if (j < columnCount - 1) {
                    stream << ",";
                }
                else {}
            }
            stream << "\n";
        }
        file.close();
    }
    qDebug() << filepath;
    return 0;
}


