import axios from 'axios';
import qs from 'qs';
import _ from 'lodash';
import { Encrypt, Decrypt } from './crypto';
import { Toast } from 'vant';
let ToastObj = null;
function getStorageSync(key) {
  const value = window.localStorage.getItem(key);
  if (value && value.indexOf('"') === 0) {
    return JSON.parse(value);
  }
  if (value && value.indexOf('{') > -1) {
    return JSON.parse(value);
  }
  return value || undefined;
}

function setStorageSync(key, value) {
  window.localStorage.setItem(key, JSON.stringify(value));
}

function removeStorageSync(key, value) {
  window.localStorage.removeItem(key);
}

function showLoading(
  { title = '', mask = false, duration = 2000 } = { title: '', mask: false }
) {
  let params = {
    duration,
    message: title,
  };
  if (mask) {
    params = {
      ...params,
      overlay: true,
      forbidClick: true,
    };
  }
  ToastObj = Toast.loading(params);
}

function showModal() {
  console.log('showModal');
}

function hideLoading() {
  ToastObj.clear();
}

function showToast({ title, duration, icon }) {
  const params = {};
  if (title) {
    params.message = title;
  }
  if (duration) {
    params.duration = duration;
  }
  if (icon) {
    params.icon = icon;
  }
  Toast(params);
}

function setNavigationBarTitle({ title }) {
  if (window?.toWXSendMsg) {
    window?.toWXSendMsg({
      type: 'uni_func',
      funcName: 'setNavigationBarTitle',
      options: { title },
    });
  }
}

function navigateTo({ title }) {
  console.log('navigateTo');
}

function login({ title, success }) {
  if (typeof success === 'function') {
    success({});
  }
}

function request({ url, data, method, header, success, fail }) {
  // console.log(87878787, { url, data, method, header, success, fail });
  const params = {
    url,
    method,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      ...header,
    },
  };
  if (method.toLowerCase() === 'get') {
    params.params = data;
  }
  if (/post|put|delete/g.test(method.toLowerCase())) {
    params.data = data;
    // 如果是
    if (/v1\/auth\/phone-login/g.test(url)) {
      params.data = qs.stringify(data);
    }
  }
  if (success) {
    axios(params)
      .then((res) => {
        console.log(101, res);
        if (_.isFunction(success)) {
          success(res);
          return;
        }
        if (_.isFunction(fail)) {
          fail('success is not function');
        }
      })
      .catch((error) => {
        if (_.isFunction(fail)) {
          fail([error, {}]);
        }
      });
    return;
  }
  return axios(params);
}

function requestPayment(options) {
  window.stompClient.send(
    '/sendToWechat',
    {},
    JSON.stringify({
      token: this.$route.params.token,
      data: Encrypt(
        JSON.stringify({
          type: 'openWxPay',
          options,
        })
      ),
    })
  );
}

function getSystemInfoSync() {
  return {
    screenWidth: window.document.documentElement.offsetWidth,
  };
}

export default {
  getStorageSync,
  setStorageSync,
  removeStorageSync,
  showLoading,
  hideLoading,
  showToast,
  showModal,
  navigateTo,
  login,
  request,
  requestPayment,
  setNavigationBarTitle,
  getSystemInfoSync,
};
