import { PayloadAction, createSlice } from "@reduxjs/toolkit";
import { PartnerLicenseInfo } from "api";
import { PartnerLicensesState, HierarchicalElement } from "./state";
import { getMyAccountAsync, getPartnerLicenseAsync } from "./operations";
import { ACCOUNTS_VIEW_LIMIT } from "./constants";

const initialState: PartnerLicensesState = {
  domain: {
    myAccountInfo: { accountId: 0 },
    total: 0,
    ownPartnerLicense: {
      accountId: 0,
      tier: 0,
      companyName: "",
      stockLicense: 0,
      issuedRequested: 0,
      shortage: 0,
      issueRequesting: 0,
    },
    childrenPartnerLicenses: [],
    totalPage: 0,
  },
  apps: {
    limit: ACCOUNTS_VIEW_LIMIT,
    offset: 0,
    currentPage: 0,
    hierarchicalElements: [],
    isLoading: true,
    selectedRow: undefined,
  },
};

export const partnerLicenseSlice = createSlice({
  name: "partnerLicense",
  initialState,
  reducers: {
    pushHierarchicalElement: (
      state,
      action: PayloadAction<{
        hierarchicalElement: HierarchicalElement;
      }>
    ) => {
      const { hierarchicalElement } = action.payload;
      state.apps.hierarchicalElements.push(hierarchicalElement);
    },
    popHierarchicalElement: (state) => {
      state.apps.hierarchicalElements.pop();
    },
    spliceHierarchicalElement: (
      state,
      action: PayloadAction<{
        deleteCount: number;
      }>
    ) => {
      const { deleteCount } = action.payload;
      state.apps.hierarchicalElements.splice(-deleteCount);
    },
    changeSelectedRow: (
      state,
      action: PayloadAction<{ value?: PartnerLicenseInfo }>
    ) => {
      const { value } = action.payload;
      state.apps.selectedRow = value;
    },
    savePageInfo: (
      state,
      action: PayloadAction<{
        limit: number;
        offset: number;
      }>
    ) => {
      const { limit, offset } = action.payload;
      state.apps.limit = limit;
      state.apps.offset = offset;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(getMyAccountAsync.pending, (state) => {
      state.apps.isLoading = true;
    });
    builder.addCase(getMyAccountAsync.fulfilled, (state, action) => {
      state.domain.myAccountInfo = action.payload;
      state.apps.isLoading = false;
    });
    builder.addCase(getMyAccountAsync.rejected, (state) => {
      state.apps.isLoading = false;
    });
    builder.addCase(getPartnerLicenseAsync.pending, (state) => {
      state.apps.isLoading = true;
    });
    builder.addCase(getPartnerLicenseAsync.fulfilled, (state, action) => {
      state.domain.total = action.payload.total;
      state.domain.ownPartnerLicense = action.payload.ownPartnerLicense;
      state.domain.childrenPartnerLicenses =
        action.payload.childrenPartnerLicenses;
      state.apps.isLoading = false;
    });
    builder.addCase(getPartnerLicenseAsync.rejected, (state) => {
      state.apps.isLoading = false;
    });
  },
});
export const {
  pushHierarchicalElement,
  popHierarchicalElement,
  spliceHierarchicalElement,
  changeSelectedRow,
  savePageInfo,
} = partnerLicenseSlice.actions;

export default partnerLicenseSlice.reducer;
