import { createTypedReducerWithImmer } from "@insite/client-framework/Common/CreateTypedReducer";
import { GetDealersApiParameter } from "@insite/client-framework/Services/DealerService";
import { assignById, setDataViewLoaded, setDataViewLoading } from "@insite/client-framework/Store/Data/DataState";
import { DealersDataView, DealersState } from "@insite/client-framework/Store/Data/Dealers/DealersState";
import { DealerCollectionModel, DealerModel } from "@insite/client-framework/Types/ApiModels";
import { Draft } from "immer";

const initialState: DealersState = {
    isLoading: {},
    byId: {},
    dataViews: {},
    idByPath: {},
    defaultLocation: {
        latitude: 0,
        longitude: 0,
    },
    pathNotFound: {},
};

const reducer = {
    "Data/Dealers/BeginLoadDealers": (draft: Draft<DealersState>, action: { parameter: GetDealersApiParameter }) => {
        setDataViewLoading(draft, action.parameter);
    },

    "Data/Dealers/CompleteLoadDealers": (
        draft: Draft<DealersState>,
        action: { parameter: GetDealersApiParameter; collection: DealerCollectionModel },
    ) => {
        if (
            draft.defaultLocation.latitude !== action.collection.defaultLatitude ||
            draft.defaultLocation.longitude !== action.collection.defaultLongitude
        ) {
            draft.defaultLocation = {
                latitude: action.collection.defaultLatitude,
                longitude: action.collection.defaultLongitude,
            };
        }
        setDataViewLoaded(
            draft,
            action.parameter,
            action.collection,
            collection => collection.dealers!,
            dealer => {
                if (dealer.detailPagePath) {
                    draft.idByPath[dealer.detailPagePath] = dealer.id;
                }
            },
            (dataView: Draft<DealersDataView>) => {
                dataView.defaultLatitude = action.collection.defaultLatitude;
                dataView.defaultLongitude = action.collection.defaultLongitude;
                dataView.defaultRadius = action.collection.defaultRadius;
                dataView.distanceUnitOfMeasure = action.collection.distanceUnitOfMeasure;
            },
        );
    },

    "Data/Dealers/BeginLoadDealer": (draft: Draft<DealersState>, action: { key: string }) => {
        draft.isLoading[action.key] = true;
    },

    "Data/Dealers/CompleteLoadDealer": (
        draft: Draft<DealersState>,
        action: { model: DealerModel; key: string; path: string },
    ) => {
        delete draft.isLoading[action.key];

        if (action.path) {
            draft.idByPath[action.path.toLowerCase()] = action.model.id;
        }

        assignById(draft, action.model);
    },

    "Data/Dealers/Reset": () => {
        return initialState;
    },

    "Data/Dealers/BeginLoadDealerByPath": (draft: Draft<DealersState>, action: { path: string }) => {
        draft.isLoading[action.path] = true;
    },

    "Data/Dealers/CompleteLoadDealerByPath": (
        draft: Draft<DealersState>,
        action: { dealer: DealerModel; path: string },
    ) => {
        delete draft.isLoading[action.path];

        if (!action?.dealer) {
            draft.pathNotFound[action.path] = true;
            return;
        }

        draft.byId[action.dealer.id] = action.dealer;
        draft.idByPath[action.path] = action.dealer.id;
    },
};

export default createTypedReducerWithImmer(initialState, reducer);
