import axios from '../bc-libs/api.request'
import config from "@/config";
import Cookies from 'js-cookie'
import CONSTANT from '../bc-config/constant'

export const login = ({ username, password, secondaryPassword  }) => {
  const data = {
    username,
    password,
    secondaryPassword,
    grant_type:'password',
    client_id:'csbckj2',
    client_secret:'csbckj',
  }
  let url
  if (config.deploymentStandAlone){
    url = 'oauth/token';
  }else{
    url = 'cas/oauth/token';
  }
  return axios.request({
    url: url,
    params: data,
    method: 'post'
  })
}

export const getUserInfo = (token) => {
  let params = {};let url ;
  if (config.deploymentStandAlone){
    url = 'oauth/user/';
  }else if (config.deploymentStructureMerger){
    url = 'api/v1/invokeCas/user/';
  }else {
    url = 'cas/oauth/user/';
  }
  return new Promise((resolve, reject) => {
    return axios.request({
      url: url,
      params: params,
      method: 'get'
    }).then(response => {
      //将正在退出的状态标记为 否 ,这样子如果登录失效,就会触发弹超时提示
      //Cookies.set(CONSTANT.IS_GOING_LOGOUT_KEY, false, 1)
      resolve(response)
    }).catch(err => {
      reject(err)
    })
  })
}

export const getNewToken = async (refresh_token_str) => {
  const data = {
    refresh_token: refresh_token_str,
    grant_type: 'refresh_token',
    client_id: 'csbckj2',
    client_secret: 'csbckj',
  }
  let url
  if (config.deploymentStandAlone) {
    url = 'oauth/token';
  } else {
    url = 'cas/oauth/token';
  }
  return await axios.request({
    url: url,
    params: data,
    method: 'post'
  })
}

export const logout = (token) => {
  let url
  if (config.deploymentStandAlone) {
    url = 'oauth/revokeToken';
  } else {
    url = 'cas/oauth/revokeToken';
  }
  const data = {
    grant_type:'password',
    client_id:'csbckj2',
    client_secret:'csbckj',
  }
  return axios.request({
    url: url,
    data: data,
    method: 'post'
  })
}

export const getUnreadCount = () => {
  return axios.request({
    url: 'api/v1/message/count',
    method: 'get'
  })
}

export const getMessage = () => {
  return axios.request({
    url: 'api/v1/message/init',
    method: 'get'
  })
}

export const getContentByMsgId = msg_id => {
  return axios.request({
    url: 'api/v1/message/content',
    method: 'get',
    params: {
      msg_id
    }
  })
}

export const hasRead = msg_id => {
  return axios.request({
    url: 'api/v1/message/has_read',
    method: 'post',
    data: {
      msg_id
    }
  })
}

export const removeReaded = msg_id => {
  return axios.request({
    url: 'api/v1/message/remove_readed',
    method: 'post',
    data: {
      msg_id
    }
  })
}

export const restoreTrash = msg_id => {
  return axios.request({
    url: 'api/v1/message/restore',
    method: 'post',
    data: {
      msg_id
    }
  })
}
