#!/usr/bin/env python
#-*- coding: UTF-8 -*-

__author__ = 'helljump'

import codecs
import logging
from PyQt4 import QtGui
import startup
import transaction
from ui import icons_rc  # @UnusedImport
from formlayout import fedit
from utils import MyProgressDialog
from lxml.html.clean import Cleaner
from lxml import etree
import re
from formlayout import OpenFile
from random import choice, randint


log = logging.getLogger(__name__)


TAG_RE = re.compile('(</?[^>]+>)', re.U | re.S | re.M)
WORD_RE = re.compile('([^\w]+)', re.U | re.S | re.M)
PATH = 'words_inject'


def tokenize(text):
    arr = []
    for chunk in TAG_RE.split(text):
        if chunk and chunk[0] == '<':
            arr.append(chunk)
            continue
        for word in WORD_RE.split(chunk):
            arr.append(word)
    return arr


def process(prj, parent=None):
    prj_prefs = prj.root()['prefs']
    prefs = prj_prefs.get(PATH, ['', 10])
    PARAMS = [
        (u'Файл ключевых слов', OpenFile(prefs[0], u'Файл ключевых слов(*.txt)')),
        (None, u'<small>Файл ключевых слов должен быть в кодировке UTF-8</small>'),
        (u'Плотность, в процентах от текста', prefs[1]),
    ]
    rc = fedit(PARAMS, title=u"Вставка ключевых слов", parent=parent, icon=QtGui.QIcon(':/aaaa.png'))
    if not rc:
        return
    prj_prefs[PATH] = rc
    transaction.commit()
    docs = prj.root()['documents']
    pdlg = MyProgressDialog(u'Обработка', u'Вставка ключевых слов', u'Отмена', 0, len(docs), parent=parent)
    pdlg.show()
    try:
        words = [l.strip() for l in codecs.open(rc[0], 'r', 'utf-8') if len(l.strip()) > 2]
        log.debug('words %i', len(words))
        for i, doc in enumerate(docs):
            if len(doc.text) < 10:
                continue
            tokenized = tokenize(doc.text)
            cnt = len(tokenized) * (rc[1] % 100)/100
            for i in range(cnt):
                p = randint(1, len(tokenized)-1)
                tokenized.insert(p, choice(words))
                if not tokenized[p+1].startswith(' '):
                    tokenized.insert(p+1, ' ')
                if not tokenized[p-1].endswith(' '):
                    tokenized.insert(p, ' ')
            doc.text = ''.join(tokenized)
            pdlg.set_value(i)
            QtGui.qApp.processEvents()
            if pdlg.wasCanceled():
                transaction.abort()
                return
        transaction.commit()
    finally:
        pdlg.close()
    if parent is not None:
        parent.model.layoutChanged.emit()


if __name__ == '__main__':

    logging.basicConfig(level=logging.DEBUG)
    root = startup.CONN.root()
    prj = root["projects"][0]
    prj.open()
    process(prj)
    prj.close()
