// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { CoreError } from '@classes/errors/error';
import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler';
import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate';
import { CoreCourse } from '@features/course/services/course';
import { CoreNavigator } from '@services/navigator';
import { CoreSitesReadingStrategy } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom';
import { makeSingleton } from '@singletons';
import { AddonModWikiIndexPage } from '../../pages/index';
import { AddonModWiki } from '../wiki';
import { AddonModWikiModuleHandlerService } from './module';

/**
 * Handler to treat links to create a wiki page.
 */
@Injectable({ providedIn: 'root' })
export class AddonModWikiCreateLinkHandlerService extends CoreContentLinksHandlerBase {

    name = 'AddonModWikiCreateLinkHandler';
    featureName = 'CoreCourseModuleDelegate_AddonModWiki';
    pattern = /\/mod\/wiki\/create\.php.*([&?]swid=\d+)/;

    /**
     * Check if the current view is a wiki page of the same wiki.
     *
     * @param route Activated route if current route is wiki index page, null otherwise.
     * @param subwikiId Subwiki ID to check.
     * @param siteId Site ID.
     * @return Promise resolved with boolean: whether current view belongs to the same wiki.
     */
    protected async currentStateIsSameWiki(route: ActivatedRoute | null, subwikiId: number, siteId: string): Promise<boolean> {
        if (!route) {
            // Current view isn't wiki index.
            return false;
        }

        const params = route.snapshot.params;
        const queryParams = route.snapshot.queryParams;

        if (queryParams.subwikiId == subwikiId) {
            // Same subwiki, so it's same wiki.
            return true;
        }

        const options = {
            cmId: params.cmId,
            readingStrategy: CoreSitesReadingStrategy.PreferCache,
            siteId,
        };

        if (queryParams.pageId) {
            // Get the page contents to check the subwiki.
            try {
                const page = await AddonModWiki.getPageContents(queryParams.pageId, options);

                return page.subwikiid == subwikiId;
            } catch {
                // Not found, check next case.
            }
        }

        try {
            // Get the wiki.
            const wiki = await AddonModWiki.getWiki(params.courseId, params.cmId, options);

            // Check if the subwiki belongs to this wiki.
            return await AddonModWiki.wikiHasSubwiki(wiki.id, subwikiId, options);
        } catch {
            // Not found, return false.
            return false;
        }
    }

    /**
     * @inheritdoc
     */
    getActions(
        siteIds: string[],
        url: string,
        params: Record<string, string>,
        courseId?: number,
    ): CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
        courseId = Number(courseId || params.courseid || params.cid);

        return [{
            action: async (siteId: string) => {
                const modal = await CoreDomUtils.showModalLoading();

                try {
                    const route = CoreNavigator.getCurrentRoute({ pageComponent: AddonModWikiIndexPage });
                    const subwikiId = parseInt(params.swid, 10);
                    const wikiId = parseInt(params.wid, 10);
                    let moduleId: number;

                    // Check if the link is inside the same wiki.
                    const isSameWiki = await this.currentStateIsSameWiki(route, subwikiId, siteId);

                    if (isSameWiki) {
                        // User is seeing the wiki, we can get the module from the wiki params.
                        moduleId = route!.snapshot.params.cmId;
                        courseId = route!.snapshot.params.courseId;
                    } else if (wikiId) {
                        // The URL specifies which wiki it belongs to. Get the module.
                        const module = await CoreCourse.getModuleBasicInfoByInstance(wikiId, 'wiki', siteId);

                        moduleId = module.id;
                        courseId = module.course;
                    } else {
                        // Not enough data.
                        throw new CoreError();
                    }

                    // Open the page.
                    CoreNavigator.navigateToSitePath(
                        AddonModWikiModuleHandlerService.PAGE_NAME + `/${courseId}/${moduleId}/edit`,
                        {
                            params: {
                                pageTitle: params.title,
                                subwikiId: subwikiId,
                            },
                            siteId,
                        },
                    );
                } catch (error) {
                    CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true);
                } finally {
                    modal.dismiss();
                }
            },
        }];
    }

}

export const AddonModWikiCreateLinkHandler = makeSingleton(AddonModWikiCreateLinkHandlerService);
