import http, { request } from '../../../utils/fetch';
import { api } from '../../../constants/config';
import message from 'components/message';

export const constants = {
  INTERACTION_LIST_GET_BEGIN: 'INTERACTION_LIST_GET_BEGIN',
  INTERACTION_LIST_GET_SUCCESS: 'INTERACTION_LIST_GET_SUCCESS',
  INTERACTION_LIST_GET_FAILURE: 'INTERACTION_LIST_GET_FAILURE',
  INTERACTION_LIST_ADD_BEGIN: 'INTERACTION_LIST_ADD_BEGIN',
  INTERACTION_LIST_ADD_SUCCESS: 'INTERACTION_LIST_ADD_SUCCESS',
  INTERACTION_LIST_ADD_FAILURE: 'INTERACTION_LIST_ADD_FAILURE',
  INTERACTION_LIST_RENAME: 'INTERACTION_LIST_RENAME',
  INTERACTION_LIST_PUBLISH: 'INTERACTION_LIST_PUBLISH',
  INTERACTION_LIST_COPY_BEGIN: 'INTERACTION_LIST_COPY_BEGIN',
  INTERACTION_LIST_COPY_SUCCESS: 'INTERACTION_LIST_COPY_SUCCESS',
  INTERACTION_LIST_COPY_FAILURE: 'INTERACTION_LIST_COPY_FAILURE',
  INTERACTION_LIST_DELETE_BEGIN: 'INTERACTION_LIST_DELETE_BEGIN',
  INTERACTION_LIST_DELETE_SUCCESS: 'INTERACTION_LIST_DELETE_SUCCESS',
  INTERACTION_LIST_DELETE_FAILURE: 'INTERACTION_LIST_DELETE_FAILURE'
};

export const actions = {
  getList() {
    return async (dispatch) => {
      dispatch({
        type: constants.INTERACTION_LIST_GET_BEGIN
      });
      const data = await http.get(api.interactionList);
      if (data) {
        dispatch({
          type: constants.INTERACTION_LIST_GET_SUCCESS,
          payload: {
            data
          }
        });
      } else {
        dispatch({
          type: constants.INTERACTION_LIST_GET_FAILURE
        });
      }
    };
  },
  create({ name, width, height }) {
    return async (dispatch) => {
      const hide = message.loading('创建终端中...', 0);
      const result = await request(api.interactionAdd, undefined, undefined, JSON.stringify({ name, width, height }));
      hide();
      dispatch({
        type: constants.INTERACTION_LIST_ADD_BEGIN
      });
      if (result) {
        dispatch({
          type: constants.INTERACTION_LIST_ADD_SUCCESS
        });
        return result;
      } else {
        dispatch({
          type: constants.INTERACTION_LIST_ADD_FAILURE
        });
      }
    };
  },
  rename({ id, name }) {
    return async (dispatch) => {
      const data = await http.post(api.interactionEdit, { id, updateList: [{ key: 'name', value: name }] });
      if (data) {
        dispatch({
          type: constants.INTERACTION_LIST_RENAME,
          payload: { id, name }
        });
      }
    };
  },
  publish({ id, published }) {
    return async (dispatch) => {
      const data = await http.post(api.interactionEdit, { id, updateList: [{ key: 'published', value: published }] });
      if (data) {
        dispatch({
          type: constants.INTERACTION_LIST_PUBLISH,
          payload: {
            id,
            published
          }
        });
      }
    };
  },
  copy({ ids }) {
    return async (dispatch) => {
      dispatch({
        type: constants.INTERACTION_LIST_COPY_BEGIN
      });
      const data = await http.post(
        api.interactionCopy,
        { ids },
        {
          error: () => {
            message.error('复制失败');
          }
        }
      );
      if (data) {
        message.success('复制成功');
        dispatch({
          type: constants.INTERACTION_LIST_COPY_SUCCESS,
          payload: data
        });
      } else {
        dispatch({
          type: constants.INTERACTION_LIST_COPY_FAILURE
        });
      }
    };
  },
  delete({ ids }) {
    return async (dispatch) => {
      dispatch({
        type: constants.INTERACTION_LIST_DELETE_BEGIN
      });
      const data = await http.post(api.interactionDelete, { ids });
      if (data) {
        dispatch({
          type: constants.INTERACTION_LIST_DELETE_SUCCESS,
          payload: {
            ids
          }
        });
      } else {
        dispatch({
          type: constants.INTERACTION_LIST_DELETE_FAILURE
        });
      }
    };
  }
};

export function reducers(state, { type, payload }) {
  switch (type) {
    case constants.INTERACTION_LIST_GET_BEGIN: {
      return {
        ...state,
        loading: true
      };
    }
    case constants.INTERACTION_LIST_GET_SUCCESS: {
      return {
        ...state,
        list: payload.data,
        loading: false
      };
    }
    case constants.INTERACTION_LIST_GET_FAILURE: {
      return {
        ...state,
        loading: false
      };
    }
    case constants.INTERACTION_LIST_ADD_BEGIN: {
      return {
        ...state,
        createLoading: true
      };
    }
    case constants.INTERACTION_LIST_ADD_SUCCESS: {
      return {
        ...state,
        createLoading: false
      };
    }
    case constants.INTERACTION_LIST_ADD_FAILURE: {
      return {
        ...state,
        createLoading: false
      };
    }
    case constants.INTERACTION_LIST_RENAME: {
      const changed = state.list.find((d) => d.id === payload.id);
      const noChanged = state.list.filter((d) => d.id !== payload.id);

      return {
        ...state,
        list: [{ ...changed, name: payload.name }].concat(noChanged)
      };
    }
    case constants.INTERACTION_LIST_PUBLISH: {
      return {
        ...state,
        list: state.list.map((d) => (d.id === payload.id ? { ...d, published: payload.published ? 1 : 0 } : d))
      };
    }
    case constants.INTERACTION_LIST_COPY_BEGIN: {
      return {
        ...state,
        loading: true
      };
    }
    case constants.INTERACTION_LIST_COPY_SUCCESS: {
      return {
        ...state,
        list: payload.concat(state.list),
        loading: false
      };
    }
    case constants.INTERACTION_LIST_COPY_FAILURE: {
      return {
        ...state,
        loading: false
      };
    }
    case constants.INTERACTION_LIST_DELETE_BEGIN: {
      return {
        ...state,
        loading: true
      };
    }
    case constants.INTERACTION_LIST_DELETE_SUCCESS: {
      return {
        ...state,
        loading: false,
        list: state.list.filter((d) => payload.ids.indexOf(d.id) === -1)
      };
    }
    case constants.INTERACTION_LIST_DELETE_FAILURE: {
      return {
        ...state,
        loading: false
      };
    }
    default:
      return state;
  }
}
