# Copyright 2019 Google LLC.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("skia.gni")

if (is_ios) {
  # Template to compile .xib and .storyboard files.
  #
  # Arguments
  #
  #     sources:
  #         list of string, sources to compile
  #
  #     ibtool_flags:
  #         (optional) list of string, additional flags to pass to the ibtool
  template("compile_ib_files") {
    action_foreach(target_name) {
      forward_variables_from(invoker,
                             [
                               "testonly",
                               "visibility",
                             ])
      assert(defined(invoker.sources),
             "sources must be specified for $target_name")
      assert(defined(invoker.output_extension),
             "output_extension must be specified for $target_name")

      ibtool_flags = []
      if (defined(invoker.ibtool_flags)) {
        ibtool_flags = invoker.ibtool_flags
      }

      _output_extension = invoker.output_extension

      script = "//gn/compile_ib_files.py"
      sources = invoker.sources
      outputs = [
        "$target_gen_dir/$target_name/{{source_name_part}}.$_output_extension",
      ]
      args = [
        "--input",
        "{{source}}",
        "--output",
        rebase_path(
            "$target_gen_dir/$target_name/{{source_name_part}}.$_output_extension",
            root_build_dir),
      ]

      #    if (!use_system_xcode) {
      #      args += [
      #        "--developer_dir",
      #        hermetic_xcode_path,
      #      ]
      #    }
      args += ibtool_flags
    }
  }

  template("bundle_data_ib_file") {
    assert(defined(invoker.source),
           "source needs to be defined for $target_name")

    _source_extension = get_path_info(invoker.source, "extension")
    assert(_source_extension == "xib" || _source_extension == "storyboard",
           "source must be a .xib or .storyboard for $target_name")

    _target_name = target_name
    if (_source_extension == "xib") {
      _compile_ib_file = target_name + "_compile_xib"
      _output_extension = "nib"
    } else {
      _compile_ib_file = target_name + "_compile_storyboard"
      _output_extension = "storyboardc"
    }

    compile_ib_files(_compile_ib_file) {
      sources = [ invoker.source ]
      output_extension = _output_extension
      visibility = [ ":$_target_name" ]
      ibtool_flags = [
        #        "--minimum-deployment-target",
        #        ios_deployment_target,
        "--auto-activate-custom-fonts",
        "--target-device",
        "iphone",
        "--target-device",
        "ipad",
      ]
    }

    bundle_data(_target_name) {
      forward_variables_from(invoker, "*", [ "source" ])

      if (!defined(public_deps)) {
        public_deps = []
      }
      public_deps += [ ":$_compile_ib_file" ]

      sources = get_target_outputs(":$_compile_ib_file")

      outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
    }
  }

  template("ios_app_bundle") {
    app_name = target_name
    gen_path = target_gen_dir
    bundle_prefix = "com.google"
    plist_string = string_join(
            "$0x0A",
            [
              "<plist version=\"1.0\">",
              "  <dict>",
              "    <key>CFBundleVersion</key> <string>0.1.0</string>",
              "    <key>CFBundleShortVersionString</key> <string>0.1.0</string>",
              "    <key>CFBundleName</key> <string>${app_name}</string>",
              "    <key>CFBundleExecutable</key> <string>${app_name}</string>",
              "    <key>CFBundleIdentifier</key> <string>${bundle_prefix}.${app_name}</string>",
              "    <key>CFBundlePackageType</key> <string>APPL</string>",
              "    <key>LSRequiresIPhoneOS</key> <true/>",
              "    <key>UILaunchStoryboardName</key> <string>LaunchScreen</string>",
              "  </dict>",
              "</plist>",
            ])

    write_file("$gen_path/${app_name}_Info.plist", plist_string)

    bundle_data("${app_name}_bundle_info_plist") {
      sources = [ "$gen_path/${app_name}_Info.plist" ]
      outputs = [ "{{bundle_resources_dir}}/Info.plist" ]
    }

    if (defined(invoker.data_sources)) {
      bundle_data("${app_name}_bundle_resources_and_skps") {
        sources = invoker.data_sources

        # iOS reserves the folders 'Resources' and 'resources' so store one level deeper
        outputs = [ "{{bundle_resources_dir}}/data/{{source_file_part}}" ]
      }
    }

    if (defined(invoker.launchscreen)) {
      bundle_data_ib_file("${app_name}_bundle_launchscreen") {
        source = invoker.launchscreen
      }
    }

    executable("${app_name}_generate_executable") {
      if (!defined(configs)) {
        configs = []
      }
      forward_variables_from(invoker,
                             "*",
                             [
                               "output_name",
                               "visibility",
                               "is_shared_library",
                               "data_sources",
                               "extra_configs",
                               "configs",
                             ])
      if (defined(invoker.configs)) {
        configs += invoker.configs
      }
      if (defined(invoker.extra_configs)) {
        configs += invoker.extra_configs
      }
      output_name = rebase_path("$gen_path/$app_name", root_build_dir)
    }

    action("${app_name}_dsymutil") {
      public_deps = [ ":${app_name}_generate_executable" ]
      sources = [ "$gen_path/$app_name" ]
      script = "//gn/call.py"
      args = [
        "dsymutil",
        rebase_path("$gen_path/$app_name"),
      ]
      outputs = [ "$gen_path/${app_name}.dSYM" ]
      testonly = defined(invoker.testonly) && invoker.testonly
      pool = "//gn/toolchain:dsymutil_pool($default_toolchain)"
    }

    bundle_data("${app_name}_bundle_executable_and_symbols") {
      public_deps = [
        ":${app_name}_dsymutil",
        ":${app_name}_generate_executable",
      ]
      sources = [
        "$gen_path/${app_name}",
        "$gen_path/${app_name}.dSYM",
      ]
      outputs = [ "{{bundle_executable_dir}}/{{source_file_part}}" ]
      testonly = defined(invoker.testonly) && invoker.testonly
    }

    create_bundle("$app_name") {
      product_type = "com.apple.product-type.application"
      testonly = defined(invoker.testonly) && invoker.testonly

      bundle_root_dir = "${root_build_dir}/${target_name}.app"
      bundle_resources_dir = bundle_root_dir
      bundle_executable_dir = bundle_root_dir

      xcode_extra_attributes = {
        PRODUCT_BUNDLE_IDENTIFIER = "${bundle_prefix}.${app_name}"
        if (ios_min_target != "") {
          IPHONEOS_DEPLOYMENT_TARGET = ios_min_target
        }
        INFOPLIST_FILE = rebase_path("${target_gen_dir}/${app_name}_Info.plist")
      }

      deps = [
        ":${app_name}_bundle_executable_and_symbols",
        ":${app_name}_bundle_info_plist",
      ]
      if (defined(invoker.launchscreen)) {
        deps += [ ":${app_name}_bundle_launchscreen" ]
      }
      if (defined(invoker.data_sources)) {
        deps += [ ":${app_name}_bundle_resources_and_skps" ]
      }

      if (skia_ios_use_signing) {
        post_processing_script = "//gn/codesign_ios.py"
        post_processing_sources = [ "$target_gen_dir/$app_name" ]
        post_processing_outputs = [
          "$bundle_root_dir/_CodeSignature/CodeResources",
          "$bundle_root_dir/embedded.mobileprovision",
        ]
        post_processing_args = [
          rebase_path("$bundle_root_dir", root_build_dir),
          skia_ios_identity,
          skia_ios_profile,
        ]
      }
    }
  }
}