/*****************************************************************************
* luaEngine Lua Engine for Qt
* Copyright (C) 2019 Syping
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/

#include "LuaEngineIO.h"
#include <QDirIterator>
#include <QTextStream>
#include <QString>
#include <QObject>
#include <QFile>
#ifdef Q_OS_WIN
#include "windows.h"
#include <iostream>
#else
#include "unistd.h"
#endif

LuaEngineIO::LuaEngineIO(QObject *parent, bool loadBaseLibraries) : LuaEngine(parent, loadBaseLibraries)
{
    L = luaState();
    pushClass(L);
}

void LuaEngineIO::pushClass(lua_State *L_p)
{
    // Directory
    pushFunction(L_p, "directoryListFiles", directoryListFiles);

    // File
    pushFunction(L_p, "linkFile", linkFile);
    pushVariant(L_p, "Symlink", (int)0);
    pushVariant(L_p, "Hardlink", (int)1);
}

void LuaEngineIO::pushClass(LuaEngine *luaEngine)
{
    pushClass(luaEngine->luaState());
}

int LuaEngineIO::directoryListFiles(lua_State *L_p)
{
    if (getArgumentCount(L_p) >= 1) {
        QStringList directories;
        QStringList nameFilters;
        QString directory = getVariant(L_p, 1).toString();
        if (getArgumentCount(L_p) >= 2) {
            nameFilters << getVariant(L_p, 2).toString();
        }
        QDirIterator dirIterator(directory, nameFilters, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
        while (dirIterator.hasNext()) {
            directories << dirIterator.next();
        }
        pushVariant(L_p, directories);
        return 1;
    }
    return 0;
}

int LuaEngineIO::linkFile(lua_State *L_p)
{
    if (getArgumentCount(L_p) >= 2) {
        bool symlinkMode = true;
        if (getArgumentCount(L_p) >= 3 && getVariant(L_p, 3).toInt() == 1) {
            symlinkMode = false;
        }
        bool linkSucceeded = false;
        if (symlinkMode) {
#ifdef Q_OS_WIN
            QString targetFile = getVariant(L_p, 2).toString();
            if (targetFile.right(4) != ".lnk") {
                targetFile += ".lnk";
            }
            linkSucceeded = QFile::link(getVariant(L_p, 1).toString(), targetFile);
#else
            linkSucceeded = QFile::link(getVariant(L_p, 1).toString(), getVariant(L_p, 2).toString());
#endif
        }
        else {
#ifdef Q_OS_WIN
            linkSucceeded = CreateHardLinkW(getVariant(L_p, 2).toString().toStdWString().c_str(), getVariant(L_p, 1).toString().toStdWString().c_str(), NULL);
#else
            linkSucceeded = link(getVariant(L_p, 1).toString().toUtf8().data(), getVariant(L_p, 2).toString().toUtf8().data());
#endif
        }
        pushVariant(L_p, linkSucceeded);
        return 1;
    }
    getVariant(L_p, false);
    return 1;
}

QString LuaEngineIO::nameForPointer(void *pointer)
{
    QString nameStorage;
    QTextStream(&nameStorage) << "LuaEngineIO" << pointer;
    return nameStorage;
}