# -*- coding: utf-8 -*-

"""
Module implementing GuiConvert.
"""

from PyQt4.QtGui import QWidget
from PyQt4.QtCore import pyqtSignature

from Ui_guiconvert import Ui_GuiConvert

from PyQt4 import QtCore, QtGui
import logging
import sys
import cfg, wbfs2fat

logger = logging.getLogger(__name__)

class GuiConvert(QWidget, Ui_GuiConvert):
    """
    Class documentation goes here.
    """
    def __init__(self, parent = None):
        """
        Constructor
        """
        QWidget.__init__(self, parent)
        self.setupUi(self)
        if not sys.platform == "linux2":
            self.fsck.setEnabled(0)
        cfg.options.convert = True
        self.readcfg()

    def readcfg(self):
        self.backup.setChecked(bool(cfg.options.backup))
        self.fsck.setChecked(bool(cfg.options.fsck))
        self.ignore_xlink.setChecked(bool(wbfs2fat.Status.err_xlink in cfg.ignore))
        self.auto_restore.setChecked(bool(cfg.options.auto_restore))
        self.create_mbr.setChecked(bool(cfg.options.create_mbr))
        self.backup_dir.setText(cfg.options.backup_dir)
        self.verbosity.setValue(cfg.options.verbosity)
        self.checkoptions()

    def checkoptions(self, fix=0):
        self.plainTextEdit.clear()
        state, check = cfg.dev.checkoptions(fix)
        for line in check:
            self.plainTextEdit.appendPlainText(line+"\n")
        if state & 1: self.readcfg()
        elif state & 2: self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ignore | QtGui.QDialogButtonBox.Cancel)
        elif state & 4: self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel)
        else: self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
        return state

    @pyqtSignature("bool")
    def on_backup_toggled(self, checked):
        """
        Slot documentation goes here.
        """
        cfg.options.backup = checked
        self.checkoptions()

    @pyqtSignature("bool")
    def on_auto_restore_toggled(self, checked):
        """
        Slot documentation goes here.
        """
        cfg.options.auto_restore = checked
        self.checkoptions()

    @pyqtSignature("bool")
    def on_create_mbr_toggled(self, checked):
        """
        Slot documentation goes here.
        """
        cfg.options.create_mbr = checked
        self.checkoptions()

    @pyqtSignature("bool")
    def on_ignore_xlink_toggled(self, checked):
        """
        Slot documentation goes here.
        """
        cfg.set_ignore(wbfs2fat.Status.err_xlink, checked)
        self.checkoptions()

    @pyqtSignature("bool")
    def on_fsck_toggled(self, checked):
        """
        Slot documentation goes here.
        """
        cfg.options.fsck = checked
        self.checkoptions()

    @pyqtSignature("bool")
    def on_backup_dialog_clicked(self, checked):
        """
        Slot documentation goes here.
        """
        self.backup_dir.setText(QtGui.QFileDialog.getExistingDirectory(self, 'Select Directory',  cfg.options.backup_dir))

    @pyqtSignature("QString")
    def on_backup_dir_textChanged(self, p0):
        """
        Slot documentation goes here.
        """
        cfg.options.backup_dir = str(p0)
        self.checkoptions()

    @pyqtSignature("int")
    def on_verbosity_valueChanged(self, p0):
        """
        Slot documentation goes here.
        """
        cfg.options.verbosity = p0
        self.checkoptions()

    @pyqtSignature("")
    def on_buttonBox_accepted(self):
        """
        Slot documentation goes here.
        """
        if self.checkoptions(1): return
        self.optionBox.hide()
        self.buttonBox.setEnabled(0)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Abort)
        self.plainTextEdit.clear()
        self.plainTextEdit.update()
        QtCore.QCoreApplication.processEvents()
        try:
            cfg.dev.convert()
            self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Close)
        except Exception as e:
            logger.error("Exception caught",exc_info=1)
            self.plainTextEdit.appendPlainText("ERROR: "+str(e))
        finally:
            self.buttonBox.setEnabled(1)

    @pyqtSignature("")
    def on_buttonBox_rejected(self):
        """
        Slot documentation goes here.
        """
        self.close()
