# Copyright 2010-2021, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

load(
    "//:build_defs.bzl",
    "mozc_cc_library",
    "mozc_cc_test",
    "mozc_macos_application",
    "mozc_objc_library",
    "mozc_select",
)
load(
    "//:config.bzl",
    "BRANDING",
    "MACOS_BUNDLE_ID_PREFIX",
)
load("//bazel/win32:build_defs.bzl", "features_gdi")

package(default_visibility = ["//visibility:private"])

mozc_cc_library(
    name = "renderer_client",
    srcs = ["renderer_client.cc"],
    hdrs = ["renderer_client.h"],
    visibility = [
        "//gui:__subpackages__",
        "//mac:__pkg__",
        "//renderer:__subpackages__",
        "//unix:__subpackages__",
        "//visibility:public",
        "//win32:__subpackages__",
    ],
    deps = [
        ":renderer_interface",
        "//base:clock",
        "//base:process",
        "//base:system_util",
        "//base:thread",
        "//base:version",
        "//base:vlog",
        "//client:client_interface",
        "//ipc",
        "//ipc:named_event",
        "//protocol:renderer_cc_proto",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
    ] + mozc_select(
        ios = ["//base/mac:mac_util"],
        macos = ["//base/mac:mac_util"],
        windows = [
            "//base/win32:win_sandbox",
        ],
    ),
)

mozc_cc_test(
    name = "renderer_client_test",
    size = "medium",
    srcs = ["renderer_client_test.cc"],
    tags = ["noandroid"],
    deps = [
        ":renderer_client",
        "//base:number_util",
        "//base:version",
        "//base/strings:zstring_view",
        "//ipc",
        "//protocol:commands_cc_proto",
        "//protocol:renderer_cc_proto",
        "//testing:gunit_main",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
    ],
)

mozc_cc_library(
    name = "renderer_server",
    srcs = ["renderer_server.cc"],
    hdrs = ["renderer_server.h"],
    # TODO(b/180075250): IPC tests don't pass in forge
    tags = ["nowin"],
    visibility = ["//renderer:__subpackages__"],
    deps = [
        ":renderer_interface",
        "//base:const",
        "//base:system_util",
        "//base:vlog",
        "//client:client_interface",
        "//config:config_handler",
        "//ipc",
        "//ipc:named_event",
        "//ipc:process_watch_dog",
        "//protocol:commands_cc_proto",
        "//protocol:config_cc_proto",
        "//protocol:renderer_cc_proto",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
    ],
)

mozc_cc_test(
    name = "renderer_server_test",
    size = "medium",
    srcs = ["renderer_server_test.cc"],
    tags = ["noandroid"],
    deps = [
        ":renderer_client",
        ":renderer_interface",
        ":renderer_server",
        "//ipc",
        "//ipc:ipc_test_util",
        "//protocol:renderer_cc_proto",
        "//testing:gunit_main",
        "//testing:mozctest",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
    ],
)

mozc_cc_library(
    name = "table_layout",
    srcs = [
        "table_layout.cc",
        "table_layout_interface.h",
    ],
    hdrs = ["table_layout.h"],
    visibility = ["//renderer:__subpackages__"],
    deps = [
        "//base:coordinates",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
    ],
)

mozc_cc_test(
    name = "table_layout_test",
    size = "small",
    srcs = [
        "table_layout_interface.h",
        "table_layout_test.cc",
    ],
    tags = ["noandroid"],
    deps = [
        ":table_layout",
        "//base:coordinates",
        "//testing:gunit_main",
    ],
)

mozc_cc_library(
    name = "window_util",
    srcs = ["window_util.cc"],
    hdrs = ["window_util.h"],
    visibility = ["//renderer:__subpackages__"],
    deps = ["//base:coordinates"],
)

mozc_cc_library(
    name = "renderer_interface",
    hdrs = ["renderer_interface.h"],
    visibility = [
        "//mac:__pkg__",
        "//renderer:__subpackages__",
        "//unix:__subpackages__",
        "//visibility:public",
    ],
    deps = [
        "//client:client_interface",
        "//protocol:renderer_cc_proto",
    ],
)

