import {createSlice, PayloadAction} from "@reduxjs/toolkit";
import {noShippingWarehouse, ShippingWarehouse} from "../../types/shippingWarehouse";
import ShippingWarehouseSelect from "../../components/ShippingWarehouseSelect/ShippingWarehouseSelect";
import {City, noCity} from "../../types/city";

export enum OrderStatus {
    ALL = -1,
    AWAITING_PACKAGING,
    AWAITING_DELIVER,
    DELIVERING,
    DELIVERED,
    CANCELLED,
    PARTIALLY_RETURNED,
    FULLY_RETURNED,
    DISPUTED
}

export const orderStatuses = [
    {key: OrderStatus.ALL, label: 'Все'},
    {key: OrderStatus.AWAITING_PACKAGING, label: 'Ожидает сборки'},
    {key: OrderStatus.AWAITING_DELIVER, label: 'Ожидает отгрузки'},
    {key: OrderStatus.DELIVERING, label: 'Доставляется'},
    {key: OrderStatus.DELIVERED, label: 'Доставлен'},
    {key: OrderStatus.CANCELLED, label: 'Отменен'},
    {key: OrderStatus.PARTIALLY_RETURNED, label: 'Частичный возврат'},
    {key: OrderStatus.FULLY_RETURNED, label: 'Полный возврат'},
    {key: OrderStatus.DISPUTED, label: 'Спорный'},
];
export const OrderStatusDictionary: { [key in OrderStatus]: string } = {
    [OrderStatus.ALL]: 'Все',
    [OrderStatus.AWAITING_PACKAGING]: 'Ожидает сборки',
    [OrderStatus.AWAITING_DELIVER]: 'Ожидает отгрузки',
    [OrderStatus.DELIVERING]: 'Доставляется',
    [OrderStatus.DELIVERED]: 'Доставлен',
    [OrderStatus.CANCELLED]: 'Отменен',
    [OrderStatus.PARTIALLY_RETURNED]: 'Частичный возврат',
    [OrderStatus.FULLY_RETURNED]: 'Полный возврат',
    [OrderStatus.DISPUTED]: 'Спорный',
};

export interface OrdersFilterState {
    isVisible: boolean;
    orderBy: "createdOn" | "shipmentDate";
    desc: boolean;
    status: OrderStatus;
    shipmentDate: string;
    page: number;
    shippingWarehouse: ShippingWarehouse;
    city: City;
}

const initialState: OrdersFilterState = {
    isVisible: false,
    orderBy: "shipmentDate",
    desc: false,
    shipmentDate: (new Date()).toISOString(),
    status: OrderStatus.AWAITING_PACKAGING,
    page: 0,
    shippingWarehouse: noShippingWarehouse,
    city: noCity
}

export const ordersFilterSlice = createSlice({
    name: 'ordersFilter',
    initialState,
    reducers: {
        openOrdersFilterModal: (state) => {
            state.isVisible = true
        },
        closeOrdersFilterModal: (state) => {
            state.isVisible = false
        },
        setOrderBy: (state, action: PayloadAction<"createdOn" | "shipmentDate">) => {
            state.orderBy = action.payload;
        },
        setDesc: (state, action: PayloadAction<boolean>) => {
            state.desc = action.payload;
        },
        setStatus: (state, action: PayloadAction<OrderStatus>) => {
            state.status = action.payload;
        },
        setShipmentDate: (state, action: PayloadAction<string>) => {
            state.shipmentDate = action.payload;
        },
        setShippingWarehouse: (state, action: PayloadAction<ShippingWarehouse>) => {
            state.shippingWarehouse = action.payload;
        },
        setCity: (state, action: PayloadAction<City>) => {
            state.city = action.payload;
        },
        setPage: (state, action: PayloadAction<number>) => {
            console.log(action)
            state.page = action.payload;
        },
        nextPage: (state) => {
            state.page = state.page + 1
        },
        refreshPagination: (state) => {
            state.page = -1
        }
    }
})

export const {
    openOrdersFilterModal,
    closeOrdersFilterModal,
    refreshPagination,
    nextPage,
    setOrderBy,
    setDesc,
    setStatus,
    setShipmentDate,
    setCity,
    setPage,
    setShippingWarehouse
} = ordersFilterSlice.actions;
export default ordersFilterSlice.reducer;