import parseQueryString from "@insite/client-framework/Common/Utilities/parseQueryString";
import Zone from "@insite/client-framework/Components/Zone";
import { GetJobQuotesApiParameter } from "@insite/client-framework/Services/JobQuoteService";
import ApplicationState from "@insite/client-framework/Store/ApplicationState";
import loadJobQuotes from "@insite/client-framework/Store/Data/JobQuotes/Handlers/LoadJobQuotes";
import { getJobQuotesDataView } from "@insite/client-framework/Store/Data/JobQuotes/JobQuotesSelector";
import { getLocation } from "@insite/client-framework/Store/Data/Pages/PageSelectors";
import updateSearchFields from "@insite/client-framework/Store/Pages/RfqJobQuotes/Handlers/UpdateSearchFields";
import PageModule from "@insite/client-framework/Types/PageModule";
import PageProps from "@insite/client-framework/Types/PageProps";
import Page from "@insite/mobius/Page";
import { HasHistory, withHistory } from "@insite/mobius/utilities/HistoryContext";
import qs from "qs";
import React, { useEffect } from "react";
import { connect, ResolveThunks } from "react-redux";

const mapStateToProps = (state: ApplicationState) => ({
    session: state.context.session,
    jobQuotesDataView: getJobQuotesDataView(state, state.pages.rfqJobQuotes.getJobQuotesParameter),
    getJobQuotesParameter: state.pages.rfqJobQuotes.getJobQuotesParameter,
    location: getLocation(state),
});

const mapDispatchToProps = {
    loadJobQuotes,
    updateSearchFields,
};

type Props = PageProps & ReturnType<typeof mapStateToProps> & ResolveThunks<typeof mapDispatchToProps> & HasHistory;

const RfqJobQuotesPage = ({
    id,
    jobQuotesDataView,
    getJobQuotesParameter,
    history,
    location,
    loadJobQuotes,
    updateSearchFields,
}: Props) => {
    let firstLoad = false;

    useEffect(() => {
        firstLoad = true;
        let parsedGetJobQuotesParameter: GetJobQuotesApiParameter = {};
        if (location.search) {
            parsedGetJobQuotesParameter = parseQueryString<GetJobQuotesApiParameter>(location.search);
        }

        updateSearchFields({
            ...getJobQuotesParameter,
            ...parsedGetJobQuotesParameter,
            type: "Replace",
        });
    }, []);

    useEffect(() => {
        if (!firstLoad) {
            const queryString = qs.stringify(getJobQuotesParameter);
            history.replace(`${location.pathname}${queryString !== "" ? `?${queryString}` : ""}`);
        }
    }, [getJobQuotesParameter]);

    useEffect(() => {
        if (!firstLoad && !jobQuotesDataView.value && !jobQuotesDataView.isLoading) {
            loadJobQuotes(getJobQuotesParameter);
        }
    });

    return (
        <Page>
            <Zone contentId={id} zoneName="Content" />
        </Page>
    );
};

const pageModule: PageModule = {
    component: connect(mapStateToProps, mapDispatchToProps)(withHistory(RfqJobQuotesPage)),
    definition: {
        hasEditableUrlSegment: true,
        hasEditableTitle: true,
        pageType: "System",
    },
};

export default pageModule;

/**
 * @deprecated Use string literal "RfqJobQuotesPage" instead of this constant.
 */
export const RfqJobQuotesPageContext = "RfqJobQuotesPage";
