import os
import threading
import time
import unittest

from OpenDrive.client_side import file_changes_json as c_json
from OpenDrive.client_side import interface
from OpenDrive.client_side import main
from OpenDrive.client_side import paths as client_paths
from OpenDrive.server_side import paths as server_paths
from tests.client_side.helper_client import h_register_dummy_user_device_client
from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, \
    h_clear_init_all_folders, h_create_empty


class TestMain(unittest.TestCase):

    def setUp(self) -> None:
        h_clear_init_all_folders()
        self._server_process = h_start_server_process()
        self.folder1_abs_local_path = client_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA, "folder1")
        h_create_empty(self.folder1_abs_local_path)
        main.MIN_UPDATE_PAUSE_TIME = 1

    def tearDown(self) -> None:
        main.shutdown()
        h_stop_server_process(self._server_process)

    @h_client_routine(clear_folders=False)
    def putest_start_logged_in(self):
        user = h_register_dummy_user_device_client()
        main_thread = threading.Thread(target=main.start, daemon=True)
        main_thread.start()
        time.sleep(2)   # wait till changes.json is created

        interface.add_sync_folder(self.folder1_abs_local_path, "folder1")
        expected_content = c_json.get_all_data()
        file_path = os.path.join(self.folder1_abs_local_path, "dummy.txt")
        with open(file_path, "w") as f:
            f.write("Hello World")
        time.sleep(5)   # wait till synchronization finished
        expected_path = os.path.join(server_paths.get_users_root_folder(user.user_id), "folder1/dummy.txt")
        self.assertTrue(os.path.exists(expected_path), "dummy file is not pulled to server!")
        self.assertEqual(expected_content, c_json.get_all_data())
        time.sleep(1)   # wait till waiting...

[((29, 5, 29, 42), 'tests.helper_all.h_client_routine', 'h_client_routine', (), '', False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), ((19, 8, 19, 34), 'tests.helper_all.h_clear_init_all_folders', 'h_clear_init_all_folders', ({}, {}), '()', False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), ((20, 31, 20, 55), 'tests.helper_all.h_start_server_process', 'h_start_server_process', ({}, {}), '()', False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), ((21, 38, 21, 108), 'OpenDrive.client_side.paths.normalize_path', 'client_paths.normalize_path', ({(21, 66, 21, 96): 'client_paths.LOCAL_CLIENT_DATA', (21, 98, 21, 107): '"""folder1"""'}, {}), "(client_paths.LOCAL_CLIENT_DATA, 'folder1')", True, 'from OpenDrive.client_side import paths as client_paths\n'), ((22, 8, 22, 51), 'tests.helper_all.h_create_empty', 'h_create_empty', ({(22, 23, 22, 50): 'self.folder1_abs_local_path'}, {}), '(self.folder1_abs_local_path)', False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), ((26, 8, 26, 23), 'OpenDrive.client_side.main.shutdown', 'main.shutdown', ({}, {}), '()', False, 'from OpenDrive.client_side import main\n'), ((27, 8, 27, 51), 'tests.helper_all.h_stop_server_process', 'h_stop_server_process', ({(27, 30, 27, 50): 'self._server_process'}, {}), '(self._server_process)', False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), ((31, 15, 31, 52), 'tests.client_side.helper_client.h_register_dummy_user_device_client', 'h_register_dummy_user_device_client', ({}, {}), '()', False, 'from tests.client_side.helper_client import h_register_dummy_user_device_client\n'), ((32, 22, 32, 70), 'threading.Thread', 'threading.Thread', (), '', False, 'import threading\n'), ((34, 8, 34, 21), 'time.sleep', 'time.sleep', ({(34, 19, 34, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((36, 8, 36, 73), 'OpenDrive.client_side.interface.add_sync_folder', 'interface.add_sync_folder', ({(36, 34, 36, 61): 'self.folder1_abs_local_path', (36, 63, 36, 72): '"""folder1"""'}, {}), "(self.folder1_abs_local_path, 'folder1')", False, 'from OpenDrive.client_side import interface\n'), ((37, 27, 37, 48), 'OpenDrive.client_side.file_changes_json.get_all_data', 'c_json.get_all_data', ({}, {}), '()', True, 'from OpenDrive.client_side import file_changes_json as c_json\n'), ((38, 20, 38, 74), 'os.path.join', 'os.path.join', ({(38, 33, 38, 60): 'self.folder1_abs_local_path', (38, 62, 38, 73): '"""dummy.txt"""'}, {}), "(self.folder1_abs_local_path, 'dummy.txt')", False, 'import os\n'), ((41, 8, 41, 21), 'time.sleep', 'time.sleep', ({(41, 19, 41, 20): '(5)'}, {}), '(5)', False, 'import time\n'), ((45, 8, 45, 21), 'time.sleep', 'time.sleep', ({(45, 19, 45, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((42, 37, 42, 85), 'OpenDrive.server_side.paths.get_users_root_folder', 'server_paths.get_users_root_folder', ({(42, 72, 42, 84): 'user.user_id'}, {}), '(user.user_id)', True, 'from OpenDrive.server_side import paths as server_paths\n'), ((43, 24, 43, 53), 'os.path.exists', 'os.path.exists', ({(43, 39, 43, 52): 'expected_path'}, {}), '(expected_path)', False, 'import os\n'), ((44, 43, 44, 64), 'OpenDrive.client_side.file_changes_json.get_all_data', 'c_json.get_all_data', ({}, {}), '()', True, 'from OpenDrive.client_side import file_changes_json as c_json\n')]