import { Currency } from "@/components/PaymentTable";
import { createSlice, createSelector } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";

export enum EUserDeviceType {
  ios,
  android,
}

interface IUserConfig {
  deviceType: EUserDeviceType;
  isShowTryApp: boolean;
  isForceShortPath: boolean;
  feature: string;
  authCode: string;
  dateOfPaymentChatMike: string;
  currency: Currency;
}

const initialState: IUserConfig = {
  deviceType: EUserDeviceType.ios,
  isShowTryApp: false,
  isForceShortPath: false,
  feature: "",
  authCode: "",
  dateOfPaymentChatMike: "",
  currency: Currency.USD,
};

const userConfigSlice = createSlice({
  name: "userConfig",
  initialState,
  reducers: {
    update(state, action: PayloadAction<Partial<IUserConfig>>) {
      return { ...initialState, ...state, ...action.payload };
    },
    addDeviceType(state, action: PayloadAction<EUserDeviceType>) {
      state.deviceType = action.payload;
      return state;
    },
    addIsShowTryApp(state, action: PayloadAction<boolean>) {
      state.isShowTryApp = action.payload;
      return state;
    },
    addIsForceShortPath(state, action: PayloadAction<boolean>) {
      state.isForceShortPath = action.payload;
      return state;
    },
    setFeature(state, action: PayloadAction<string>) {
      state.feature = action.payload;
      return state;
    },
    setAuthCode(state, action: PayloadAction<string>) {
      state.authCode = action.payload;
      return state;
    },
    setDateOfPaymentChatMike(state, action: PayloadAction<Date>) {
      state.dateOfPaymentChatMike = action.payload.toISOString();
      return state;
    },
    setCurrency(state, action: PayloadAction<Currency>) {
      state.currency = action.payload;
      return state;
    }
  },
  extraReducers: (builder) => builder.addCase("reset", () => initialState),
});

export const { actions } = userConfigSlice;
export const selectUserDeviceType = createSelector(
  (state: { userConfig: IUserConfig }) => state.userConfig.deviceType,
  (userConfig) => userConfig
);
export const selectIsShowTryApp = createSelector(
  (state: { userConfig: IUserConfig }) => state.userConfig.isShowTryApp,
  (userConfig) => userConfig
);
export const selectIsForceShortPath = createSelector(
  (state: { userConfig: IUserConfig }) => state.userConfig.isForceShortPath,
  (userConfig) => userConfig
);
export const selectFeature = createSelector(
  (state: { userConfig: IUserConfig }) => state.userConfig.feature,
  (userConfig) => userConfig
);
export const selectAuthCode = createSelector(
  (state: { userConfig: IUserConfig }) => state.userConfig.authCode,
  (userConfig) => userConfig
);
export const selectDateOfPaymentChatMike = createSelector(
  (state: { userConfig: IUserConfig }) => state.userConfig.dateOfPaymentChatMike,
  (userConfig) => userConfig
);
export const selectCurrency = createSelector(
  (state: { userConfig: IUserConfig }) => state.userConfig.currency,
  (userConfig) => userConfig
);
export default userConfigSlice.reducer;
