<template>
  <div class="IndexPage">
    <div class="index-title">
      <div class="title-flower"></div>
    </div>
    <div class="location">
      <div class="building index-location-bj" @click="playFn(1)"></div>
      <div class="building index-location-xa" @click="playFn(2)"></div>
      <div class="building index-location-sh" @click="playFn(3)"></div>
      <div class="building index-location-wh" @click="playFn(4)"></div>
      <div class="building index-location-gz" @click="playFn(5)"></div>
    </div>
    <div class="location-name">
      <div class="place-name bj" @click="playFn(1)"></div>
      <div class="place-name xa" @click="playFn(2)"></div>
      <div class="place-name sh" @click="playFn(3)"></div>
      <div class="place-name wh" @click="playFn(4)"></div>
      <div class="place-name gz" @click="playFn(5)"></div>
    </div>
    <div class="index-line"></div>
    <div class="location-mark">
      <div class="mark mark-bj" :class="isMarkList[0] ? 'is-mark' : ''"></div>
      <div class="mark mark-xa" :class="isMarkList[1] ? 'is-mark' : ''"></div>
      <div class="mark mark-sh" :class="isMarkList[2] ? 'is-mark' : ''"></div>
      <div class="mark mark-wh" :class="isMarkList[3] ? 'is-mark' : ''"></div>
      <div class="mark mark-gz" :class="isMarkList[4] ? 'is-mark' : ''"></div>
    </div>
    <div class="top-bird-1"></div>
    <div class="top-bird-2"></div>
    <div class="wh-cloud"></div>
    <div class="crane-1"></div>
    <div class="crane-2"></div>
    <div class="boat-1"></div>
    <div class="boat-2"></div>
    <div class="boat-3"></div>
    <div class="bottom-bird-1"></div>
    <div class="bottom-cloud"></div>
    <div class="xfl-icon" :class="'xfl-location-' + locationId">
      <div id="frameBox"></div>
    </div>
    <!-- 指引提示 -->
    <div class="click-tips" v-if="locationId == 0"></div>
    <div class="tips-text" v-if="locationId == 0"></div>

  </div>
  <div class="video-popup" @touchmove.prevent>
    <div class="video-box">
      <video class="plyr" id="player" controls>
        <source src="https://cdn.plyr.io/static/our-video.mp4" type="video/mp4">
        <!-- 你可以根据需要添加多个source标签，支持不同的视频格式 -->
      </video>
    </div>
    <div class="cls-btn" @click="hideVideo"></div>
  </div>
  <Line />

</template>

<script setup>
import gsap from 'gsap'
import Plyr from 'plyr';
import 'plyr/dist/plyr.css'; // 导入 Plyr 样式
import ImageFramePlayer from 'image-frame-player';
import {showSuccessToast} from "vant";
import Line from './Line.vue';


const playerRef = ref(null)
const frame = ref(null)
const imageFramePlayer = ref(null)
const locationId = ref(0)
const isMarkList = ref([false, false, false, false, false])
const isDone = ref(false)
const isShowDone = ref(false)

// 视频库
const videoList = [
  new URL(`../assets/video/video.mp4`, import.meta.url).href,
  new URL(`../assets/video/video.mp4`, import.meta.url).href,
  new URL(`../assets/video/video.mp4`, import.meta.url).href,
  new URL(`../assets/video/video.mp4`, import.meta.url).href,
  new URL(`../assets/video/gz.mp4`, import.meta.url).href,
]

// 帧图
const frameList = Array.from({ length: 35 }, (_, index) => {
  return new URL(`../assets/images/xlz/xfl_${index + 1}.png`, import.meta.url).href
});

onMounted(() => {
  animationFn();
  palyFrame()
  playerRef.value = new Plyr('#player', {
    controls: ['play', 'progress', 'current-time', 'fullscreen'],
    fullscreen: {
      enabled: true, // 启用全屏功能
      fallback: true, // 启用全屏回退
      iosNative: true, // 在 iOS 上启用原生全屏
    },
    autoplay: true, // 自动播放
    mute: false,    // 初始不静音
    loop: {        // 循环播放
      active: false
    },
  })

})

// 帧图初始化
const palyFrame = () => {
  imageFramePlayer.value = new ImageFramePlayer({
    dom: document.getElementById("frameBox"),
    imgArr: frameList,
    fps: 25,
    useCanvas: true,
    loop: -1,
    yoyo: true
  });
  imageFramePlayer.value.play();
};



// 播放事件
const playFn = (index) => {

  if (locationId.value === 0 || locationId.value === index) {
    locationId.value = index
    playerRef.value.source = {
      type: 'video',
      sources: [
        {
          src: videoList[locationId.value - 1],
          type: 'video/mp4'
        }
      ]
    };
    gsap.to(".xfl-icon", {
      autoAlpha: 1,
      duration: 0.5,
      onComplete: () => {
        gsap.to(".video-popup", {
          autoAlpha: 1,
          duration: 0.5
        });
      }
    });
  } else {

    gsap.to(".xfl-icon", {
      autoAlpha: 0,
      duration: 0.5,
      onComplete: () => {
        locationId.value = index
        playerRef.value.source = {
          type: 'video',
          sources: [
            {
              src: videoList[locationId.value - 1],
              type: 'video/mp4'
            }
          ]
        };
        gsap.to(".xfl-icon", {
          autoAlpha: 1,
          duration: 0.5,
          onComplete: () => {
            gsap.to(".video-popup", {
              autoAlpha: 1,
              duration: 0.5
            });
          }
        });
      }
    });
  }


  // 标记已经去过的地点
  isMarkList.value[locationId.value - 1] = true

}


