import os
from os.path import dirname
from unittest import TestCase

import pytest
from src.superannotate import AppException
from src.superannotate import SAClient
from src.superannotate.lib.core import DEPRICATED_DOCUMENT_VIDEO_MESSAGE
from src.superannotate.lib.core import INVALID_PROJECT_TYPE_TO_PROCESS
from src.superannotate.lib.core import LIMITED_FUNCTIONS
from src.superannotate.lib.core import ProjectType

sa = SAClient()


class TestDeprecatedFunctionsVideo(TestCase):
    PROJECT_NAME = "TestDeprecatedFunctionsVideo"
    PROJECT_DESCRIPTION = "desc"
    PROJECT_TYPE = "Video"
    PATH_TO_URLS = "data_set/attach_urls.csv"
    TEST_FOLDER_PATH = "data_set/sample_project_vector"
    TEST_FOLDER_VIDEO_EXPORT_PATH = "data_set/sample_video_text_export"
    UPLOAD_IMAGE_NAME = "6022a74b5384c50017c366cv"
    PROJECT_NAME_2 = "second project"
    PROJECT_DESCRIPTION_2 = "second project"
    PROJECT_TYPE_2 = "Vector"
    EXCEPTION_MESSAGE = LIMITED_FUNCTIONS[ProjectType.VIDEO]
    EXCEPTION_MESSAGE_2 = INVALID_PROJECT_TYPE_TO_PROCESS
    EXCEPTION_MESSAGE_DOCUMENT_VIDEO = DEPRICATED_DOCUMENT_VIDEO_MESSAGE

    def setUp(self, *args, **kwargs):
        self.tearDown()
        sa.create_project(
            self.PROJECT_NAME, self.PROJECT_DESCRIPTION, self.PROJECT_TYPE
        )
        sa.create_project(
            self.PROJECT_NAME_2, self.PROJECT_DESCRIPTION_2, self.PROJECT_TYPE_2
        )

    def tearDown(self) -> None:
        projects = sa.search_projects(self.PROJECT_NAME, return_metadata=True)
        projects.extend(sa.search_projects(self.PROJECT_NAME_2, return_metadata=True))
        for project in projects:
            sa.delete_project(project)

    @property
    def video_export_path(self):
        return os.path.join(
            dirname(dirname(__file__)), self.TEST_FOLDER_VIDEO_EXPORT_PATH
        )

    @property
    def folder_path(self):
        return os.path.join(dirname(dirname(__file__)), self.TEST_FOLDER_PATH)

    @property
    def annotation_path(self):
        return f"{self.folder_path}/example_image_1.jpg___objects.json"

    @property
    def image_path(self):
        return f"{self.folder_path}/example_image_1.jpg"

    @pytest.mark.flaky(reruns=2)
    def test_deprecated_functions(self):
        try:
            sa.upload_images_from_folder_to_project(self.PROJECT_NAME, "some")
        except AppException as e:
            self.assertIn(self.EXCEPTION_MESSAGE, str(e))
        try:
            sa.upload_images_to_project(self.PROJECT_NAME, ["some"])
        except AppException as e:
            self.assertIn(self.EXCEPTION_MESSAGE, str(e))
        try:
            sa.upload_image_annotations(self.PROJECT_NAME, "some", self.annotation_path)
        except AppException as e:
            self.assertIn(self.EXCEPTION_MESSAGE, str(e))
        try:
            sa.download_image_annotations(
                self.PROJECT_NAME, self.UPLOAD_IMAGE_NAME, "./"
            )
        except AppException as e:
            self.assertIn(self.EXCEPTION_MESSAGE, str(e))
        try:
            sa.upload_images_to_project(
                self.PROJECT_NAME,
                [
                    self.image_path,
                ],
            )
        except AppException as e:
            self.assertIn(self.EXCEPTION_MESSAGE, str(e))
        try:
            sa.upload_video_to_project(self.PROJECT_NAME, "some path")
        except AppException as e:
            self.assertIn(self.EXCEPTION_MESSAGE, str(e))
        try:
            sa.set_project_default_image_quality_in_editor(
                self.PROJECT_NAME, "original"
            )
        except AppException as e:
            self.assertIn(self.EXCEPTION_MESSAGE_DOCUMENT_VIDEO, str(e))
        try:
            sa.prepare_export(self.PROJECT_NAME, include_fuse=True, only_pinned=True)
        except AppException as e:
            self.assertIn("Include fuse functionality is not supported", str(e))
        try:
            sa.upload_videos_from_folder_to_project(self.PROJECT_NAME, self.folder_path)
        except AppException as e:
            self.assertIn(self.EXCEPTION_MESSAGE, str(e))
