// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import * as Common from '../../core/common/common.js';
import * as i18n from '../../core/i18n/i18n.js';
import * as UI from '../../ui/legacy/legacy.js';

import type * as Sensors from './sensors.js';

const UIStrings = {
  /**
   * @description Title of the Sensors view. The Sensors view contains GPS, orientation sensors, touch
   * settings, and more.
   */
  sensors: 'Sensors',
  /**
   * @description A tag of the Sensors view that can be searched in the command menu.
   */
  geolocation: 'geolocation',
  /**
   * @description A tag of the Sensors view that can be searched in the command menu.
   */
  timezones: 'timezones',
  /**
   * @description Text in the Sensors view of the Device toolbar.
   */
  locale: 'locale',
  /**
   * @description A tag of the Sensors view that can be searched in the command menu.
   */
  locales: 'locales',
  /**
   * @description A tag of the Sensors view that can be searched in the command menu.
   */
  accelerometer: 'accelerometer',
  /**
   * @description A tag of the Sensors view that can be searched in the command menu. Refers to the
   * orientation of a device (for example, a phone) in 3D space, tilted right or left.
   */
  deviceOrientation: 'device orientation',
  /**
   * @description Title of the Locations settings tab. Refers to geographic locations for GPS.
   */
  locations: 'Locations',
  /**
   * @description Command that opens the Sensors view. The Sensors view contains GPS,
   * orientation sensors, touch settings, and more.
   */
  showSensors: 'Show Sensors',
  /**
   * @description Command that shows the Locations settings tab.
   */
  showLocations: 'Show Locations',
} as const;
const str_ = i18n.i18n.registerUIStrings('panels/sensors/sensors-meta.ts', UIStrings);
const i18nLazyString = i18n.i18n.getLazilyComputedLocalizedString.bind(undefined, str_);

let loadedSensorsModule: (typeof Sensors|undefined);

async function loadEmulationModule(): Promise<typeof Sensors> {
  if (!loadedSensorsModule) {
    loadedSensorsModule = await import('./sensors.js');
  }
  return loadedSensorsModule;
}

UI.ViewManager.registerViewExtension({
  location: UI.ViewManager.ViewLocationValues.DRAWER_VIEW,
  commandPrompt: i18nLazyString(UIStrings.showSensors),
  title: i18nLazyString(UIStrings.sensors),
  id: 'sensors',
  persistence: UI.ViewManager.ViewPersistence.CLOSEABLE,
  order: 100,
  async loadView() {
    const Sensors = await loadEmulationModule();
    return new Sensors.SensorsView.SensorsView();
  },
  tags: [
    i18nLazyString(UIStrings.geolocation),
    i18nLazyString(UIStrings.timezones),
    i18nLazyString(UIStrings.locale),
    i18nLazyString(UIStrings.locales),
    i18nLazyString(UIStrings.accelerometer),
    i18nLazyString(UIStrings.deviceOrientation),
  ],
});

UI.ViewManager.registerViewExtension({
  location: UI.ViewManager.ViewLocationValues.SETTINGS_VIEW,
  id: 'emulation-locations',
  commandPrompt: i18nLazyString(UIStrings.showLocations),
  title: i18nLazyString(UIStrings.locations),
  order: 40,
  async loadView() {
    const Sensors = await loadEmulationModule();
    return new Sensors.LocationsSettingsTab.LocationsSettingsTab();
  },
  settings: [
    'emulation.locations',
  ],
  iconName: 'location-on',
});

Common.Settings.registerSettingExtension({
  storageType: Common.Settings.SettingStorageType.SYNCED,
  settingName: 'emulation.locations',
  settingType: Common.Settings.SettingType.ARRAY,
  // TODO(crbug.com/1136655): http://crrev.com/c/2666426 regressed localization of city titles.
  // These titles should be localized since they are displayed to users.
  defaultValue: [
    {
      title: 'Berlin',
      lat: 52.520007,
      long: 13.404954,
      timezoneId: 'Europe/Berlin',
      locale: 'de-DE',
      accuracy: 150,
    },
    {
      title: 'London',
      lat: 51.507351,
      long: -0.127758,
      timezoneId: 'Europe/London',
      locale: 'en-GB',
      accuracy: 150,
    },
    {
      title: 'Moscow',
      lat: 55.755826,
      long: 37.6173,
      timezoneId: 'Europe/Moscow',
      locale: 'ru-RU',
      accuracy: 150,
    },
    {
      title: 'Mountain View',
      lat: 37.386052,
      long: -122.083851,
      timezoneId: 'America/Los_Angeles',
      locale: 'en-US',
      accuracy: 150,
    },
    {
      title: 'Mumbai',
      lat: 19.075984,
      long: 72.877656,
      timezoneId: 'Asia/Kolkata',
      locale: 'mr-IN',
      accuracy: 150,
    },
    {
      title: 'San Francisco',
      lat: 37.774929,
      long: -122.419416,
      timezoneId: 'America/Los_Angeles',
      locale: 'en-US',
      accuracy: 150,
    },
    {
      title: 'Shanghai',
      lat: 31.230416,
      long: 121.473701,
      timezoneId: 'Asia/Shanghai',
      locale: 'zh-Hans-CN',
      accuracy: 150,
    },
    {
      title: 'São Paulo',
      lat: -23.55052,
      long: -46.633309,
      timezoneId: 'America/Sao_Paulo',
      locale: 'pt-BR',
      accuracy: 150,
    },
    {
      title: 'Tokyo',
      lat: 35.689487,
      long: 139.691706,
      timezoneId: 'Asia/Tokyo',
      locale: 'ja-JP',
      accuracy: 150,
    },
  ],
});
