from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import re

__all__ = ['Pdfdialog']

class Pdfdialog(QDialog):
    '''Dialog pro vyber listu z pdf
    '''
    def __init__(self, page, parent = None):
        super(Pdfdialog, self).__init__(parent)
        self.setWindowTitle("Choose pdf pages")
        layout = QVBoxLayout(self)
        self.checkall = QCheckBox(self)
        self.labelpage = QLabel()
        self.labelpage.setText(str(page) + ' pages available')
        self.checkall.setText('All pages')
        self.textbox = QLineEdit()
        self.labelformat = QLabel()
        self.labelformat.setText('Type page numbers:')
        self.labelex = QLabel()
        self.labelex.setText('Example: [2-3, 5, 6, 10-20]')
        layout.addWidget(self.labelpage)
        layout.addWidget(self.checkall)
        layout.addWidget(self.textbox)
        layout.addWidget(self.labelformat)
        layout.addWidget(self.labelex)
        self.sites = []
        self.corr = 0
        self.page = page

        # OK and Cancel buttons
        buttons = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
            Qt.Horizontal, self)
        buttons.accepted.connect(self.setSites)
        buttons.rejected.connect(self.reject)
        self.checkall.stateChanged.connect(self.state_changed)
        self.textbox.textEdited.connect(self.correct)
        layout.addWidget(buttons)

    def state_changed(self, int):
        '''Rozhoduti oznaceni vsech prvku
        '''
        if self.checkall.isChecked():
            self.textbox.setEnabled(0)
            self.getAll()
        else:
            self.textbox.setEnabled(1)
            self.correct()

    def getAll(self):
        '''Vlozeni vsech stranek
        '''
        self.corr = 1
        self.delSites()
        self.sites = self.sites + range(1, self.page + 1)
        self.labelformat.setText("All pages")

    def delSites(self):
        '''Vymaze vsechny stranky
        '''
        for i in self.sites:
            del i
        self.sites = []

    def correct(self):
        '''Overeni spravneho zapisu poctu stranek
        '''
        text = self.textbox.text()
        parse = text.split(',')
        reg1 = re.compile('^\s*(\d+)\s*-\s*(\d+)\s*$')
        reg2 = re.compile('^\s*\d+\s*$')
        self.delSites()
        for p in parse:
            res1 = reg1.match(p)
            res2 = reg2.match(p)
            if(res1):
                num1 = int(res1.group(1))
                num2 = int(res1.group(2))
                if (((not self.sites) or num1 > self.sites[-1]) and num1 > 0 and num1 < num2 and num2 <= self.page):
                    self.sites = self.sites + range(num1,num2+1)
                else:
                    self.corr = 0
                    self.labelformat.setText("Format ERROR")
                    return
            elif(res2):
                num1 = int(res2.group(0))
                if (((not self.sites) or num1 > self.sites[-1]) and num1 > 0 and num1 <= self.page):
                    self.sites.append(num1)
                else:
                    self.corr = 0
                    self.labelformat.setText("Format ERROR")
                    return
            else:
                self.corr = 0
                self.labelformat.setText("Format ERROR")
                return
        self.corr = 1
        self.labelformat.setText("Format OK")

    def iscorrect(self):
        '''Potvrzeni overeni spravneho zapisu poctu stranek
        '''
        return self.corr

    def setSites(self):
        '''Potvrzeni formulare
        '''
        if not self.checkall.isChecked():
            self.correct()
        else:
            self.getAll()
        if self.iscorrect():
            self.accept()

    def getSites(self):
        '''Ziskani seznamu stranek
        '''
        return self.sites


    @staticmethod
    def show(pages, parent = None):
        '''Staticka metoda pro zobrazeni dialogu
        '''
        if(pages == 1):
            return ([1], True)
        else:
            dialog = Pdfdialog(pages, parent)
            result = dialog.exec_()
            return (dialog.getSites(), result == QDialog.Accepted)