import Vue from 'vue'
import {
  ACCESS_TOKEN,
  INDEX_MAIN_PAGE_PATH,
  UI_CACHE_DB_DICT_DATA,
  USER_INFO,
  USER_NAME
} from "@/store/mutation-types"
import store from '@/store'
import {
  getCasToken
} from '@api/login'
import {
  welcome
} from '@/utils/util'
import {
  initWithUrl
} from '@/WsMgr'
/**
 * 单点登录
 */
const init = async (callback) => {
  if (process.env.VUE_APP_SSO == 'true') {
    console.log("-------单点登录开始-------")
    if (process.env.VUE_APP_IFRAME_CHECK === 'TRUE') {
      await getCasToken().then((response) => {
        if (response.code == '200') {
          const result = response.result
          const userInfo = result.userInfo
          Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
          Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
          Vue.ls.set(USER_INFO, Object.assign(userInfo, {
            ejdw: result.ejdw
          }), 7 * 24 * 60 * 60 * 1000)
          Vue.ls.set(UI_CACHE_DB_DICT_DATA, result.sysAllDictItems, 7 * 24 * 60 * 60 * 1000)
          store.commit('SET_TOKEN', result.token)
          store.commit('SET_INFO', userInfo)
          store.commit('SET_NAME', {
            username: userInfo.username,
            realname: userInfo.realname,
            welcome: welcome()
          })
          store.commit('SET_AVATAR', userInfo.avatar)
          initWithUrl(process.env.VUE_APP_WSS_URI + "?token=" + result.token)
          loginSuccess(callback)
        } else {
          if (process.env.NODE_ENV === 'development') {
            gotoCas()
          } else {
            window.location.href = process.env.VUE_APP_API_BASE_URL + "sys/cas/client/redirect"
            //window.location.href = "localhost:8000/" + "sys/cas/client/redirect"
            //process.env.VUE_APP_API_BASE_URL + "sys/cas/client/redirect"
          }
        }
      }).catch(e => {
        gotoCas()
      })
    }
    console.log("-------单点登录结束-------")
  } else {
    callback && callback()
  }
}

function gotoCas() {
  let service = process.env.VUE_APP_API_BASE_URL + "sys/cas/client/validateLogin"
  let serviceUrl = encodeURIComponent(service)
  window.location.href = process.env.VUE_APP_CAS_BASE_URL + '/login?service=' + serviceUrl
}
const SSO = {
  init: init
}

function getUrlParam(paraName) {
  let url = document.location.toString()
  let arrObj = url.split("?")

  if (arrObj.length > 1) {
    let arrPara = arrObj[1].split("&")
    let arr

    for (let i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split("=")

      if (arr != null && arr[0] == paraName) {
        return arr[1]
      }
    }
    return ""
  } else {
    return ""
  }
}

function validateSt(ticket, service, callback) {
  let params = {
    ticket: ticket,
    service: service
  }
  store.dispatch('ValidateLogin', params).then(res => {
    //this.departConfirm(res)
    if (res.success) {
      loginSuccess(callback)
    } else {
      let sevice = "http://" + window.location.host + "/"
      let serviceUrl = encodeURIComponent(sevice)
      window.location.href = window._CONFIG['casPrefixUrl'] + "/login?service=" + serviceUrl
    }
  }).catch((err) => {
    console.log(err)
    //that.requestFailed(err);
  })
}

function loginSuccess(callback) {
  callback()
}
export default SSO