mozc_cc_library(
    name = "renderer_mock",
    testonly = True,
    hdrs = ["renderer_mock.h"],
    visibility = [
        "//unix:__subpackages__",
        "//visibility:public",
    ],
    deps = [
        ":renderer_interface",
        "//client:client_interface",
        "//protocol:renderer_cc_proto",
        "//testing:gunit",
    ],
)

mozc_cc_test(
    name = "window_util_test",
    size = "small",
    srcs = ["window_util_test.cc"],
    tags = ["noandroid"],
    deps = [
        ":window_util",
        "//base:coordinates",
        "//testing:gunit_main",
    ],
)

mozc_cc_library(
    name = "renderer_style_handler",
    srcs = ["renderer_style_handler.cc"],
    hdrs = ["renderer_style_handler.h"],
    features = features_gdi(),  # for Windows
    visibility = ["//renderer:__subpackages__"],
    deps = [
        "//base:singleton",
        "//protocol:renderer_cc_proto",
    ] + mozc_select(
        windows = [
            "//bazel/win32:gdi32",
            "//bazel/win32:user32",
            "@com_microsoft_wil//:wil",
        ],
    ),
)

mozc_cc_test(
    name = "renderer_style_handler_test",
    size = "small",
    srcs = ["renderer_style_handler_test.cc"],
    deps = [
        ":renderer_style_handler",
        "//protocol:renderer_cc_proto",
        "//testing:gunit_main",
    ],
)

# macOS
mozc_macos_application(
    name = "mozc_renderer_macos",
    app_icons = ["//data/images/mac:product_icon.icns"],
    bundle_id = MACOS_BUNDLE_ID_PREFIX + ".Renderer",
    bundle_name = BRANDING + "Renderer",
    infoplists = ["mac/Info.plist"],
    resources = [
        "//data/images/mac:candidate_window_logo.tiff",
    ],
    visibility = [
        "//mac:__pkg__",
        "//renderer:__subpackages__",
    ],
    deps = [":mozc_renderer_main_macos"],
    # When we support Breakpad, uncomment the following block.
    # additional_contents = {
    #     "[Breakpad]" : "Frameworks",
    # },
)

mozc_cc_library(
    name = "mozc_renderer_main_macos",
    srcs = mozc_select(macos = ["mac/mac_renderer_main.cc"]),
    deps = [
        ":init_mozc_renderer",
    ] + mozc_select(
        macos = [":mozc_renderer_lib_macos"],
    ),
)

mozc_objc_library(
    name = "mozc_renderer_lib_macos",
    srcs = [
        "mac/CandidateController.mm",
        "mac/CandidateView.mm",
        "mac/CandidateWindow.mm",
        "mac/InfolistView.mm",
        "mac/InfolistWindow.mm",
        "mac/RendererBaseWindow.mm",
        "mac/mac_server.mm",
        "mac/mac_server_send_command.mm",
        "mac/mac_view_util.mm",
    ],
    hdrs = [
        "mac/CandidateController.h",
        "mac/CandidateView.h",
        "mac/CandidateWindow.h",
        "mac/InfolistView.h",
        "mac/InfolistWindow.h",
        "mac/RendererBaseWindow.h",
        "mac/mac_server.h",
        "mac/mac_server_send_command.h",
        "mac/mac_view_util.h",
    ],
    sdk_frameworks = [
        "Carbon",
        "Cocoa",
        "Foundation",
    ],
    deps = [
        ":renderer_interface",
        ":renderer_server",
        ":renderer_style_handler",
        ":table_layout",
        ":window_util",
        "//base:const",
        "//base:coordinates",
        "//base:util",
        "//base/mac:mac_util",
        "//client:client_interface",
        "//mac:common",
        "//protocol:candidate_window_cc_proto",
        "//protocol:commands_cc_proto",
        "//protocol:renderer_cc_proto",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/synchronization",
    ],
)

mozc_cc_library(
    name = "init_mozc_renderer",
    srcs = ["init_mozc_renderer.cc"],
    hdrs = ["init_mozc_renderer.h"],
    visibility = ["//renderer:__subpackages__"],
    deps = [
        "//base:init_mozc",
        "//base:run_level",
        "//base:system_util",
        "@com_google_absl//absl/flags:flag",
    ],
)
