# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import globalvars as g

class QCustomTextCursor(QTextCursor):
  def __init__(self, parent=None):
    QTextCursor.__init__(self, parent.document())
    self.parent = parent
    self.lastEnterPos = 0

  def updateLastEnterPos(self):
    self.lastEnterPos = self.position()-len(g.cursorChar)

  def updateLastEnterPosWithLen(self, slen):
    self.lastEnterPos = self.position()+slen

  def previousChar(self):
    self.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
    char = unicode(self.selection().toPlainText())
    self.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
    return char

  def nextChar(self):
    self.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
    char = unicode(self.selection().toPlainText())
    self.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
    return char

  def deleteCursorChar(self):
    if self.atEnd() and self.previousChar() == g.cursorChar:
      self.deletePreviousChar()

  def deleteUpperEmptyLine(self):
    self.movePosition(QTextCursor.Up)
    self.select(QTextCursor.LineUnderCursor)
    if len(self.selectedText()) == 0:
      self.deleteChar()
    self.movePosition(QTextCursor.End)

  def insertCharacter(self, c):
    self.clearSelection()
    if not self.atEnd() or (self.atEnd() and self.previousChar() != g.cursorChar):
      self.insertText(c)
      self.underlinePreviousChar(True)
    else:
      self.movePosition(QTextCursor.PreviousCharacter)
      self.insertText(c)
      self.movePosition(QTextCursor.NextCharacter)

  def underlinePreviousChar(self, underline):
    self.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
    cformat = self.charFormat()
    cformat.setFontUnderline(underline)
    self.setCharFormat(cformat)
    self.movePosition(QTextCursor.NextCharacter)

  def underlineNextChar(self, underline):
    self.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
    cformat = self.charFormat()
    cformat.setFontUnderline(underline)
    self.setCharFormat(cformat)
    self.movePosition(QTextCursor.PreviousCharacter)

  def keyBackspacePreviousCharacter(self):
    if self.position() > self.lastEnterPos+len(g.cursorChar):
      self.movePosition(QTextCursor.PreviousCharacter)
      self.deletePreviousChar()
      self.movePosition(QTextCursor.NextCharacter)

  def keyDeletePreviousCharacter(self):
    if not self.movePosition(QTextCursor.NextCharacter):
      return
    self.movePosition(QTextCursor.PreviousCharacter)
    self.deletePreviousChar()
    self.movePosition(QTextCursor.NextCharacter)
    self.underlinePreviousChar(True)

  def keySpace(self):
    self.movePosition(QTextCursor.PreviousCharacter)
    self.insertText(" ")
    self.movePosition(QTextCursor.NextCharacter)

  def moveLeft(self):
    if self.position() > self.lastEnterPos+len(g.cursorChar):
      self.clearSelection()
      self.movePosition(QTextCursor.PreviousCharacter)
      self.underlineNextChar(False)
      self.underlinePreviousChar(True)

  def moveRight(self):
    if not self.atEnd():
      self.clearSelection()
      self.underlinePreviousChar(False)
      self.underlineNextChar(True)
      self.movePosition(QTextCursor.NextCharacter)

  def moveToEnd(self):
    if not self.atEnd():
      char = self.previousChar()
      self.deletePreviousChar()
      self.insertHtml(char)
      self.movePosition(QTextCursor.End)
      self.insertHtml(g.cursorChar)

  def printString(self, s, withPrompt, newLine):
    self.movePosition(QTextCursor.End)
    if withPrompt == False and newLine == False:
      self.deleteCursorChar()
    elif withPrompt == True and newLine == False:
      self.select(QTextCursor.LineUnderCursor)
      self.removeSelectedText()
      self.insertHtml(g.prompt)
    elif withPrompt == False and newLine == True:
      self.deleteCursorChar()
      self.insertHtml("<br>")
    elif withPrompt == True and newLine == True:
      self.deleteCursorChar()
      self.insertHtml("<br>" + g.prompt)
    self.insertHtml(QString.fromUtf8(s))
    self.insertText(g.cursorChar)
    self.underlinePreviousChar(True)
    self.parent.scrollBar.setValue(self.parent.scrollBar.maximum())

  def selectAll(self):
    self.setPosition(self.lastEnterPos)
    self.movePosition(QTextCursor.End, QTextCursor.KeepAnchor)

  def findAllMinusLastWord(self): ###TODO: This won't preserve more than one space. Fix it somewhen in the future...
    self.selectAll()
    text = unicode(self.selection().toPlainText()).split()[:-1]
    self.movePosition(QTextCursor.End)
    if len(text) == 0:
      text = ""
    else:
      text = " ".join(text) + " "
    return text

  def findLastWord(self): ###TODO: Same as selectAllMinusLastWord()
    self.selectAll()
    lastword = unicode(self.selection().toPlainText())
    self.movePosition(QTextCursor.End)
    if len(lastword) == 1:
      lastword = ""
    else:
      lastword = lastword.split()[-1]
    return lastword