import os

from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, rmdir
from conan.tools.gnu import PkgConfigDeps
from conan.tools.scm import Git

class tgowtConan(ConanFile):
    name = "tg_owt"
    package_type = "library"
    user = "aurora"
    
    # Optional metadata
    license = "BSD-3-Clause"
    author = "Angelica Volkova <angelica.volkova@fruct.org>"
    url = "https://gitverse.ru/auroraos/mirror_tg_owt"
    description = "Telegram WebRTC"
    topics = ("webrtc", "telegram", "webrtc-library")

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {
        "shared": [True, False],
        "use_pipewire": [True, False],
        "use_h264": [True, False]
    }
    default_options = {
        "shared": False,
        "use_pipewire": False,
        "use_h264": True
    }


    def requirements(self):
        self.requires("abseil/20240116.1@aurora")
        self.requires("crc32c/1.1.2@aurora")
        self.requires("ffmpeg/7.0.1@aurora")
        self.requires("mozjpeg/4.1.5@aurora")
        self.requires("openh264/2.5.0@aurora")
        self.requires("openssl/3.2.3@aurora")
        self.requires("opus/1.5.2@aurora")
        self.requires("libsrtp/2.5.0@aurora")
        self.requires("libvpx/1.14.1@aurora")

    def layout(self):
        cmake_layout(self, src_folder="src")

    def export_sources(self):
        export_conandata_patches(self)

    def source(self):
        git = Git(self)
        git.clone(url="https://github.com/desktop-app/tg_owt.git", target=self.source_folder) 
        git.checkout(commit="698c4f754faee18f49cd3b4c1f89b419f7e80da5")
        self.run("git submodule update --init --recursive", cwd=self.source_folder)
    
    def generate(self):
        deps = CMakeDeps(self)
        deps.generate()
        
        pkg_deps = PkgConfigDeps(self)
        pkg_deps.generate()
        
        tc = CMakeToolchain(self)
        tc.cache_variables["TG_OWT_USE_PIPEWIRE"] = self.options.use_pipewire
        tc.cache_variables["TG_OWT_USE_X11"] = False
        tc.generate()

    def build(self):
        apply_conandata_patches(self)
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        copy(self, pattern="LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
        cmake = CMake(self)
        cmake.install()
        rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))

    def package_info(self):
        self.cpp_info.set_property("pkg_config_name", "tg_owt")
        self.cpp_info.libs = ["tg_owt"]
        self.cpp_info.includedirs = [
                "include/tg_owt",
                "include/tg_owt/api",
                "include/tg_owt/call",
                "include/tg_owt/common_video",
                "include/tg_owt/logging",
                "include/tg_owt/media",
                "include/tg_owt/modules",
                "include/tg_owt/p2p",
                "include/tg_owt/rtc_base",
                "include/tg_owt/system_wrappers",
                "include/tg_owt/third_party/abseil-cpp",
                "include/tg_owt/third_party/libyuv/include",
                "include/tg_owt/third_party/libsrtp/include",
                ]
        if self.settings.os == "Windows":
            self.cpp_info.defines = ["WEBRTC_WIN", "NOMINMAX"]
            self.cpp_info.system_libs = ['secur32', 'winmm', 'dmoguids',
                    'wmcodecdspuuid', 'msdmo', 'Strmiids']
        if self.settings.os == "Linux":
            self.cpp_info.system_libs = ["dl"]
            self.cpp_info.defines = ["WEBRTC_POSIX", "WEBRTC_LINUX"]
        if self.settings.os == "Macos":
            self.cpp_info.defines = ["WEBRTC_POSIX", "WEBRTC_MAC"]
        if self.settings.os == "iOS":
            self.cpp_info.defines = ["WEBRTC_POSIX", "WEBRTC_IOS", "WEBRTC_MAC"]
        if self.options.use_h264:
            self.cpp_info.defines += ["WEBRTC_USE_H264"]
