import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import "element-ui/lib/theme-chalk/index.css";
import "./assets/css/element-variables.scss";
import "./assets/css/reset.css";
import "./assets/css/common.scss";
import echarts from "./utils/echartsUi";
import Keycloak from "./utils/keycloak/keycloak";
import Fingerprint2 from 'fingerprintjs2';

import {
  Menu,
  Submenu,
  MenuItem,
  Table,
  TableColumn,
  Input,
  Button,
  Pagination,
  DatePicker,
  Row,
  Col,
  Select,
  Option,
  Dialog,
  Message,
  MessageBox,
  Tabs,
  TabPane,
  Tooltip,
  Upload,
  Checkbox,
  CheckboxGroup,
  Popover,
  Tag,
  Popconfirm,
  Alert,
  Form,
  FormItem,
  Autocomplete,
  Switch,
  RadioGroup,
  RadioButton,
  Radio,
  Breadcrumb,
  BreadcrumbItem,
} from "element-ui";

import lang from 'element-ui/lib/locale/lang/en'
import locale from 'element-ui/lib/locale'
import { encodeTokenToBase64 } from "./utils/utils.js"
import {invalidToken} from "@/api/permission";
const sha256 = require('js-sha256');

import * as filters from "./utils/filters.js";
Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key]); //插入过滤器名和对应方法
});
locale.use(lang);
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Tabs);
Vue.use(TabPane);
Vue.use(Dialog);
Vue.use(Menu);
Vue.use(Submenu);
Vue.use(MenuItem);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(Input);
Vue.use(Button);
Vue.use(Pagination);
Vue.use(DatePicker);
Vue.use(Row);
Vue.use(Col);
Vue.use(Select);
Vue.use(Option);
Vue.use(Tooltip);
Vue.use(Upload);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(Popover);
Vue.use(Tag);
Vue.use(Popconfirm);
Vue.use(Alert);
Vue.use(Autocomplete);
Vue.use(Switch);
Vue.use(RadioGroup);
Vue.use(RadioButton);
Vue.use(Radio);
Vue.use(Breadcrumb);
Vue.use(BreadcrumbItem);
Vue.config.productionTip = false;
Vue.prototype.$message = Message;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$echarts = echarts;

// keycloak init options
const initOptions = {
  url: process.env.VUE_APP_KEYCLOAK_AUTH_SERVER_URL,
  realm: process.env.VUE_APP_KEYCLOAK_REALM || '',
  clientId: process.env.VUE_APP_KEYCLOAK_CLIENT_ID || '',
  onLoad: "login-required",
};

// const initOptions = {
//   url: 'http://139.217.94.179:8080/auth',
//   realm: 'demo',
//   clientId: 'vue-demo',
//   onLoad:'login-required'
// }

// new Vue({
//   router,
//   render: h => h(App)
// }).$mount('#app')

// 创建一个新的 Fingerprint2 实例
Fingerprint2.get((result, components) => {
  console.log('Fingerprint',result)
  let fingerprint = sha256(JSON.stringify(result))
  sessionStorage.setItem("deviceId", fingerprint);
});
const keycloak = Keycloak(initOptions);
keycloak
  .init({
    onLoad: initOptions.onLoad,
    pkceMethod: "S256",
    promiseType: "native",
  })
  .then((authenticated) => {
    if (!authenticated) {
      window.location.reload();
    } else {
      console.log(keycloak, '=======')
      Vue.prototype.$keycloak = keycloak;
      router['$keycloak'] = keycloak
      let token = encodeTokenToBase64("Bearer " + keycloak.token);
      sessionStorage.setItem("token", token);
      setInterval(async () => {
        keycloak.updateToken(-1).then(async () => {
          console.log(keycloak.token, '=========')
          let token = encodeTokenToBase64("Bearer " + keycloak.token);
          let beforeToken = sessionStorage.getItem("token")
          sessionStorage.setItem("token", token);
          invalidToken(beforeToken);
        })
      }, 60 * 1000)
      // setTimeout(() => {
      new Vue({
        router,
        render: (h) => h(App),
      }).$mount("#app");
      // }, 2000);
    }
  })
  .catch((error) => {
    console.log("Authenticated Failed", error);
  });
