// index.js
// 获取应用实例
import { VueLikePage } from "../../utils/page";
import { CDN_URL, API_BASE_URL, VIDEO_BASE_URL, app } from "../../config/index";
import Router from "../../utils/routes";
let socket_io = require("../../utils/socket.io-mp.js");
var g_app = getApp();

VueLikePage([], {
  data: {
    cdn_url: CDN_URL,
    rndWord: "",
    showModal: false,
    type:0
  },
  methods: {
    onLoad: function () {},

    disconnect() {
      app.websocket.disconnect();
      app.websocket = null;
    },

    wish_put: function (e) {
      this.setData({
        rndWord: e.detail.value,
      });
    },

    back() {
      this.setData({
        showModal: false,
      });
    },

    ok() {
      let link = "",
        m_type = "";

      if (this.data.rndWord.substring(0,3) == "pic") {
        this.setData({
          type:1
        })
        link = `${VIDEO_BASE_URL}4dpic/${this.data.rndWord}.jpg`;
        m_type = "jpeg";
      } else {
        link = `${VIDEO_BASE_URL}4dvedio/${this.data.rndWord}.mp4`;
        m_type = "video";
        this.setData({
          type:0
        })
      }

      console.log(link);

      wx.downloadFile({
        url: link,
        success: (res) => {
          console.log(res);
          if (res.statusCode == '404') {
            return app.showAlert("作品暂未生成，请稍后再试");
          }
          //判断是否为数组
          let typeType =
            Object.prototype.toString.call(res.header["Content-Type"]) == "[object String]" ? res.header["Content-Type"] : res.header["Content-Type"][0];


          //判断不是xml文件
          if (typeType.indexOf(m_type) > -1) {

            g_app.globalData.m_file = res.tempFilePath;
            this.gotoWork();
          }
        },
        fail: () => {
          app.showAlert("作品暂未生成，请稍后再试");
        },
      });
      this.setData({
        showModal: false,
      });
    },

    gotoWork() {
      Router.push({
        url: "work",
        query: {
          type: this.data.type,
          id: this.data.currentScene,
          rdw: encodeURIComponent(this.data.rndWord),
        },
      });
    },

    getWork() {
      this.setData({
        showModal: true,
      });
      // Router.push({
      //   url: "work",
      // });
    },

    onShow(){
      if (app.websocket) {
        this.disconnect();
      }
    },

    checkTextDetail: function () {
      wx.showModal({
        title: "作品标识码",
        content: this.data.rndWord,
        showCancel: true,
        confirmText: "复制文本",
        success: (result) => {
          if (result.confirm) {
            wx.setClipboardData({
              data: this.data.rndWord,
              success: () => {
                wx.getClipboardData({
                  success: (data) => {
                    console.log(data);
                  },
                });
              },
            });
          }
        },
      });
    },

    bindStart: function () {
      if (app.websocket) {
        return;
      }

      app.websocket = socket_io(API_BASE_URL, {
        transports: ["websocket"],
      });

      app.websocket.on("connect", () => {
        console.log("SOCKET连接成功");
        app.emitEvent.request((data) => {
          app.websocket.on("ForceOffline", () => {
            console.log("ForceOffline成功");
            app.showAlert("服务器连接失败，请稍后再试", () => {
              wx.reLaunch({
                url: "start",
              });
            });
            this.disconnect();
          });

          app.websocket.on("error", () => {
            this.disconnect();
            app.showAlert("服务器异常，请稍后再试");
          });


          if (data == 808) {
            return app.showAlert("机器被占用，请稍后再试");
          }

          console.log(data,4545454);
          try {
            var json = JSON.parse(data);
            g_app.globalData.scenes = [...json.scenes];
            if (g_app.globalData.scenes.length > 0) {
              Router.push({
                url: "index",
              });
            } else {
              app.showAlert("暂无场景");
            }
          } catch (error) {
            this.disconnect();
            app.showAlert("网络异常，请稍后再试");
          }
        });
      });
    },
  },
});
