/*
 * @Description: 添加水印
 */
"use strict";

let watermark = {};

let setWatermark = (str) => {
  let id = "1.23452384164.123412415";
  let strarr = str.split("@");

  if (document.getElementById(id) !== null) {
    document.body.removeChild(document.getElementById(id));
  }

  let can = document.createElement("canvas");
  can.width = 280;
  can.height = 250;

  let cans = can.getContext("2d");
  cans.rotate((-25 * Math.PI) / 150);
  cans.font = "14px fontFamily";
  // cans.fillStyle = "rgba(88, 88, 88, 0.1)";
  cans.fillStyle = "#f1f0f0"; //"#e1e1e1";
  cans.textAlign = "left";
  cans.textBaseline = "Middle";
  // cans.fillText(str, can.width / 16, can.height);
  cans.globalAlpha = "0.7";
  cans.fillText(strarr[0], can.width / 16, can.height);
  cans.fillText(strarr[1], can.width / 16, can.height + 16);

  let div = document.createElement("div");
  div.id = id;
  div.style.pointerEvents = "none";
  div.style.top = "0px";
  div.style.left = "-80px";
  div.style.position = "fixed";
  div.style.zIndex = "1000000";
  div.style.width = document.documentElement.clientWidth + 80 + "px";
  div.style.height = document.documentElement.clientHeight + "px";
  div.style.background =
    "url(" + can.toDataURL("image/png") + ") left top repeat";
  document.body.appendChild(div);
  return id;
};

// 该方法只允许调用一次
watermark.set = (str) => {
  let id = setWatermark(str);
  setInterval(() => {
    if (document.getElementById(id) === null) {
      id = setWatermark(str);
    }
  }, 500);
  window.onresize = () => {
    setWatermark(str);
  };
};

const outWatermark = (id) => {
  if (document.getElementById(id) !== null) {
    const div = document.getElementById(id);
    div.style.display = "none";
  }
};
watermark.out = () => {
  const str = "1.23452384164.123412415";
  outWatermark(str);
};

export default watermark;
