import "@/utils/electronLog";
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "./registerServiceWorker";

import "./plugins/iview.js";
import "./mixins/mixins.js";
import "./styles/global.css";

// import "./plugins/tracking.js";
// import "./plugins/face.js";
// import "qm-tracking/tracking.js";
// import "qm-tracking/face.js";

import axiosPlugin from "./utils/axios";
import "./utils/axiosRetry";
import "./utils/monitors";
import "webrtc-adapter";

import MainLayout from "@/components/MainLayout/MainLayout.vue";

Vue.use(axiosPlugin);

Vue.config.productionTip = process.env.NODE_ENV !== "production";

Vue.component("main-layout", MainLayout);

if (process.env.NODE_ENV !== "production") {
  // 测试vue cli打包的环境变量
  console.log("环境检查: process.env.NODE_ENV === " + process.env.NODE_ENV);
}

if (process.env.NODE_ENV === "development") {
  console.log("非生产环境：准备自动登录");
  (async () => {
    if (window.sessionStorage.getItem("token")) {
      console.log("非生产环境: 已有token，自动登录");
      return;
    }
    console.log("非生产环境: 没有token，自动登录");

    const response = await fetch("/api/ecs_core/auth/login", {
      method: "POST",
      headers: {
        "Content-Type": "application/json;charset=UTF-8",
      },
      body: JSON.stringify({
        domain: process.env.VUE_APP_LOGIN_DOMAIN,
        accountValue: process.env.VUE_APP_LOGIN_ACCOUNT_VALUE,
        password: process.env.VUE_APP_LOGIN_PASSWORD,
        accountType: process.env.VUE_APP_LOGIN_ACCOUNTTYPE,
      }),
    });
    let data = await response.json();
    if (data.token) {
      window.sessionStorage.setItem("token", data.token);
      window.localStorage.setItem("key", data.key);
      window.localStorage.setItem("domain", process.env.VUE_APP_LOGIN_DOMAIN);

      const student = await (await fetch(
        "/api/ecs_core/student/getStudentInfoBySession",
        {
          method: "GET",
          headers: {
            "Content-Type": "application/json;charset=UTF-8",
            key: data.key,
            token: data.token,
          },
        }
      )).json();
      const user = { ...data, ...student };

      window.localStorage.setItem("user-for-reload", JSON.stringify(user));
    } else {
      console.log(data.desc);
    }
  })();
}

if (
  process.env.NODE_ENV === "development" &&
  process.env.VUE_APP_ENABLE_VUE_RENDER_LOGS === "true"
) {
  require("./plugins/vueLifecylceLogs");
}

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount("#app");
