function artplayerPluginTouch(option) {
  return (art) => {
    var startx, starty, isLeft, isRight;
    var upDown, leftRight;
    var loaded = false;
    function getAngle(angx, angy) {
      return (Math.atan2(angy, angx) * 180) / Math.PI;
    }
    function getDirection(startx, starty, endx, endy) {
      var angx = endx - startx;
      var angy = endy - starty;
      var result = 0;
      if (Math.abs(angx) < 2 && Math.abs(angy) < 2) {
        return result;
      }
      var angle = getAngle(angx, angy);
      if (angle >= -135 && angle <= -45) {
        result = 1;
      } else if (angle > 45 && angle < 135) {
        result = 2;
      } else if (
        (angle >= 135 && angle <= 180) ||
        (angle >= -180 && angle < -135)
      ) {
        result = 3;
      } else if (angle >= -45 && angle <= 45) {
        result = 4;
      }
      return result;
    }

    //触摸控制layer
    art.layers.add({
      name: "hidden_layer",
      html: `<div><div>`,
      style: {
        display: "block",
        position: "absolute",
        width: "100%",
        height: "100%",
      },
    });

    art.on("ready", () => {
      art.once("play", () => {
        loaded = true;
      });
      art.once("video:playing", () => {
        loaded = true;
      });
    });

    art.layers.hidden_layer.addEventListener(
      "touchstart",
      function (e) {
        if (loaded) {
          //artpalyer组件内有状态干扰 所以延时100
          setTimeout(() => {
            let player = document.getElementsByClassName("art-video-player")[0];
            let utils = Artplayer.utils;
            let has = utils.hasClass(player, "art-hover");
            if (has) {
              art.controls.show = false;
              utils.removeClass(player, "art-hover");
              utils.addClass(player, "art-hide-cursor");
            } else {
              art.controls.show = true;
              utils.removeClass(player, "art-hide-cursor");
              utils.addClass(player, "art-hover");
            }
          }, 100);
        }
        startx = e.touches[0].pageX;
        starty = e.touches[0].pageY;
        isLeft = startx < document.body.clientWidth / 2;
        isRight = !isLeft;
        upDown = false;
        leftRight = false;
      },
      false
    );
    art.layers.hidden_layer.addEventListener(
      "touchmove",
      function (e) {
        if (!art.fullscreenWeb) return;
        var endx, endy;
        endx = e.touches[0].pageX;
        endy = e.touches[0].pageY;
        var direction = getDirection(startx, starty, endx, endy);
        switch (direction) {
          case 0:
            break;
          case 1:
            if (upDown) return;
            leftRight = true;
            touchStart(isLeft);
            touchMove(isLeft, starty - endy);
            break;
          case 2:
            if (upDown) return;
            leftRight = true;
            touchStart(isLeft);
            touchMove(isLeft, starty - endy);
            break;
          case 3:
            if (art.isLock) return;
            if (leftRight) return;
            upDown = true;
            art.backward = 5;
            break;
          case 4:
            if (art.isLock) return;
            if (leftRight) return;
            upDown = true;
            art.forward = 5;
            break;
          default:
            break;
        }
      },
      false
    );

    function touchStart(isLeft) {
      option.volumeShow(isLeft);
    }

    function touchMove(isLeft, ymoved) {
      var height = document.body.clientHeight;
      var rate = Math.abs(ymoved) / height;
      if (ymoved < 0) {
        rate = rate * -1;
      }
      if (rate > 1) {
        rate = 1;
      }
      if (rate < -1) {
        rate = -1;
      }
      option.volumeChange(isLeft, rate)
    }

    art.layers.hidden_layer.addEventListener(
      "touchend",
      function (e) {
        if (!art.fullscreenWeb) return;
        if (!leftRight) return;
        option.volumeHidden();
      },
      false
    );

    return {
      name: "artplayerPluginTouch",
    };
  };
}

if (typeof window !== "undefined") {
  window["artplayerPluginTouch"] = artplayerPluginTouch;
}
