#include "k9assistant.h"
#include "k9astsource.h"
#include "k9astdvdoption.h"
#include "k9astdestination.h"
#include "k9astmp4.h"
#include "k9asttitles.h"
#include "k9aststreams.h"
#include "k9astmpeg.h"
#include "k9dialogs.h"
#include "k9execcopy.h"
#include "k9tools.h"
#include "k9dvdtitle.h"
#include "k9settings.h"
#include "aboutbox.h"
k9Assistant::k9Assistant(QWidget *parent):QWizard(parent) {
    setWindowIcon(QIcon(":/icons/icon-app"));
    setWindowTitle("backlite");
    setPixmap(QWizard::LogoPixmap,QPixmap(":/icons/icon-app"));
    m_drives=new k9CdDrives(this);
}


void k9Assistant::back() {
   QWizard::back();
}

void k9Assistant::customButtonClicked ( int which ) {
    k9Settings settings(k9Dialogs::getMainWidget(),"");
    settings.exec();

}

void k9Assistant::helpRequested() {
   AboutBox about;
   about.exec();
}

void k9Assistant::run() {
   connect(this,SIGNAL(customButtonClicked(int)),this,SLOT(customButtonClicked(int)));
   connect(this,SIGNAL(helpRequested()),this,SLOT(helpRequested()));
   setOption(QWizard::HaveCustomButton1,true);
   setOption(QWizard::HaveHelpButton,true);
   setButtonText(QWizard::HelpButton,i18n("about"));


   setButtonText(QWizard::CustomButton1 ,i18n("settings"));
   m_sourcePage=new QWizardPage;
   m_sourcePage->setTitle(i18n("DVD Backup"));
   m_sourcePage->setSubTitle(i18n("Select the source of the backup"));
   m_astSource=new k9astSource(this,m_drives,&m_dvd);
   QGridLayout *layout = new QGridLayout();
   layout->addWidget(m_astSource,1,1);
   m_sourcePage->setLayout(layout);
   setPage(page_source, m_sourcePage);


   m_destinationPage=new QWizardPage;
   m_destinationPage->setTitle(i18n("DVD Backup"));
   m_destinationPage->setSubTitle(i18n("Select the destination of the backup"));
   m_astDestination=new k9astDestination(this,m_drives,&m_dvd);
   layout = new QGridLayout();
   layout->addWidget(m_astDestination,1,1);
   m_destinationPage->setLayout(layout);
   setPage(page_destination, m_destinationPage);

   m_titlesPage=new QWizardPage;
   m_titlesPage->setTitle(i18n("DVD Backup"));
   m_titlesPage->setSubTitle(i18n("Select the titles you want to copy"));
   m_astTitles=new k9astTitles(this,&m_dvd);
   layout = new QGridLayout();
   layout->addWidget(m_astTitles,1,1);
   m_titlesPage->setLayout(layout);
   setPage(page_titles, m_titlesPage);

   m_streamsPage=new QWizardPage;
   m_streamsPage->setTitle(i18n("DVD Backup"));
   m_streamsPage->setSubTitle(i18n("Select the streams you want to keep"));
   m_astStreams=new k9astStreams(this,&m_dvd);
   layout = new QGridLayout();
   layout->addWidget(m_astStreams,1,1);
   m_streamsPage->setLayout(layout);
   setPage(page_streams, m_streamsPage);

    m_DVDOptionPage=new QWizardPage;
    m_DVDOptionPage->setTitle(i18n("DVD Backup"));
   m_DVDOptionPage->setSubTitle(i18n("Set DVD playback options"));
   m_astDVDOption=new k9astDVDOption(this,&m_dvd);
   layout = new QGridLayout();
   layout->addWidget(m_astDVDOption,1,1);
   m_DVDOptionPage->setLayout(layout);
   setPage(page_DVDOption, m_DVDOptionPage);

    m_mpegPage=new QWizardPage;
    m_mpegPage->setTitle(i18n("DVD Backup"));
   m_mpegPage->setSubTitle(i18n("Set mpeg options for each title"));
   m_astMpeg=new k9astMpeg(this,&m_dvd);
   layout = new QGridLayout();
   layout->addWidget(m_astMpeg,1,1);
   m_mpegPage->setLayout(layout);
   setPage(page_mpeg, m_mpegPage);


   m_mp4Page=new QWizardPage,
   m_mp4Page->setTitle(i18n("DVDBackup"));
   m_mp4Page->setSubTitle(i18n("Set mp4 options for each title"));
   m_astMp4=new k9astMp4(this,&m_dvd);
   layout = new QGridLayout();
   layout->addWidget(m_astMp4,1,1);
   m_mp4Page->setLayout(layout);
   setPage(page_mp4, m_mp4Page);



    connect(m_astTitles,SIGNAL(addTitle(k9DVDTitle*)),m_astStreams,SLOT(addTitle(k9DVDTitle*)));
    connect(m_astTitles,SIGNAL(removeTitle(k9DVDTitle*)),m_astStreams,SLOT(removeTitle(k9DVDTitle*)));

    connect(m_astTitles,SIGNAL(addTitle(k9DVDTitle*)),m_astMpeg,SLOT(addTitle(k9DVDTitle*)));
    connect(m_astTitles,SIGNAL(removeTitle(k9DVDTitle*)),m_astMpeg,SLOT(removeTitle(k9DVDTitle*)));

    connect(m_astTitles,SIGNAL(addTitle(k9DVDTitle*)),m_astMp4,SLOT(addTitle(k9DVDTitle*)));
    connect(m_astTitles,SIGNAL(removeTitle(k9DVDTitle*)),m_astMp4,SLOT(removeTitle(k9DVDTitle*)));

   connect(m_astTitles,SIGNAL(addTitle(k9DVDTitle*)),m_astDVDOption,SLOT(addTitle(k9DVDTitle*)));
    connect(m_astTitles,SIGNAL(removeTitle(k9DVDTitle*)),m_astDVDOption,SLOT(removeTitle(k9DVDTitle*)));

    m_drives->scanDrives();

}

