#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#

import pytest

import deluge.component as component
from deluge.configmanager import ConfigManager
from deluge.conftest import BaseTestCase
from deluge.i18n import setup_translation

libs_available = True
# Allow running other tests without GTKUI dependencies available
try:
    from deluge.ui.gtk3.files_tab import FilesTab
    from deluge.ui.gtk3.gtkui import DEFAULT_PREFS
    from deluge.ui.gtk3.mainwindow import MainWindow
except (ImportError, ValueError):
    # gi.require_version gives ValueError if library not available
    libs_available = False

setup_translation()


@pytest.mark.gtkui
class TestFilesTab(BaseTestCase):
    def set_up(self):
        if libs_available is False:
            pytest.skip('GTKUI dependencies not available')

        ConfigManager('gtk3ui.conf', defaults=DEFAULT_PREFS)
        self.mainwindow = MainWindow()
        self.filestab = FilesTab()
        self.t_id = '1'
        self.filestab.torrent_id = self.t_id
        self.index = 1

    def tear_down(self):
        return component.shutdown()

    def print_treestore(self, title, treestore):
        root = treestore.get_iter_first()
        level = 1

        def p_level(s, lvl):
            print('{}{}'.format(' ' * lvl, s))

        def _print_treestore_children(i, lvl):
            while i:
                p_level(treestore[i][0], lvl)
                if treestore.iter_children(i):
                    _print_treestore_children(treestore.iter_children(i), lvl + 2)
                i = treestore.iter_next(i)

        print('\n%s' % title)
        _print_treestore_children(root, level)
        print('')

    def verify_treestore(self, treestore, tree):
        def _verify_treestore(itr, tree_values):
            i = 0
            while itr:
                values = tree_values[i]
                if treestore[itr][0] != values[0]:
                    return False
                if treestore.iter_children(itr):
                    if not _verify_treestore(treestore.iter_children(itr), values[1]):
                        return False
                itr = treestore.iter_next(itr)
                i += 1
            return True

        return _verify_treestore(treestore.get_iter_first(), tree)

    def test_files_tab(self):
        self.filestab.files_list[self.t_id] = (
            {'index': 0, 'path': '1/test_10.txt', 'offset': 0, 'size': 13},
            {'index': 1, 'path': 'test_100.txt', 'offset': 13, 'size': 14},
        )
        self.filestab.update_files()
        self.filestab._on_torrentfilerenamed_event(
            self.t_id, self.index, '2/test_100.txt'
        )

        ret = self.verify_treestore(
            self.filestab.treestore,
            [['1/', [['test_10.txt']]], ['2/', [['test_100.txt']]]],
        )
        if not ret:
            self.print_treestore('Treestore not expected:', self.filestab.treestore)
        assert ret

    def test_files_tab2(self):
        self.filestab.files_list[self.t_id] = (
            {'index': 0, 'path': '1/1/test_100.txt', 'offset': 0, 'size': 13},
            {'index': 1, 'path': 'test_101.txt', 'offset': 13, 'size': 14},
        )
        self.filestab.update_files()
        self.filestab._on_torrentfilerenamed_event(
            self.t_id, self.index, '1/1/test_101.txt'
        )

        ret = self.verify_treestore(
            self.filestab.treestore,
            [['1/', [['1/', [['test_100.txt'], ['test_101.txt']]]]]],
        )
        if not ret:
            self.print_treestore('Treestore not expected:', self.filestab.treestore)
        assert ret

    def test_files_tab3(self):
        self.filestab.files_list[self.t_id] = (
            {'index': 0, 'path': '1/test_100.txt', 'offset': 0, 'size': 13},
            {'index': 1, 'path': 'test_101.txt', 'offset': 13, 'size': 14},
        )
        self.filestab.update_files()
        self.filestab._on_torrentfilerenamed_event(
            self.t_id, self.index, '1/test_101.txt'
        )

        ret = self.verify_treestore(
            self.filestab.treestore, [['1/', [['test_100.txt'], ['test_101.txt']]]]
        )
        if not ret:
            self.print_treestore('Treestore not expected:', self.filestab.treestore)
        assert ret

    def test_files_tab4(self):
        self.filestab.files_list[self.t_id] = (
            {'index': 0, 'path': '1/test_100.txt', 'offset': 0, 'size': 13},
            {'index': 1, 'path': '1/test_101.txt', 'offset': 13, 'size': 14},
        )
        self.filestab.update_files()
        self.filestab._on_torrentfilerenamed_event(
            self.t_id, self.index, '1/2/test_101.txt'
        )

        ret = self.verify_treestore(
            self.filestab.treestore,
            [['1/', [['2/', [['test_101.txt']]], ['test_100.txt']]]],
        )
        if not ret:
            self.print_treestore('Treestore not expected:', self.filestab.treestore)
        assert ret

    def test_files_tab5(self):
        self.filestab.files_list[self.t_id] = (
            {'index': 0, 'path': '1/test_100.txt', 'offset': 0, 'size': 13},
            {'index': 1, 'path': '2/test_101.txt', 'offset': 13, 'size': 14},
        )
        self.filestab.update_files()
        self.filestab._on_torrentfilerenamed_event(
            self.t_id, self.index, '1/test_101.txt'
        )

        ret = self.verify_treestore(
            self.filestab.treestore, [['1/', [['test_100.txt'], ['test_101.txt']]]]
        )
        if not ret:
            self.print_treestore('Treestore not expected:', self.filestab.treestore)
        assert ret
