import Actions from '@/types/Store/Actions';
import {
  FetchWasherDeviceInfoRes,
  CreateWasherOrderRes,
  AppointOrder,
  AppointOrderStatus,
} from '@/api/washingMachine';

const INITIAL_STATE = {
  deviceInfo: {
    bluetoothMode: 0,
    deviceCode: '',
    deviceName: '',
    enabledAppoint: false,
    optionalPrograms: [],
    paymentWaysInner: [],
    paymentWaysOuter: [],
    position: '',
    requiredPrograms: [],
  },
  orderInfo: {
    appointmentTimeout: 0,
    deviceCode: '',
    deviceName: '',
    money: 0,
    orderCode: '',
    orderType: 0,
    payTimeout: 0,
    paymentWayId: 0,
    position: '',
    serviceId: 0,
    services: [],
  },
  orderDetail: {
    bluetoothMode: 0,
    deviceCode: '',
    deviceName: '',
    endDate: '',
    money: 0,
    orderCode: '',
    position: '',
    services: [],
    startDate: '',
    status: AppointOrderStatus.appointing,
  },
};

export type WasherStoreState = {
  deviceInfo: FetchWasherDeviceInfoRes;
  orderInfo: CreateWasherOrderRes;
  orderDetail: AppointOrder;
};

export const updateWasherDeviceInfo = (
  entity: StoreState<FetchWasherDeviceInfoRes>,
): Actions => ({
  type: 'UPDATE_WASHER_DEVICE',
  payload: entity,
});

export const updateWasherOrderInfo = (
  entity: StoreState<CreateWasherOrderRes>,
): Actions => ({
  type: 'UPDATE_WASHER_ORDER',
  payload: entity,
});
export const updateWasherOrderDetail = (
  entity: StoreState<AppointOrder>,
): Actions => ({
  type: 'UPDATE_WASHER_DETAIL',
  payload: entity,
});

export default function washer(
  state: WasherStoreState = INITIAL_STATE,
  actions: Actions,
): WasherStoreState {
  switch (actions.type) {
    case 'UPDATE_WASHER_DEVICE':
      return {
        ...state,
        deviceInfo: {
          ...state.deviceInfo,
          ...actions.payload,
        },
      };
    case 'UPDATE_WASHER_ORDER':
      return {
        ...state,
        orderInfo: {
          ...state.orderInfo,
          ...actions.payload,
        },
      };
    case 'UPDATE_WASHER_DETAIL':
      return {
        ...state,
        orderDetail: {
          ...state.orderDetail,
          ...actions.payload,
        },
      };
    default:
      return state;
  }
}
