#include "TaskTreeClass.h"
#include <iostream>
#include <memory>
#include <boost/filesystem.hpp>
#include <QStandardItemModel>
#include <QStringListModel>
#include <QMessageBox>
#include <unordered_set>
#include <QProcess>
#include <QDebug>
#include <QtCore>
#include <QtXml>
#include <QDomDocument>


 

TaskNode::TaskNode(QWidget* parent) :QCheckBox(parent)
{
	this->status = TaskStatusEnum::wait;
	this->setText("TaskNode");
	this->description = "TaskNode";
	this->TaskXmlPath = "";

    this->setContextMenuPolicy(Qt::CustomContextMenu);
    this->ContentListContextMenu = new QMenu(this); // 表格控件的右键菜单
    QObject::connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(ShowContentListContextMenu(QPoint)));

    //// 加载数据
    //QAction* action_Node_ExcuteTask_Action = this->ContentListContextMenu->addAction(u8"加载"); 
    //QObject::connect(action_Node_ExcuteTask_Action, SIGNAL(triggered()), this, SLOT(ExcuteTask()));
   
    // 重命名
    QAction* action_rename=this->ContentListContextMenu->addAction(u8"重命名");
    QObject::connect(action_rename, SIGNAL(triggered()), this, SLOT(renameNode()));

    // 移除
    QAction* action_remove = this->ContentListContextMenu->addAction(u8"移除");
    QObject::connect(action_remove, SIGNAL(triggered()), this, SLOT(deleteNode()));

}

TaskNode::~TaskNode()
{
    this->Nodelist.clear();
}

QString TaskNode::getName()
{
    return this->text();
}

int TaskNode::setName(QString name)
{
    this->setText(name);
    return 0;
}

QString TaskNode::getTaskName()
{
    return this->text();
}

int TaskNode::loadXmlDocument(QString xmlFilePath, QDomDocument& doc)
{
    QFile file(xmlFilePath.toUtf8().constData()); //相对路径、绝对路径、资源路径都可以
    if (!file.open(QFile::ReadOnly)) //可以用QIODevice，Truncate表示清空原来的内容
    {
        QString tiptext = "File isn't opened  in readonly mode ,file path "+ xmlFilePath;
        qDebug() << tiptext.toUtf8().constData();
        return 2;
    }

    QTextStream stream(&file);
    QTextCodec* codec = QTextCodec::codecForName(u8"UTF-8");
    stream.setCodec(codec);
    QString content = stream.readAll();
    file.close();
    if (!doc.setContent(content)) // 关联文件流
    {
        QString tiptext = u8"File isn't readed in xml format ,file path " + xmlFilePath;
        qDebug() << tiptext.toUtf8().constData();
        file.close();
        return 3;
    }
    file.close(); // 文件读取结束
    return -1;
}

int TaskNode::writeXmlDocument(QDomDocument& doc)
{

    QFile wfile(this->TaskXmlPath.toUtf8().constData()); 	// 保存 XML 文件
    if (wfile.open(QFile::ReadWrite | QFile::Text))
    {
        QTextStream out(&wfile);
        doc.save(out, QDomNode::EncodingFromDocument);
        wfile.close();
    }
    return -1;
}

int TaskNode::loadXmlFile(QString xmlFilePath)
{
    return 0;
}

int TaskNode::saveXmlFile()
{
    return 0;
}


int TaskNode::addNode(std::shared_ptr<TaskNode> n)
{
    this->Nodelist.push_back(n);
    return 0;
}

std::shared_ptr<TaskNode> TaskNode::getNode(int nid)
{
    if (nid < this->Nodelist.size()) {
        return this->Nodelist[nid];
    }
    return std::shared_ptr<TaskNode>(nullptr);
}

std::shared_ptr<TaskNode> TaskNode::getNode(QString nodename)
{
    for (int i = 0; i < this->Nodelist.size(); i++) {
        if (strcmp(nodename.toUtf8().constData(), this->Nodelist[i]->getName().toUtf8().constData()) == 0) {
            return this->Nodelist[i];
        }
    }
    return std::shared_ptr<TaskNode>(nullptr);
}

std::shared_ptr<TaskNode> TaskNode::removeAt(int nid)
{
    if (nid < this->Nodelist.size()) {
        std::shared_ptr<TaskNode> result = this->Nodelist[nid];
        this->Nodelist.erase(this->Nodelist.begin()+nid); // 删除 nid
        return result;
    }
    return std::shared_ptr<TaskNode>(nullptr);
}

std::shared_ptr<TaskNode> TaskNode::removeByName(QString nodename)
{
    for (int i = 0; i < this->Nodelist.size(); i++) {
        if (strcmp(nodename.toUtf8().constData(), this->Nodelist[i]->getName().toUtf8().constData()) == 0) {
            std::shared_ptr<TaskNode> result = this->Nodelist[i];
            this->Nodelist.erase(this->Nodelist.begin() + i); // 删除 nid
            return result;
        }
    }
    return std::shared_ptr<TaskNode>(nullptr);
}

TaskStatusEnum TaskNode::getStatus()
{
    return this->status;
   // return TaskStatusEnum();
}

int TaskNode::setStatus(TaskStatusEnum taskstatus)
{
    this->status = taskstatus;
    return 0;
}

void TaskNode::renameNode()
{
    QString newName=QInputDialog::getText(this, u8"重命名", u8"请输入新的名称", QLineEdit::Normal, this->text());
    emit this->renameNode(this->text(), newName);
}

void TaskNode::deleteNode()
{
    this->FinishTask();
    emit this->deleteNode(this->text());
}

void TaskNode::copyNewNode()
{

    emit this->copyNewNode(this->CopyToNew());
}

void TaskNode::ShowContentListContextMenu(QPoint p)
{
    this->ContentListContextMenu->exec(QCursor::pos());
}

int TaskNode::ExcuteTask()
{
    this->status = TaskStatusEnum::excuting;
    return 0;
}

int TaskNode::FinishTask()
{
    return 0;
}

TaskNode* TaskNode::CopyToNew()
{
    return nullptr;
}
 
int TaskNode::preView()
{
    return 0;
}


int TaskTreeClass::loadXmlFile(QString xmlFilePath)
{
    return 0;
}

int TaskTreeClass::saveXmlFile()
{
    return 0;
}

