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

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

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

    if (result) {
      dispatch({
        type: actionTypes.MOBILE_CONSTANT_GET_LIST_SUCCESS,
        payload: {
          list: result
        }
      });
    } else {
      dispatch({
        type: actionTypes.MOBILE_CONSTANT_GET_LIST_FAILURE
      });
    }

    return result;
  };
}

export function updateMobileConstant({ name, moduleName, category, 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.mobileConstantUpdate, {
      name,
      moduleName,
      category,
      thumb: thumbPath
    });

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

    return result;
  };
}

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

    dispatch(getMobileConstants());

    return result;
  };
}

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

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

      dispatch(getMobileConstants());
    }

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

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

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

    return result;
  };
}

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

    return result;
  };
}

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

export default function reducer(state = initialState, { type, payload }) {
  switch (type) {
    case actionTypes.MOBILE_CONSTANT_GET_LIST_START: {
      return { ...state, loading: true };
    }
    case actionTypes.MOBILE_CONSTANT_GET_LIST_SUCCESS: {
      return { ...state, loading: false, list: payload.list };
    }
    case actionTypes.MOBILE_CONSTANT_GET_LIST_FAILURE: {
      return { ...state, loading: false };
    }
    case actionTypes.MOBILE_CONSTANT_LOADING: {
      return { ...state, loading: payload };
    }
    default:
      return state;
  }
}
