import difference from 'lodash/difference';
import filter from 'lodash/filter';
import isEmpty from 'lodash/isEmpty';
import { RouteComponentProps } from 'react-router';
import { AppRouteSections, IRouteQueryParams, IRouteUrlParams } from '../reducers/RouteReducer';
import CaseUtils from './caseUtils';

export default class RouteUtils {

    static getUrlEndpoint() {
        return '/';
    }

    static getActiveSection(routeProps: RouteComponentProps<IRouteUrlParams>, appRouteSectionsSequence: AppRouteSections[]): AppRouteSections {
        if (routeProps.location.pathname === '/') {
            return AppRouteSections.PRODUCT_SELECTOR;
        }
        const allSections = filter(routeProps.location.pathname.split('/'), (section) => !isEmpty(section));
        const activeSectionFromURL = allSections[allSections.length - 1] as AppRouteSections;
        return appRouteSectionsSequence.indexOf(activeSectionFromURL) > -1 ? activeSectionFromURL : null;
    }

    static getQueryString(routeProps: RouteComponentProps<IRouteUrlParams>, resetSearchParams = false): string {
        const parsedUrlParams = CaseUtils.getUrlParsedParams(routeProps.location.search);
        const product = parsedUrlParams.product;
        const version = parsedUrlParams.version;
        const queryParams: IRouteQueryParams = {
            product,
            version
        };
        return !resetSearchParams ? CaseUtils.getStringifiedParams(queryParams) : undefined;
    }
    static navigateToSection(routeProps: RouteComponentProps<IRouteUrlParams>, newSection: AppRouteSections, appRouteSectionsSequence: AppRouteSections[], resetSearchParams = false) {
        const navigationObj = { pathname: RouteUtils.getRoutePathForActiveSection(newSection, appRouteSectionsSequence), search: RouteUtils.getQueryString(routeProps, resetSearchParams) };
        routeProps.history.replace(navigationObj);
    }

    static getNavigateToSectionLink(routeProps: RouteComponentProps<IRouteUrlParams>, newSection: AppRouteSections, appRouteSectionsSequence: AppRouteSections[], resetSearchParams = false) {
        return { pathname: RouteUtils.getRoutePathForActiveSection(newSection, appRouteSectionsSequence), search: RouteUtils.getQueryString(routeProps, resetSearchParams) };
    }

    static getCompletedSectionsForActiveSection(activeSection: AppRouteSections, appRouteSectionsSequence: AppRouteSections[]): AppRouteSections[] {
        // Once Case is submitted, we dont want the previous section in main content but we want there disabled placeholders in progress bar
        if (activeSection === AppRouteSections.SUBMIT_CASE) {
            return difference(appRouteSectionsSequence, [AppRouteSections.SUBMIT_CASE]);
        }
        const activeSectionIndex = appRouteSectionsSequence.indexOf(activeSection);
        return appRouteSectionsSequence.slice(0, activeSectionIndex);
    }
    static getRoutePathForActiveSection(activeSection: AppRouteSections, appRouteSectionsSequence: AppRouteSections[]): string {
        if (activeSection === AppRouteSections.SUBMIT_CASE) {
            return `/${AppRouteSections.SUBMIT_CASE}`;
        }
        const completedSections = RouteUtils.getCompletedSectionsForActiveSection(activeSection, appRouteSectionsSequence);
        return (isEmpty(completedSections)) ? `/${activeSection}` : `/${completedSections.join('/')}/${activeSection}`;
    }
}