// 关闭弹窗
const hideVideo = () => {
  // playerRef.value.fullscreen.enter();
  playerRef.value.stop();
  gsap.to(".video-popup", {
    autoAlpha: 0,
    duration: 0.5
  });

  if(isDone.value && !isShowDone.value){
    showSuccessToast('您已完成所有任务！')
    isShowDone.value = true
  }
}


// 入场动画
const animationFn = () => {


  const tl = gsap.timeline({
    onComplete: () => {
      gsap.to(".title-flower", {
        rotation: 360,
        duration: 4,
        repeat: -1,
        ease: "linear",
      });
      gsap.to(".title-flower", {
        scale: 1.5,
        duration: 4,
        repeat: -1,
        ease: "linear",
        yoyo: true,
      });
      gsap.to(".index-title", {
        y: "-=20",
        duration: 2,
        repeat: -1,
        ease: "linear",
        yoyo: true,
      });

      gsap.to(".place-name", {
        scale: "1.05",
        y: "-=5",
        duration: 2,
        repeat: -1,
        yoyo: true,
        // ease: "back.inOut(1.7)",
      })

      gsap.to(".boat-1,.boat-2", {
        scale: "1.05",
        x: "-=30",
        duration: 4,
        repeat: -1,
        yoyo: true,
        ease: "back.inOut(1.1)",
      })
      gsap.to(".boat-3", {
        scale: "1.05",
        x: "+=30",
        duration: 3,
        repeat: -1,
        yoyo: true,
        ease: "back.inOut(2.9)",
      })
      gsap.to(".crane-1", {
        scale: "1.1",
        y: "-=10",
        duration: 3,
        repeat: -1,
        yoyo: true,
      })
      gsap.to(".crane-2", {
        scale: "1.1",
        y: "-=10",
        x: "-=20",
        duration: 3,
        repeat: -1,
        yoyo: true,
      })
      gsap.to(".click-tips", {
        scale: ".7",
        x: "-=10",
        duration: 2,
        repeat: -1,
        yoyo: true,
        ease: "bounce.out()",
      })
    }
  });
  tl.from(".IndexPage", {
    autoAlpha: 0,
    scale: 1.2,
    duration: 0.75,
    ease: "power2.out",
    onComplete: () => {

    }
  })
    .from(".index-title", {
      autoAlpha: 0,
      y: 20,
      duration: 1,
      ease: "power1.out",
    })
    .from(".bj,.index-location-bj", {
      autoAlpha: 0,
      y: -20,
      duration: 1,
      ease: "back.inOut(.3)",
    }, 0.3)
    .from(".xa,.index-location-xa", {
      autoAlpha: 0,
      y: -20,
      duration: 1,
      ease: "back.inOut(3)",
    }, 0.5)
    .from(".sh,.index-location-sh", {
      autoAlpha: 0,
      y: -20,
      duration: 1,
      ease: "back.inOut(3)",
    }, 0.7)
    .from(".wh,.index-location-wh", {
      autoAlpha: 0,
      y: -20,
      duration: 1,
      ease: "back.inOut(3)",
    }, 1)
    .from(".gz,.index-location-gz", {
      autoAlpha: 0,
      y: -20,
      duration: 1,
      ease: "back.inOut(3)",
    }, 1.2)
    .from(".index-line,.location-mark", {
      autoAlpha: 0,
      duration: 1,
      ease: "back.inOut(3)",
    }, 1.5)
    .from(".click-tips,.tips-text", {
      autoAlpha: 0,
      x: 20,
      duration: 1,
      ease: "back.inOut(3)",
    })




}

</script>


