// (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 { Component, OnInit } from '@angular/core';
import { ContextLevel } from '@/core/constants';
import { AddonCompetencySummary, AddonCompetency } from '@addons/competency/services/competency';
import { IonRefresher } from '@ionic/angular';
import { CoreNavigator } from '@services/navigator';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils';
import { ADDON_COMPETENCY_SUMMARY_PAGE } from '@addons/competency/competency.module';

/**
 * Page that displays the competency summary.
 */
@Component({
    selector: 'page-addon-competency-competency-summary',
    templateUrl: 'competencysummary.html',
})
export class AddonCompetencyCompetencySummaryPage implements OnInit {

    competencyLoaded = false;
    competencyId!: number;
    competency?: AddonCompetencySummary;
    contextLevel?: ContextLevel;
    contextInstanceId?: number;

    protected fetchSuccess = false; // Whether a fetch was finished successfully.

    /**
     * @inheritdoc
     */
    async ngOnInit(): Promise<void> {
        try {
            this.competencyId = CoreNavigator.getRequiredRouteNumberParam('competencyId');
            this.contextLevel = CoreNavigator.getRouteParam<ContextLevel>('contextLevel');
            this.contextInstanceId = CoreNavigator.getRouteNumberParam('contextInstanceId');
        } catch (error) {
            CoreDomUtils.showErrorModal(error);

            CoreNavigator.back();

            return;
        }

        try {
            await this.fetchCompetency();
        } finally {
            this.competencyLoaded = true;
        }
    }

    /**
     * Fetches the competency summary and updates the view.
     *
     * @returns Promise resolved when done.
     */
    protected async fetchCompetency(): Promise<void> {
        try {
            const result = await AddonCompetency.getCompetencySummary(this.competencyId);
            if (!this.contextLevel || this.contextInstanceId === undefined) {
                // Context not specified, use user context.
                this.contextLevel = ContextLevel.USER;
                this.contextInstanceId = result.usercompetency?.userid;
            }

            this.competency = result.competency;

            if (!this.fetchSuccess) {
                this.fetchSuccess = true;
                CoreUtils.ignoreErrors(AddonCompetency.logCompetencyView(this.competencyId, this.competency.competency.shortname));
            }
        } catch (error) {
            CoreDomUtils.showErrorModalDefault(error, 'Error getting competency summary data.');
        }
    }

    /**
     * Refreshes the competency summary.
     *
     * @param refresher Refresher.
     */
    refreshCompetency(refresher: IonRefresher): void {
        AddonCompetency.invalidateCompetencySummary(this.competencyId).finally(() => {
            this.fetchCompetency().finally(() => {
                refresher?.complete();
            });
        });
    }

    /**
     * Opens the summary of a competency.
     *
     * @param competencyId Competency Id.
     */
    openCompetencySummary(competencyId: number): void {
        CoreNavigator.navigate(
            `../../${competencyId}/${ADDON_COMPETENCY_SUMMARY_PAGE}`,
            {
                params: { contextLevel: this.contextLevel, contextInstanceId: this.contextInstanceId },
            },
        );
    }

}
