"use strict";

require("./backend");

require("./svg");

var _Presentation = require("./model/Presentation");

var _Selection = require("./model/Selection");

var _Preferences = require("./model/Preferences");

var _Storage = require("./Storage");

var _Viewport = require("./player/Viewport");

var _Player = require("./player/Player");

var _Controller = require("./Controller");

var _Preview = require("./view/Preview");

var _Properties = require("./view/Properties");

var _Toolbar = require("./view/Toolbar");

var _Timeline = require("./view/Timeline");

var _nunjucks = _interopRequireDefault(require("nunjucks"));

var i18n = _interopRequireWildcard(require("./i18n"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
window.addEventListener("load", () => {
  _nunjucks.default.configure({
    watch: false
  });

  Notification.requestPermission();
  const presentation = new _Presentation.Presentation();
  const selection = new _Selection.Selection(presentation);
  const viewport = new _Viewport.Viewport(presentation, true);
  const player = new _Player.Player(viewport, presentation, true);
  const locale = i18n.init();
  const preferences = new _Preferences.Preferences();
  const controller = new _Controller.Controller(preferences, presentation, selection, viewport, player, locale);
  const preview = new _Preview.Preview(document.getElementById("sozi-editor-view-preview"), presentation, selection, viewport, controller);
  const properties = new _Properties.Properties(document.getElementById("sozi-editor-view-properties"), selection, controller);
  const toolbar = new _Toolbar.Toolbar(document.getElementById("sozi-editor-view-toolbar"), properties, presentation, viewport, controller);
  const timeline = new _Timeline.Timeline(document.getElementById("sozi-editor-view-timeline"), presentation, selection, controller);
  const storage = new _Storage.Storage(controller, presentation, selection);
  const body = document.querySelector("body");
  const left = document.querySelector(".left");
  const right = document.querySelector(".right");
  const top = document.querySelector(".top");
  const bottom = document.querySelector(".bottom");
  const hsplitter = document.querySelector(".hsplitter");
  const vsplitter = document.querySelector(".vsplitter");
  document.getElementById("message").addEventListener("click", evt => {
    controller.hideNotification();
    evt.stopPropagation();
  });
  let hsplitterStartY, vsplitterStartX;
  const hsplitterHeight = hsplitter.getBoundingClientRect().height;
  const vsplitterWidth = vsplitter.getBoundingClientRect().width;

  function hsplitterOnMouseMove(evt) {
    const topHeightPercent = 100 * (hsplitterStartY + evt.clientY) / window.innerHeight;
    top.style.height = topHeightPercent + "%";
    hsplitter.style.top = topHeightPercent + "%";
    bottom.style.height = `calc(${100 - topHeightPercent}% - ${hsplitterHeight}px)`;
    window.dispatchEvent(new UIEvent("resize"));
    return false;
  }

  function hsplitterOnMouseUp() {
    body.removeEventListener("mousemove", hsplitterOnMouseMove);
    body.removeEventListener("mouseup", hsplitterOnMouseUp);
  }

  function vsplitterOnMouseMove(evt) {
    const leftWidthPercent = 100 * (vsplitterStartX + evt.clientX) / window.innerWidth;
    left.style.width = leftWidthPercent + "%";
    vsplitter.style.left = leftWidthPercent + "%";
    right.style.width = `calc(${100 - leftWidthPercent}% - ${vsplitterWidth}px)`;
    window.dispatchEvent(new UIEvent("resize"));
    return false;
  }

  function vsplitterOnMouseUp() {
    body.removeEventListener("mousemove", vsplitterOnMouseMove);
    body.removeEventListener("mouseup", vsplitterOnMouseUp);
  }

  hsplitter.addEventListener("mousedown", evt => {
    hsplitterStartY = hsplitter.getBoundingClientRect().top - evt.clientY;
    body.addEventListener("mousemove", hsplitterOnMouseMove);
    body.addEventListener("mouseup", hsplitterOnMouseUp);
  });
  vsplitter.addEventListener("mousedown", evt => {
    vsplitterStartX = vsplitter.getBoundingClientRect().left - evt.clientX;
    body.addEventListener("mousemove", vsplitterOnMouseMove);
    body.addEventListener("mouseup", vsplitterOnMouseUp);
  });
  window.addEventListener("keydown", evt => {
    let key = "";

    if (evt.ctrlKey) {
      key += "Ctrl+";
    }

    if (evt.altKey) {
      key += "Alt+";
    }

    if (evt.shiftKey) {
      key += "Shift+";
    }

    key += evt.key.toUpperCase();
    let actionFound = null;

    for (let action in preferences.keys) {
      if (preferences.keys[action] === key) {
        actionFound = action;
        break;
      }
    } // If an action was found, validate the active input field
    // before executing the action.


    const inputFocused = document.activeElement && /input|select|textarea|section/i.test(document.activeElement.tagName);

    if (actionFound && inputFocused) {
      document.activeElement.blur();
    }

    switch (actionFound) {
      case "autoselectOutlineElement":
        controller.autoselectOutlineElement();
        break;

      case "resetLayer":
        controller.resetLayer();
        break;

      case "addFrame":
        controller.addFrame();
        break;

      case "save":
        controller.save();
        break;

      case "redo":
        controller.redo();
        break;

      case "undo":
        controller.undo();
        break;

      case "focusTitleField":
        document.getElementById('field-title').select();
        break;

      case "reload":
        controller.reload();
        break;

      case "toggleFullscreen":
        document.getElementById('btn-fullscreen').click();
        break;

      case "toggleDevTools":
        storage.backend.toggleDevTools();
        break;
    } // Keyboard actions that may collide with input fields
    // are executed only if no input element has the focus.


    if (!actionFound && !inputFocused) {
      actionFound = true;

      switch (evt.key) {
        case "End":
          controller.selectFrame(-1);
          break;

        case "Home":
          controller.selectFrame(0);
          break;

        case "ArrowLeft":
          controller.selectRelativeFrame(-1);
          break;

        case "ArrowRight":
          controller.selectRelativeFrame(1);
          break;

        case "Delete":
          controller.deleteFrames();
          break;

        case "a":
          if (evt.ctrlKey) {
            controller.selectAllFrames();
          } else {
            actionFound = false;
          }

          break;

        default:
          actionFound = false;
      }
    }

    if (actionFound) {
      evt.preventDefault();
    }
  }, false);
}, false);
