#include "cdlgnewfile.h"
#include "ui_cdlgnewfile.h"
#include "mainwindow.h"
#include "logic.h"
CDlgNewFile::CDlgNewFile(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CDlgNewFile)
{
    ui->setupUi(this);
    ui->label_Info->setStyleSheet("color:red");
    ui->lineEdit_FileName->m_bNumOnly=false;
}

CDlgNewFile::~CDlgNewFile()
{
    delete ui;
}

void CDlgNewFile::SetCreateInfo(const QString &strPath,
                              QString &strName,
                              QString &strRelPath,
                              FileStyle eFileStyle)
{
    ui->lineEdit_FilePath->setText(strPath);
    ui->lineEdit_FileName->setText(strName);
    m_strRelpath=strRelPath;
    m_eFileStyle=eFileStyle;
}

void CDlgNewFile::CreateFileNormal()
{
    QString path= ui->lineEdit_FilePath->text();
    QString text = ui->lineEdit_FileName->text();
    QString strRelpath=m_strRelpath;
    if(text.isEmpty())
    {
        MQT_THROW_STR(tr("File name can not empty"));
    }

    if(strRelpath.isEmpty())
    {
        strRelpath=QFileInfo(text).baseName();
    }
    else
    {
        strRelpath+="/"+QFileInfo(text).baseName();
    }

    path+="/"+QFileInfo(text).baseName()+"."+FILE_FILTER;

    QFileInfo info(path);
    if(info.exists())
    {
        MQT_THROW_STR(tr("File aleady exist"));
    }

    QDir dir_model;
    QString newpatternpath=CMakePath::make_pattern_path(strRelpath);
    bool bsuccess=dir_model.mkpath(newpatternpath);
    if(!bsuccess)
    {
        MQT_THROW_STR(tr("Make Pattern path failed,check pattern directory"));
    }
    else
    {
        //复制模板文件
        QString s=m_gSystem.GetCurFile();
        QString curpatternpath= CMakePath::make_pattern_path(s.left(s.length()-4)) ;
        QDir dir(curpatternpath);
        QStringList filelist=dir.entryList(QDir::Files);
        foreach(QString s,filelist)
        {
            QFile::copy(curpatternpath+"/"+s,newpatternpath+"/"+s);
        }
    }
    m_gMainWindow->save_File(path);
    strRelpath=strRelpath+"."+FILE_FILTER;
    m_gSystem.SetCurFile(strRelpath);
}

void CDlgNewFile::CreateFilePath()
{

    QString path= ui->lineEdit_FilePath->text();
    QString text = ui->lineEdit_FileName->text();
    QString strRelpath=m_strRelpath;
    if(text.isEmpty())
    {
        MQT_THROW_STR(tr("Path name can not empty"));
    }
    QFileInfo infodata(path);
    infodata.absoluteDir();

    QString dirPath=path+"/"+text;
    if(QFileInfo(dirPath).exists())
    {
        MQT_THROW_STR(tr("Path aleady exist"));
    }
    bool  bsuccess=false;
    bsuccess=infodata.absoluteDir().mkdir(infodata.baseName()+"/"+text);
    if(!bsuccess)
    {
       MQT_THROW_STR(QObject::trUtf8("%1 path create failed")
                                  .arg(strRelpath));
    }

    QFileInfo infopattern(CMakePath::make_pattern_path(strRelpath));
    //infopattern.absoluteDir();
    bsuccess=infopattern.absoluteDir().mkpath(infopattern.baseName()+"/"+text);
    if(!bsuccess)
    {
        MQT_THROW_STR(QObject::trUtf8("%1 pattern path create failed")
                                  .arg(strRelpath));
    }
}

void CDlgNewFile::showEvent(QShowEvent *)
{
    ui->label_Info->clear();
	ui->lineEdit_FileName->setFocus();
}

void CDlgNewFile::on_buttonBox_accepted()
{
    try
    {
        if(m_eFileStyle==FILE_NORMAL)
        {
            CreateFileNormal();
        }
        else if(m_eFileStyle==FILE_DIR)
        {
            CreateFilePath();
        }
    }
    catch(QString& s)
    {
        ui->label_Info->setText(trUtf8("new file failed:%1.").arg(s));
        return;
    }
    m_gMainWindow->FileTreeFresh();
    this->hide();
    this->accepted();
}

void CDlgNewFile::on_buttonBox_rejected()
{
    this->hide();
    this->rejected();
}
