
import axios from 'axios';
import cookie from 'react-cookies'

const setCookier = () => {
  const _params = window.location.search
  if (_params) {
    let _search = _params.split('?')[1];
    _search.split('&').forEach(item => {
      const _arr = item.split('=');
      cookie.remove(_arr[0], {
        path: '/',
        domain: '.educoder.net'
      });
      cookie.save(_arr[0], _arr[1], { domain: '.educoder.net', path: '/' })
    });
  }
}

// 获取代码块
export function fetchWxCode(identifier, params) {
  setCookier();
  const url = `/tasks/${identifier}/rep_content.json`;
  params = Object.assign({}, params, { withCredentials: true });
  return axios.get(url, { params });
}

// 获取测试值
export function fetchWxCodeTextCase(identifier) {
  setCookier();
  const url = `/tasks/${identifier}.json`;
  const params = Object.assign({}, { withCredentials: true });
  return axios.get(url, { params });
}

// 更新代码块内容
export function UpdateWxCode(identifier, params) {
  setCookier();
  const url = `/myshixuns/${identifier}/update_file.json`;
  params = Object.assign({}, params, { withCredentials: true });
  return axios.post(url, params);
}

// 恢复初始化
export function restoreWxCode(identifier, params) {
  setCookier();
  const url = `/tasks/${identifier}/reset_original_code.json`;
  params = Object.assign({}, params, { withCredentials: true });
  return axios.get(url, { params });
}
// 评测
export function fetchWxCodeGameBuild(identifier, params) {
  setCookier();
  const url = `/tasks/${identifier}/game_build.json`;
  params = Object.assign({}, params, { withCredentials: true });
  return axios.get(url, { params });
}

export function fetchWxCodeGameStatus(identifier, params) {
  setCookier();
  const url = `/tasks/${identifier}/game_status.json`;
  params = Object.assign({}, params, { withCredentials: true });
  return axios.get(url, { params });
}
