import * as notifyService from '@services/message/notification';
import { dataUtils, stringUtils } from '@utils';
import { routerRedux } from 'dva/router';

export default {
  namespace: 'notification',

  state: {
    todoData: [],
    todoParams: {},
    warnData: [],
    warnParams: {},
    infoData: [],
    infoParams: {},
    total: 0,
    currentItem: {},
    modalVisible: false,
    mode: 'create',
    activeTab: 'INBOX_TODO',
    isValidRcpt: false,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/messages/notification') {
          // dispatch({ type: 'clean' });
          // dispatch({ type: 'query' });
          const type = stringUtils.queryURL('type');
          if (type) {
            dispatch({
              type: 'updateState',
              payload: { activeTab: type },
            });
          }
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, select, put }) {
      const { activeTab } = yield select(({ notification }) => notification);
      const { todoParams, warnParams, infoParams } = yield select(({ notification }) => notification);
      const params = activeTab === 'INBOX_TODO' ? todoParams :
        (activeTab === 'INBOX_WARN' ? warnParams : infoParams);

      const rcptData = yield call(notifyService.rcptValid);
      if (rcptData.success) {
        yield put({
          type: 'updateState',
          payload: {
            isValidRcpt: rcptData.data
          },
        });
      } else {
        throw rcptData;
      }

      const data = yield call(notifyService.query, { ...params, ...payload, type: activeTab });
      if (data.success) {
        const result = data.data.rows;
        const newData = result.map((i) => {
          return Object.assign({}, {
            id: i.id,
            msgStatus: i.readTime === null,
            msgSubject: i.payload.subject,
            msgClass: i.type,
            msgEventType: i.payload.eventTypeName,
            msgSnsType: i.payload.snsTypeName,
            msgSendTime: dataUtils.dtm(i.createTime),
            readTime: dataUtils.dtm(i.readTime),
          });
        });

        if (activeTab === 'INBOX_TODO') {
          yield put({
            type: 'updateState',
            payload: {
              todoData: newData,
              todoParams: { ...params, total: data.data.total },
            },
          });
        }

        if (activeTab === 'INBOX_WARN') {
          yield put({
            type: 'updateState',
            payload: {
              warnData: newData,
              warnParams: { ...params, total: data.data.total },
            },
          });
        }

        if (activeTab === 'INBOX_INFO') {
          yield put({
            type: 'updateState',
            payload: {
              infoData: newData,
              infoParams: { ...params, total: data.data.total },
            },
          });
        }

      } else {
        throw data;
      }
    },

    * querySingle({ payload }, { call, put, select }) {
      const { activeTab } = yield select(({ notification }) => notification);
      const data = yield call(notifyService.getSingle, { id: payload.id, type: activeTab });
      if (data.success) {
        const result = data.data;
        const newData = {
          id: result.id,
          msgSubject: result.payload.subject,
          msgContent: result.payload.content,
          msgEventType: result.payload.eventType,
          msgSnsType: result.payload.snsType,
          msgSendTime: dataUtils.dtm(result.createTime),
        };
        yield put({
          type: 'updateStatus',
          payload: {
            id: result.id,
            activeTab,
          },
        });
        yield put({
          type: 'showModal',
          payload: {
            currentItem: newData,
            mode: payload.mode,
          },
        });
        yield put({
          type: 'app/updateMsgCnt',
          payload: {
            type: 'info',
            mode: 'minus'
          },
        });
      } else {
        throw data;
      }

      yield put({
        type: 'showModal',
        payload: {
          mode: payload.mode,
        },
      });
    },

    * getSingle({ payload }, { put }) {
      if (payload.mode === 'create') {
        yield put({
          type: 'showModal',
          payload: {
            mode: payload.mode,
          },
        });
      } else {
        yield put({ type: 'querySingle', payload });
      }
    },

    * view({ payload }, { put }) {
      yield put({ type: 'hideModal' });
    },

    * hideModal({ payload }, { put }) {
      yield put({
        type: 'updateState',
        payload: {
          modalVisible: false,
          currentItem: {},
        },
      });
    },

    * clean({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          todoData: [],
          todoParams: {},
          warnData: [],
          warnParams: {},
          infoData: [],
          infoParams: {},
          total: 0,
          currentItem: {},
          modalVisible: false,
          mode: 'create',
          activeTab: 'INBOX_TODO',
        },
      });
    },

    * updateStatus({ payload }, { put, select }) {
      const { id, activeTab } = payload;
      const { todoData, warnData, infoData } = yield select(({ notification }) => notification);
      const list = activeTab === 'INBOX_TODO' ? todoData :
        (activeTab === 'INBOX_WARN' ? warnData : infoData);

      const data = list.filter((i) => i.id === id);
      data[0].msgStatus = false;
      yield put({
        type: 'updateState',
        payload: {
          todoData,
          warnData,
          infoData,
        },
      });
    },

    * changeTabs({ payload }, { put }) {
      const { activeTab } = payload;
      yield put({
        type: 'updateState',
        payload,
      });
      yield put(routerRedux.replace({
        pathname: location.pathname,
        search: '?type=' + activeTab,
      }));
    }
  },

  reducers: {

    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    showModal(state, action) {
      return { ...state, ...action.payload, modalVisible: true };
    },

  },

};
