import { actionTypes } from 'constants/action-types';
import { isNodePage } from 'utils/utils';

export enum screenStateType {
  untreated, //未处理
  firstRound, // 高优先级已结束
  lastRound // 低优先级请求已结束
}

export enum screenEditModel {
  CANVAS = 'canvas', //画布编辑
  NODES = 'nodes' // 节点管理
}

export function setScreenDataState(state: screenStateType = screenStateType.lastRound) {
  return async (dispatch) => {
    return dispatch({
      type: actionTypes.SCREEN_DATA_STATE,
      payload: { screenState: state }
    });
  };
}

export function updateScreentEditModel(model: screenEditModel = screenEditModel.CANVAS) {
  return async (dispatch) => {
    return dispatch({
      type: actionTypes.SCREEN_EDIT_MODEL,
      payload: {
        editModel: model
      }
    });
  };
}
const defaultType = isNodePage() ? screenEditModel.NODES : screenEditModel.CANVAS;

export interface initialStateType {
  screenState: screenStateType;
  editModel: screenEditModel;
}

const initialState: initialStateType = {
  screenState: screenStateType.lastRound,
  editModel: defaultType
};

export default function reducer(state = initialState, { type, payload }): initialStateType {
  switch (type) {
    case actionTypes.SCREEN_DATA_STATE: {
      const { screenState = screenStateType.lastRound } = payload;
      return { ...state, screenState };
    }
    case actionTypes.SCREEN_EDIT_MODEL: {
      const { editModel = screenEditModel.CANVAS } = payload;
      return { ...state, editModel };
    }
    default:
      return state;
  }
}
