import message from 'components/message';
import http from '../utils/fetch';
import { api } from '../constants/config';
import { actionTypes } from '../constants/action-types';
import { uploadAssets } from 'utils/upload';

export function getConstants() {
  return async (dispatch) => {
    dispatch({
      type: actionTypes.CONSTANT_GET_LIST_START
    });

    const result = await http.get(api.constantList);

    if (result) {
      dispatch({
        type: actionTypes.CONSTANT_GET_LIST_SUCCESS,
        payload: {
          list: result
        }
      });
      return result;
    } else {
      dispatch({
        type: actionTypes.CONSTANT_GET_LIST_FAILURE
      });
    }

    return result;
  };
}

export function updateConstant({ name, moduleName, category, type, thumb }) {
  return async (dispatch) => {
    let thumbPath = thumb;
    if (typeof thumb === 'object' && thumb !== null) {
      const result = await uploadAssets({
        path: `thumb/${thumb.name}`,
        file: thumb
      });
      if (!result) {
        return;
      }
      thumbPath = result;
    }

    const result = await http.post(api.constantUpdate, {
      name,
      moduleName,
      category,
      type,
      thumb: thumbPath
    });

    if (result) {
      dispatch(getConstants());
    }

    return result;
  };
}

export function sortConstant(orders) {
  return async (dispatch) => {
    const result = await http.post(api.constantSort, orders);

    dispatch(getConstants());

    return result;
  };
}

// 发布或者下架组件
export function publishConstants({ constants, publish }) {
  return async (dispatch) => {
    dispatch({
      type: actionTypes.CONSTANT_LOADING,
      payload: true
    });

    const result = await http.post(api.constantPublish, { constants, publish });
    if (result) {
      if (publish) {
        message.success('发布成功');
      } else {
        message.success('下架成功');
      }

      dispatch(getConstants());
    }

    dispatch({
      type: actionTypes.CONSTANT_LOADING,
      payload: false
    });
  };
}

// 删除组件
export function deleteConstants(constants) {
  return async (dispatch) => {
    dispatch({
      type: actionTypes.CONSTANT_LOADING,
      payload: true
    });
    const result = await http.post(api.constantDelete, { constants });
    if (result) {
      dispatch(getConstants());
      message.success('删除成功');
    }
    dispatch({
      type: actionTypes.CONSTANT_LOADING,
      payload: false
    });
  };
}

export function exportConstants(constants) {
  return async () => {
    const result = await http.post(api.constantExport, {
      constants
    });

    return result;
  };
}

export function importConstants(data) {
  return async () => {
    const result = await http.post(api.constantImport, data);

    return result;
  };
}

const initialState = {
  loading: true,
  list: []
};

export default function reducer(state = initialState, { type, payload }) {
  switch (type) {
    case actionTypes.CONSTANT_GET_LIST_START: {
      return { ...state, loading: true };
    }
    case actionTypes.CONSTANT_GET_LIST_SUCCESS: {
      return { ...state, loading: false, list: payload.list };
    }
    case actionTypes.CONSTANT_GET_LIST_FAILURE: {
      return { ...state, loading: false };
    }
    case actionTypes.CONSTANT_LOADING: {
      return { ...state, loading: payload };
    }
    default:
      return state;
  }
}