<style lang="scss" scoped>
.IndexPage {
  @include bgSrc("index/bg.jpg");
  @include box(750px, 1929px);
  overflow: auto;
  position: relative;

  .index-title {
    @include bgSrc("index/title.png");
    @include pos(502px, 299px, 114px, 44px);
    position: relative;
    pointer-events: none;
    overflow: hidden;

    .title-flower {
      @include bgSrc("index/title-flower.png");
      @include pos(58px, 56px, 246px, 79px);
    }
  }

  .index-line {
    @include bgSrc("index/line.png");
    @include pos(348px, 1048px, 170px, 568px);
    pointer-events: none;
  }

  .wh-cloud {
    @include bgSrc("index/wh-cloud.png");
    @include pos(324px, 298px, 9px, 1061px);
    pointer-events: none;
  }

  .location {
    .index-location-bj {
      @include bgSrc("index/location-bj.png");
      @include pos(507px, 233px, 142px, 341px);
    }

    .index-location-xa {
      @include bgSrc("index/location-xa.png");
      @include pos(303px, 319px, 36px, 647px);
    }

    .index-location-sh {
      @include bgSrc("index/location-sh.png");
      @include pos(378px, 413px, 372px, 760px);
    }

    .index-location-wh {
      @include bgSrc("index/location-wh.png");
      @include pos(192px, 229px, 94px, 1067px);
    }

    .index-location-gz {
      @include bgSrc("index/location-gz.png");
      @include pos(750px, 507px, 0px, 1402px);
    }
  }

  .location-name {
    .bj {
      @include bgSrc("index/name-bj.png");
      @include pos(98px, 65px, 161px, 404px);
    }

    .xa {
      @include bgSrc("index/name-xa.png");
      @include pos(98px, 65px, 190px, 658px);
    }

    .sh {
      @include bgSrc("index/name-sh.png");
      @include pos(98px, 65px, 610px, 797px);
    }

    .wh {
      @include bgSrc("index/name-wh.png");
      @include pos(98px, 66px, 266px, 1061px);
    }

    .gz {
      @include bgSrc("index/name-gz.png");
      @include pos(98px, 66px, 499px, 1389px);
    }
  }

  .location-mark {
    .mark {
      @include bgSrc("index/sign-icon.png");
      // 灰色滤镜
      filter: grayscale(100%);
    }

    .mark-bj {
      @include pos(73px, 75px, 466px, 540px);
    }

    .mark-xa {
      @include pos(73px, 75px, 291px, 731px);
    }

    .mark-sh {
      @include pos(73px, 75px, 393px, 976px);
    }

    .mark-wh {
      @include pos(73px, 75px, 152px, 1348px);
    }

    .mark-gz {
      @include pos(73px, 75px, 150px, 1558px);
    }

    .is-mark {
      filter: grayscale(0%);
    }
  }

  .top-bird-1 {
    @include bgSrc("index/top-bird-1.png");
    @include pos(112px, 46px, 559px, 117px);
    pointer-events: none;
  }

  .top-bird-2 {
    @include bgSrc("index/top-bird-2.png");
    @include pos(102px, 42px, 553px, 408px);
    pointer-events: none;
  }

  .crane-1 {
    @include bgSrc("index/crane-1.png");
    @include pos(80px, 116px, 44px, 1147px);
    pointer-events: none;
  }

  .crane-2 {
    @include bgSrc("index/crane-2.png");
    @include pos(117px, 112px, 197px, 1210px);
    pointer-events: none;
  }

  .boat-1 {
    @include bgSrc("index/boat.png");
    @include pos(181px, 57px, 567px, 1156px);
    pointer-events: none;
  }

  .boat-2 {
    @include bgSrc("index/boat-2.png");
    @include pos(186px, 62px, 125px, 1749px);
    pointer-events: none;
  }

  .boat-3 {
    @include bgSrc("index/boat.png");
    @include pos(197px, 49px, 438px, 1834px);
    pointer-events: none;
  }

  .bottom-bird-1 {
    @include bgSrc("index/bottom-bird-1.png");
    @include pos(89px, 42px, 324px, 1797px);
    pointer-events: none;
  }

  .bottom-cloud {
    @include bgSrc("index/cloud-bottom.png");
    @include pos(749px, 371px, 0px, 1558px);
    pointer-events: none;
  }

  .xfl-icon {
    pointer-events: none;
    position: absolute;
    width: 163px;
    height: 153px;
    // 以中心放大1.5倍
    transform: scale(1.5);

    #frameBox {
      width: 100%;
      height: 100%;
    }
  }

  .xfl-location-0 {
    visibility: hidden;
  }

  .xfl-location-1 {
    left: 421px;
    top: 512px;
  }

  .xfl-location-2 {
    left: 245px;
    top: 679px;
  }

  .xfl-location-3 {
    left: 347px;
    top: 929px;
    transform: scaleX(-1) scale(1.5);

  }

  .xfl-location-4 {
    left: 104px;
    top: 1305px;
  }

  .xfl-location-5 {
    left: 104px;
    top: 1513px;
    transform: scaleX(-1) scale(1.5);
  }

  .click-tips {
    @include pos(58px, 71px, 556px, 500px);
    @include bgSrc("index/arrow-icon.png");
    pointer-events: none;
  }

  .tips-text {
    @include pos(163px, 92px, 586px, 560px);
    @include bgSrc("index/tips.png");
    pointer-events: none;
  }
}

.video-popup {
  @include fixed();
  @include flexCen();
  background-color: rgba($color: #000000, $alpha: .6);
  visibility: hidden;

  .cls-btn {
    @include box(50px, 50px);
    @include bgSrc("index/cls-btn.png");
    margin-top: 40px;
  }

  .video-box {
    width: 700px;
    border-radius: 12px;
    border: 1px solid #fff;
    overflow: hidden;

    .plyr {
      @include box(100%, 100%);
    }
  }
}


</style>
