# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name                 : Mascaret
Description          : Pre and Postprocessing for Mascaret for QGIS
Date                 : June,2017
copyright            : (C) 2017 by Artelia
email                :
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 """
from qgis.core import *
from qgis.gui import *
from qgis.PyQt.uic import *
from qgis.PyQt.QtCore import *
if int(qVersion()[0])<5:   #qt4

    from qgis.PyQt.QtGui import *
else: #qt4
    from qgis.PyQt.QtWidgets import *

import os

from .ui.warningbox import Class_warningBox


class class_deletsh_dialog(QDialog):

    def __init__(self, mgis, iface):
        QDialog.__init__(self)
        self.mgis=mgis
        self.mdb =self.mgis.mdb
        self.iface = iface
        self.ui = loadUi(os.path.join(self.mgis.masplugPath, 'ui/ui_delete_sh.ui'), self)
        self.box = Class_warningBox(self.mgis)
        self.initGUI()

    def initGUI(self):
        self.listeModel = self.mdb.listeModels()
        if len(self.listeModel)>0:
            self.tree = self.ui.treeWidget
            self.parent={}
            for model in self.listeModel:
                self.parent[model] = QTreeWidgetItem(self.tree)
                self.parent[model].setText(0, model)
                self.parent[model].setFlags(self.parent[model].flags() |
                                          Qt.ItemIsTristate |
                                          Qt.ItemIsUserCheckable)
                self.parent[model].setCheckState(0, Qt.Unchecked)
        else:
            self.ui.b_delete.setDisabled(True)
        self.ui.b_delete.clicked.connect(self.lancement)
        self.ui.b_cancel.clicked.connect(self.annule)

    def lancement(self):
        selection = []
        for model in self.listeModel:
            if  self.parent[model].checkState(0) > 0:
                selection.append("{}".format(model))
        self.close()

        self.iface.messageBar().clearWidgets()
        progressMessageBar = self.iface.messageBar()
        progress = QProgressBar()
        progress.setMaximum(100)
        progressMessageBar.pushWidget(progress)

        n = len(selection)
        ok = self.box.yes_no_q('Do you want to delete ?')

        if ok:
            for i, model in enumerate(selection):
                self.mdb.drop_model(model, cascade=True)
                if self.mgis.DEBUG:
                    self.mgis.addInfo("Deletion of {0} Model is done".format(model))

                progress.setValue(i / float(n) * 100)
        else:
            if self.mgis.DEBUG:
                self.mgis.addInfo('Droping Model cancelled.')

        self.iface.messageBar().clearWidgets()

    def annule(self):
        self.close()


