<script>
  import instApi from "./api/inst";
  import to from "await-to-js";
  const module = uni.requireNativePlugin("leven-arcFace-ArcFaceModule");
  export default {
    onLaunch: function () {
      this.getAllPhoto();
      setInterval(() => {
        this.getAllPhoto();
      }, 60000);
      plus.navigator.hideSystemNavigation();
      plus.screen.lockOrientation("landscape-primary");
      plus.navigator.setFullscreen(true); //隐藏状态栏(应用全屏:只能隐藏状态栏，标题栏和虚拟返回键都还可以显示)
    },
    onShow: function () {
      console.log("App Show");
    },
    onHide: function () {
      console.log("App Hide");
    },
    methods: {
      // 获取本地和线上注册的人脸图片信息
      async getAllPhoto() {
        let onLineFacePhoto = [];
        let localFacePhoto = [];
        // 获取线上所有的人脸图片
        let [err, res] = await to(instApi.getPhoto({}));
        if (err) return;
        if (res.code == 200 && res.data.length > 0) {
          onLineFacePhoto = res.data.map((item) => {
            console.log("头像", this.completionImgPath(item.url));
            return {
              ...item,
              url: this.completionImgPath(item.url),
              name: btoa(encodeURIComponent(this.completionImgPath(item.url))),
            };
          });
          console.log("在线", onLineFacePhoto);
          // 获取注册了的人脸信息
          module.getAllFace((localRes) => {
            localFacePhoto = localRes.code == 0 ? localRes.data.list : [];
            console.log("本地", localFacePhoto);
            // 如果本地注册量0 并且 线上有图片 则直接全部注册
            if (localFacePhoto.length == 0 && onLineFacePhoto.length > 0) {
              this.register(onLineFacePhoto);
            } else if (
              localFacePhoto.length > 0 &&
              onLineFacePhoto.length > 0
            ) {
              this.handleFacePhoto(onLineFacePhoto, localFacePhoto);
            }
          });
        }
      },
      completionImgPath(path) {
        let url = path;
        if (url.indexOf("http") == -1) {
          url = process.uniEnv.VITE_FILE + url;
        }
        return url;
      },
      // 对比在线人脸和本地人脸的数组是否有差异
      diffFacePhoto(onlineArr, localArr) {
        const unregistered = onlineArr.filter(
          (onlineItem) =>
            !localArr.some((localItem) => localItem.id === onlineItem.id)
        );
        console.log("unregistered", unregistered);
        return unregistered;
      },
      // 批量注册
      register(list) {
        console.log("需注册的人脸列表", list);
        if (list.length > 0) {
          module.batchRegister(
            {
              // 同一人是否可以多次注册，默认true
              registerMultiple: false,
              list,
            },
            (res) => {
              console.log("注册信息", res);
            }
          );
        }
      },
      handleFacePhoto(onLineFacePhoto, localFacePhoto) {
        let localArr = localFacePhoto;
        let onlineArr = onLineFacePhoto;
        // 创建两个空数组来存放结果
        let notInLocalArr = [];
        let nameUrlMismatch = [];

        // 将localArr转换为对象以便快速查找
        let localArrMap = new Map();
        localArr.forEach((item) => {
          localArrMap.set(item.id, decodeURIComponent(atob(item.name)));
        });
        // 遍历onlineArr来检查id是否在localArr中不存在，以及name和url是否匹配
        onlineArr.forEach((item) => {
          if (!localArrMap.has(item.id)) {
            notInLocalArr.push(item);
          } else if (localArrMap.get(item.id) !== item.url) {
            nameUrlMismatch.push(item);
          }
        });
        console.log("IDs not in localArr:", notInLocalArr);
        console.log("Name and URL mismatch:", nameUrlMismatch);
        // 如果有需要注册的人脸，则进行注册
        if (notInLocalArr.length > 0) {
          this.register(notInLocalArr);
        }
        // 如果有需要更新的人脸，则进行删除更新
        if (nameUrlMismatch.length > 0) {
          this.updateFace(nameUrlMismatch);
        }
      },
      // 更新人脸(删除后重新注册)
      updateFace(list) {
        console.log("需更新的人脸列表", list);
        if (list.length > 0) {
          list.map((item) => {
            // 先删除
            console.log("item.id", item.id);
            module.getFace(
              {
                id: item.id,
              },
              (res) => {
                console.log("是否存在要删除的人脸", res);
                module.deleteFace(
                  {
                    id: item.id,
                  },
                  (res) => {
                    console.log("删除信息", res);
                    if (res.code == 0) {
                      module.imageFaceRegister(
                        {
                          url: item.url,
                          id: item.id,
                          name: item.name,
                          // 同一人是否可以多次注册，默认true
                          registerMultiple: false,
                        },
                        (res) => {
                          console.log(res);
                        }
                      );
                    }
                  }
                );
              }
            );
          });
        }
      },
    },
  };
</script>

<style lang="scss">
  /* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
  @import "@/uni_modules/uview-ui/index.scss";
  @import "./style/common.scss";
</style>
