from qtpy import QtWidgets, QtCore, QtGui

from ..backend import franktonjunction, themeing, themeing_constants
from ..connections import signals
import uuid

from ..globals import pointers

from ..gui import ui_editorwidget


class EditorViewWidget(ui_editorwidget.ui_EditorViewWidget):
    def __init__(self, parent=None):

        super().__init__(parent)

        self.textframe = TextFrame(parent)
        self.text = self.textframe.text

        self.previewframe = PreviewFrame(parent)
        self.preview = self.previewframe.preview

        self.splitter.addWidget(self.textframe)
        self.splitter.addWidget(self.previewframe)
        self.preview_is_shown = True

    def toggle_preview(self):

        if not self.preview_is_shown:
            self.show_preview()
            self.preview_is_shown = True

        else:
            self.hide_preview()
            self.preview_is_shown = False

    def show_preview(self):
        self.splitter.setSizes([1, 1])
        show_sheet = self.text.highlighter.splitter_shown_sheet
        self.splitter.setStyleSheet(show_sheet)

    def hide_preview(self):
        self.splitter.setSizes([1, 0])
        hide_sheet = self.text.highlighter.splitter_hidden_sheet
        self.splitter.setStyleSheet(hide_sheet)


class TextFrame(ui_editorwidget.ui_EditorFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.text = TextEditor(parent)
        self.layout.addWidget(self.text)


class PreviewFrame(ui_editorwidget.ui_EditorFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.preview = ui_editorwidget.ui_Preview(parent)
        self.layout.addWidget(self.preview)
        self.setMinimumSize(0, 0)
        self.layout.setContentsMargins(30, 30, 30, 30)


class TextEditor(ui_editorwidget.ui_TextEditor):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.highlighter = themeing.MarkdownHighlighter(self)
        self._id = None
        self.completer = None

        # For citations

        self.bibpath = None
        self.suppressbib = False
        self.cslpath = None
        self.reflist_title = None

        # For filehandling
        self.savepath = None
        self.savedir = None

        self.textChanged.connect(self.on_text_change)
        self.cursorPositionChanged.connect(self.on_cursor_change)

    def on_text_change(self):
        franktonjunction.previewthread().start()
        franktonjunction.statusthread().start()
        self.maybe_new_block_state()

    def on_cursor_change(self):
        franktonjunction.statusthread().start()
        self.maybe_new_block_state()

    def get_block_state(self, cursor=None):
        if not cursor:
            cursor = self.textCursor()
        try:
            return cursor.block.userData().in_block
        except AttributeError:
            return

    def maybe_new_block_state(self):
        state = self.get_block_state()
        signals.BlockFormatSignal.emit(state)


    @property
    def id(self):
        if not self._id:
            self._id = uuid.uuid1().hex
        return self._id

    def contextMenuEvent(self, event):

        spelldict = self.highlighter.spelldict

        if not spelldict:
            return self.acceptcontext(event)
        else:
            return franktonjunction.check_this(self, event)

    def acceptcontext(self, event):
        menu = self.createStandardContextMenu()
        menu.exec_(event.globalPos())

    def keyPressEvent(self, event):
        signals.EditorKeyPressSignal.emit(event)

