/* eslint-disable */
var StampModule = {
    stampList: null,
    observer: null,
    initialized: false,
    contentDocument: null,
    PDFViewerApplication: null,
    _setScaleObserver: function() {
      var n = this;
      var e =
        window.MutationObserver ||
        window.WebKitMutationObserver ||
        window.MozMutationObserver;
      if (e) {
        var t = function(e) {
          e.forEach(function(e) {
            if (e.type == "attributes") {
              Object.keys(n.stampList).forEach(function(e) {
                var t = n.PDFViewerApplication.pdfViewer.getPageView(
                  n.stampList[e].pageNum - 1
                );
                n._insertStampImg(t, n.stampList[e]);
              });
            }
          });
        };
        n.observer = new e(t);
        var i = n.PDFViewerApplication.pdfViewer.getPageView(0);
        n.observer.observe(i.div, {
          attributes: true,
          attributeFilter: ["style"],
          attributeOldValue: true,
        });
      } else {
        var i = n.PDFViewerApplication.pdfViewer.getPageView(0);
        i.div.onresize = function() {
          Object.keys(n.stampList).forEach(function(e) {
            var t = n.PDFViewerApplication.pdfViewer.getPageView(
              n.stampList[e].pageNum - 1
            );
            n._insertStampImg(t, n.stampList[e]);
          });
        };
        n.observe = true;
      }
    },
    _setScrollObserver: function() {
      var n = this;
      var e = n.contentDocument.getElementById("viewerContainer");
      e.onscroll = onScroll;
      function onScroll() {
        var e = n.PDFViewerApplication.pdfViewer.currentPageNumber;
        Object.keys(n.stampList).forEach(function(e) {
          var t = n.PDFViewerApplication.pdfViewer.getPageView(
            n.stampList[e].pageNum - 1
          );
          n._insertStampImg(t, n.stampList[e]);
        });
      }
    },
    _enableDrag: function(i, a) {
      var o = this;
      var r = 0,
        s = 0;
      var l = 0,
        p = 0;
      var n = i.viewport.scale;
      var e = parseInt(a.style.width, 10);
      var t = parseInt(a.style.height, 10);
      var u = i.pdfPage.pageInfo.view[2];
      var c = i.pdfPage.pageInfo.view[3];
      var v = u - e - 2;
      var d = c - t - 2;
      a.onmousedown = onMouseDown;
      a.addEventListener("touchstart", onMouseDown, false);
      function onMouseDown(e) {
        e = e || window.event;
        e.preventDefault();
        for (var t = 0; t < i.div.children.length; t++) {
          i.div.children[t].classList.add("disable-select");
        }
        r = parseInt(a.style.left, 10);
        s = parseInt(a.style.top, 10);
        if (e.changedTouches !== undefined) {
          var n = e.changedTouches;
          l = n[0].pageX;
          p = n[0].pageY;
        } else {
          l = e.clientX;
          p = e.clientY;
          a.style.pointerEvents = "none";
          a.style.cursor = "auto";
        }
        i.div.onmousemove = onMouseMove;
        i.div.onmouseup = onMouseUp;
        o.contentDocument.onmouseup = onMouseUp;
        document.onmouseup = onMouseUp;
        i.div.addEventListener("touchmove", onMouseMove, false);
        i.div.addEventListener("touchend", onMouseUp, false);
      }
      function onMouseMove(e) {
        e = e || window.event;
        e.preventDefault();
        if (e.changedTouches !== undefined) {
          var t = e.changedTouches;
          r = (t[0].pageX - l) / n + r;
          s = (t[0].pageY - p) / n + s;
          l = t[0].pageX;
          p = t[0].pageY;
        } else {
          r = (e.clientX - l) / n + r;
          s = (e.clientY - p) / n + s;
          l = e.clientX;
          p = e.clientY;
        }
        r = r < 0 ? 0 : r > v ? v : r;
        s = s < 0 ? 0 : s > d ? d : s;
        a.style.left = r + "px";
        a.style.top = s + "px";
        a.style.transformOrigin = 0 - r + "px " + (0 - s) + "px 0px";
        o.stampList[a.id].left = r;
        o.stampList[a.id].top = s;
      }
      function onMouseUp(e) {
        i.div.onmouseup = null;
        i.div.onmousemove = null;
        o.contentDocument.onmouseup = null;
        document.onmouseup = null;
        i.div.removeEventListener("touchmove", onMouseMove, false);
        i.div.removeEventListener("touchend", onMouseUp, false);
        a.style.pointerEvents = "auto";
        for (var t = 0; t < i.div.children.length; t++) {
          i.div.children[t].classList.remove("disable-select");
        }
      }
    },
    _enableDelete: function(n, i) {
      var a = this;
      i.style.pointerEvents = "auto";
      i.onclick = deleteStamp;
      i.addEventListener("touchend", deleteStamp, false);
      function deleteStamp(e) {
        e = e || window.event;
        e.preventDefault();
        i.onclick = null;
        n.remove();
        a._deleteStamp(n.id);
        var t;
        if (typeof window.CustomEvent === "function") {
          t = new CustomEvent("removestamp", {
            bubbles: true,
            cancelable: true,
            detail: { stampId: n.id },
          });
        } else {
          t = a.contentDocument.createEvent("CustomEvent");
          t.initCustomEvent("removestamp", true, true, { stampId: n.id });
        }
        a.contentDocument.dispatchEvent(t);
      }
    },
    /* 
    type : insert 第一次插入签章的时候
    */
    _insertStampImg: function(e, t,type) {
      if (this.contentDocument.getElementById(t.id)) {
        this.contentDocument.getElementById(t.id).remove();
      }
      var n = this.PDFViewerApplication.pdfViewer.currentPageNumber;
      if (e.div.hasAttribute("data-loaded") || Math.abs(t.pageNum - n) <= 5) {
        var i = this.contentDocument.createElement("div");
        i.className = "stamp-img";
        i.id = t.id;
        i.style.left = t.left + "px";
        i.style.top = t.top + "px";
        i.style.transformOrigin = 0 - t.left + "px " + (0 - t.top) + "px 0px";
        i.style.transform =
          "matrix(" +
          e.viewport.scale +
          ", 0, 0, " +
          e.viewport.scale +
          ", 0, 0)";
        i.style.width = t.width + "px";
        i.style.height = t.height + "px";
        i.style.backgroundImage = t.backgroundImage;
        i.style.backgroundSize = t.backgroundSize;
        var a = this.contentDocument.createElement("div");
        a.className = "cross-img";
        a.style.left = t.width - 8 + "px";
        a.style.top = "-8px";
        i.appendChild(a);
        e.div.appendChild(i);
        // 找到当前章的位置
        if(type==='insert'){
          e.annotationLayerFactory.container.scrollTop=i.offsetTop+e.div.offsetTop
        }
        this._enableDrag(e, i,type);
        this._enableDelete(i, a);
      }
      if (null == this.observer) {
        this._setScaleObserver();
        this._setScrollObserver();
      }
    },
    _deleteStamp: function(e) {
      if (undefined != this.stampList[e]) {
        delete this.stampList[e];
        this.stampList.length--;
      }
      if (this.stampList.length <= 0 && null != this.observer) {
        var t =
          window.MutationObserver ||
          window.WebKitMutationObserver ||
          window.MozMutationObserver;
        if (t) {
          this.observer.disconnect();
          this.observer = null;
        } else {
          var n = this.PDFViewerApplication.pdfViewer.getPageView(0);
          n.div.onresize = null;
          this.observer = null;
        }
        var i = this.contentDocument.getElementById("viewerContainer");
        i.onscroll = null;
      }
    },
    _generateUUID: function() {
      var e = [];
      var t = "0123456789abcdef";
      for (var n = 0; n < 36; n++) {
        e[n] = t.substr(Math.floor(Math.random() * 16), 1);
      }
      e[14] = "4";
      e[19] = t.substr((e[19] & 3) | 8, 1);
      e[8] = e[13] = e[18] = e[23] = "-";
      var i = e.join("");
      return i;
    },
    _handleStampEvent: function(e) {
      var t = this;
      switch (e.type) {
        case "removestamp":
          if (null != t.contentDocument.onremovestamp) {
            e.preventDefault();
            t.contentDocument.onremovestamp(e);
          }
          break;
        default:
          break;
      }
    },
    initialize: function stampModuleInitialize(e, t) {
      var n = this;
      n.initialized = true;
      n.contentDocument = e;
      n.stampList = new Array();
      n.PDFViewerApplication = t;
      n.contentDocument.onremovestamp = null;
      n.contentDocument.addEventListener(
        "removestamp",
        n._handleStampEvent.bind(n)
      );
      return n;
    },
    insertStamp: function stampModuleInsertStamp(e, s, t, l, p, n) {
      var u = this;
      var c =
        typeof t === "undefined" || t === 0 ? u.PDFViewerApplication.page : t;
      var v = u.PDFViewerApplication.pdfViewer.getPageView(c - 1);
      var d = "data:image/png;base64," + e;
      var m = new Image();
      m.hidden = true;
      m.onload = function() {
        var e = u._generateUUID();
        var t = s ? s : 96;
        var n = (72 / t) * m.width;
        var i = (72 / t) * m.height;
        var a =
          typeof l == "undefined" ? (v.viewport.viewBox[2] - n) / 2 : l - n / 2;
        var o =
          typeof p == "undefined" ? (v.viewport.viewBox[3] - i) / 2 : p - i / 2;
        var r = new Object();
        r.id = e;
        r.pageNum = c;
        r.left = a;
        r.top = o;
        r.width = n;
        r.height = i;
        r.backgroundImage = "url(" + d + ")";
        r.backgroundSize = r.width + "px" + " " + r.height + "px";
        r.zIndex = 10;
        u.stampList[e] = r;
        u.stampList.length++;
        u._insertStampImg(v, r,'insert');
      };
      m.src = d;
    },
    clearStamps: function stampModuleClearStamps() {
      var t = this.stampList;
      var n = this;
      Object.keys(t).forEach(function(e) {
        if (n.contentDocument.getElementById(t[e].id)) {
          n.contentDocument.getElementById(t[e].id).remove();
        }
        n._deleteStamp(e);
      });
    },
    getStamps: function stampModuleGetStamps() {
      var n = new Array();
      var i = this.stampList;
      Object.keys(i).forEach(function(e) {
        var t = new Object();
        t["class"] = "Annotation";
        t.type = "point";
        t.page = i[e].pageNum;
        t.x = i[e].left + i[e].width / 2;
        t.y = i[e].top + i[e].height / 2;
        n.push(t);
      });
      return n;
    },
  };
  export default StampModule;
  