#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'ipetrash'


from PyQt5 import QtWidgets as qtw
from PyQt5.QtTest import QTest

import time
import requests
from bs4 import BeautifulSoup

from console import get_inherited_children, ROOT_URL


class MainWindow(qtw.QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('qt__class_tree__parse_and_print__recursively__from__doc_qt_io')

        self.tree = qtw.QTreeWidget()
        self.tree.setAlternatingRowColors(True)
        self.tree.setHeaderLabel('NAME')

        self.setCentralWidget(self.tree)

        self.number_total_class = 0

    def _fill_root(self, node: qtw.QTreeWidgetItem, url: str, global_number: int, indent_level=0):
        if global_number > 0 and self.number_total_class >= global_number:
            return

        QTest.qWait(1000)

        indent = '  ' * indent_level

        rs = requests.get(url)
        root = BeautifulSoup(rs.content, 'html.parser')

        name_class = root.select_one('.context > .title').text.split()[0]

        inherited_children = get_inherited_children(url, root)
        number_inherited_children = len(inherited_children)
        if number_inherited_children > 0:
            name_class = '{} ({})'.format(name_class, number_inherited_children)
            print(indent + name_class + ':')
        else:
            print(indent + name_class)

        item = qtw.QTreeWidgetItem([name_class])

        if not node:
            self.tree.addTopLevelItem(item)
        else:
            node.addChild(item)
            node.setExpanded(True)

        self.number_total_class += 1

        for name, url in inherited_children:
            self._fill_root(item, url, global_number, indent_level + 1)

    def fill_tree(self, global_number=-1):
        self.number_total_class = 0
        self.tree.clear()

        t = time.clock()

        self._fill_root(None, ROOT_URL, global_number)

        qtw.QMessageBox.information(
            self,
            'Complete!',
            'Items: {}.\nElapsed: {:.3f} sec'.format(self.number_total_class, time.clock() - t)
        )

    def closeEvent(self, e):
        quit()


if __name__ == '__main__':
    app = qtw.QApplication([])

    w = MainWindow()
    w.resize(500, 500)
    w.show()

    w.fill_tree()

    app.exec()

[((84, 10, 84, 30), 'PyQt5.QtWidgets.QApplication', 'qtw.QApplication', ({(84, 27, 84, 29): '[]'}, {}), '([])', True, 'from PyQt5 import QtWidgets as qtw\n'), ((23, 20, 23, 37), 'PyQt5.QtWidgets.QTreeWidget', 'qtw.QTreeWidget', ({}, {}), '()', True, 'from PyQt5 import QtWidgets as qtw\n'), ((35, 8, 35, 25), 'PyQt5.QtTest.QTest.qWait', 'QTest.qWait', ({(35, 20, 35, 24): '(1000)'}, {}), '(1000)', False, 'from PyQt5.QtTest import QTest\n'), ((39, 13, 39, 30), 'requests.get', 'requests.get', ({(39, 26, 39, 29): 'url'}, {}), '(url)', False, 'import requests\n'), ((40, 15, 40, 55), 'bs4.BeautifulSoup', 'BeautifulSoup', ({(40, 29, 40, 39): 'rs.content', (40, 41, 40, 54): '"""html.parser"""'}, {}), "(rs.content, 'html.parser')", False, 'from bs4 import BeautifulSoup\n'), ((44, 29, 44, 62), 'console.get_inherited_children', 'get_inherited_children', ({(44, 52, 44, 55): 'url', (44, 57, 44, 61): 'root'}, {}), '(url, root)', False, 'from console import get_inherited_children, ROOT_URL\n'), ((52, 15, 52, 48), 'PyQt5.QtWidgets.QTreeWidgetItem', 'qtw.QTreeWidgetItem', ({(52, 35, 52, 47): '[name_class]'}, {}), '([name_class])', True, 'from PyQt5 import QtWidgets as qtw\n'), ((69, 12, 69, 24), 'time.clock', 'time.clock', ({}, {}), '()', False, 'import time\n'), ((76, 78, 76, 90), 'time.clock', 'time.clock', ({}, {}), '()', False, 'import time\n')]