# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/config/components/idl_tool/idl.gni")
import("//build/ohos.gni")
import("//foundation/arkui/ace_engine/ace_config.gni")

time_root_path = "//base/time/time_service"
time_jsnapi_path = "${time_root_path}/framework/js/napi"
hilog_root = "//base/hiviewdfx/hilog"
c_utils_root = "//commonlibrary/c_utils"

if (target_os == "android") {
  java_library("time_android_java") {
    java_files =
        [ "android/java/src/ohos/ace/plugin/timeplugin/TimePlugin.java" ]

    subsystem_name = "plugins"
    part_name = "system_date_time"
  }

  ohos_combine_jars("time_java") {
    deps = [ ":time_android_java" ]
    jar_path = "${root_out_dir}/plugins/system_date_time/ace_time_android.jar"

    subsystem_name = "plugins"
    part_name = "system_date_time"
  }
}

template("plugin_system_date_time_static") {
  forward_variables_from(invoker, "*")

  ohos_source_set(target_name) {
    defines += invoker.defines
    defines += [ "CROSSPLATFORM" ]
    cflags_cc += invoker.cflags_cc
    configs = [ "//base/time/time_service/services:time_service_gen_config" ]

    include_dirs = [
      "//base/time/time_service/framework/js/common/include",
      "//base/time/time_service/utils/native/include",
      "//base/time/time_service/services/time/include",
      "${time_jsnapi_path}/common/include",
      "${time_root_path}/interfaces/inner_api/include",
      "${time_jsnapi_path}/system_date_time/include",
      "${time_root_path}/utils/native/include",
      "//plugins/system_date_time/mock",
      "//commonlibrary/c_utils/base/include",
    ]

    deps = [
      "${c_utils_root}/base:utilsbase",
      "${hilog_root}/interfaces/native/innerkits:libhilog_${target_os}",
      "//base/time/time_service/services:timeservice_interface",
      "//plugins/interfaces/native:ace_plugin_util_${platform}",
      "//plugins/libs/napi:napi_${target_os}",
    ]

    sources = [
      "${time_jsnapi_path}/common/src/napi_utils.cpp",
      "${time_jsnapi_path}/common/src/napi_work.cpp",
      "${time_jsnapi_path}/system_date_time/src/napi_system_date_time.cpp",
      "${time_root_path}/framework/js/common/src/time_gettime_utils.cpp",
      "date_time_init.cpp",
      "parameters.cpp",
      "time_service_client.cpp",
    ]

    if (platform == "android") {
      include_dirs += [ "//plugins/system_date_time/android/java/jni" ]
      sources += [ "//plugins/system_date_time/android/java/jni/time_jni.cpp" ]
      deps += [ ":time_java" ]
    } else if (platform == "ios") {
      include_dirs += [ "//plugins/system_date_time/ios" ]
      sources += [ "//plugins/system_date_time/ios/time_ios.mm" ]
    }

    cflags_c = [ "-fvisibility=hidden" ]

    cflags_cc += [ "-fvisibility=hidden" ]

    subsystem_name = "plugins"
    part_name = "system_date_time"
  }
}

foreach(item, ace_platforms) {
  plugin_system_date_time_static("system_date_time_static_" + item.name) {
    platform = item.name
    defines = []
    cflags_cc = []
    config = {
    }

    if (defined(item.config)) {
      config = item.config
    }

    if (defined(config.defines)) {
      defines = config.defines
    }

    if (defined(config.cflags_cc)) {
      cflags_cc = config.cflags_cc
    }
  }
}