void k9Assistant::cleanupPage(int id) {
   if (currentPage() ==m_destinationPage) {
        for (int i=0;i< m_dvd.gettitleCount();i++) {
            k9DVDTitle *title=m_dvd.gettitle(i);
            title->setSelected(false);

        }
    }
}

int k9Assistant::nextId() const {
    switch (currentId()) {
    case page_source:
        return page_destination;
    case page_destination:
        return page_titles;
    case page_titles:
        return page_streams;
    case page_streams:
        switch (destination()) {
        case dstAvi:
        case dstAudio:
            return page_mp4;
        case dstMpeg:
            return page_mpeg;
        case dstDVD:
        default:
            return page_DVDOption;
        }

    case page_DVDOption:
        return page_mpeg;
    case page_mpeg:
    case page_mp4:
    default:
        return -1;
    }
}

bool k9Assistant::validateCurrentPage()  {
    QString err="";

    m_astTitles->stopPreview();

    if (   currentPage() == m_sourcePage) {
        //m_astStreams->clear();
        m_astDVDOption->clear();
        if (m_astSource->openDvd()){
            m_astTitles->fill();
            m_astDestination->updatePaths();
        }
        else
            err=m_dvd.geterrMsg();
    } else if (currentPage() == m_destinationPage) {

        if (!m_astDestination->isOk())
                err=i18n("The destination of the copy isn't valid");

        m_astStreams->selectOne(false);
        if (m_astDestination->isMatroska())
           m_astMp4->setMatroskaCodecs();
        if ((m_destination== dstAvi) && !m_astDestination->isMatroska()) {
            m_astStreams->selectOne(true);
            m_astStreams->hideSubtitles(false);
            m_astMp4->setAviCodecs();
        } else if (m_destination==dstAudio) {
            m_astMp4->setAudio();
            m_astStreams->hideSubtitles(true);
        } else
            m_astStreams->hideSubtitles(false);

        m_astStreams->updateColumns();
    } else if (currentPage() == m_titlesPage) {
        m_astTitles->updateTitles();
        if (!m_astTitles->isOk())
            err=i18n("You must select at least on title !");
        else
            m_astTitles->stopPreview();
        m_astStreams->updateColumns();
    } else if (currentPage() == m_DVDOptionPage) {
        m_astDVDOption->updateOptions();
        m_astMpeg->updateFactor();
    } else if (currentPage() == m_streamsPage) {
        m_astStreams->updateStreams();

    }
    if (err=="") {
        return true;
        //streams->updateColumns();
    } else {
        k9Dialogs::error(err,i18n("cannot continue"));
        return false;
    }
}

void k9Assistant::accept() {
    m_astTitles->unloadPreview();


    k9ExecCopy execCopy;
    execCopy.setCopyMenus(m_copyMenus);
    execCopy.setPath(m_astDestination->getPath());
    if (m_DVDdestination== dstFolder)
        execCopy.setOutput(k9ExecCopy::oFolder);
    else if (m_DVDdestination==dstIso)
        execCopy.setOutput(k9ExecCopy::oISO);
    else
        execCopy.setOutput(k9ExecCopy::oDVD);
    execCopy.setDvd(&m_dvd);

    switch (m_destination) {
        case dstDVD:
            execCopy.copyDVD();
            break;
        case dstAvi:
            execCopy.CreateMP4();
            break;
        case dstMpeg:
            execCopy.extractMPEG2();
            break;
        case dstAudio:
            execCopy.extractAudio();
        default:
            break;
     }
    k9Config config;
    config.setKeepMenus(m_copyMenus);
    if (!execCopy.isCancelled()) {

        if (config.getPrefDelTmpFiles())
            k9Tools::clearOutput();

        QWizard::accept();
    }
}
