import {Component} from '@angular/core';
import {ActivatedRoute} from '@angular/router';

import {APP_SETTINGS, FormulaEditorConfig, FormulaRuleType, GlobalModalsService, NotificationService} from 'm2-core';
import {CdDemoComponent} from '../../../../abstract/CdDemoComponent';
import {CdHelperService} from '../../../../../../service/cd-helper.service';


@Component({
    templateUrl: './cd-global-modals-service-demo.component.html'
})
export class CdGlobalModalsServiceDemoComponent extends CdDemoComponent {

    constructor(
        protected activatedRoute: ActivatedRoute,
        private globalModalsService: GlobalModalsService,
        private cdHelperService: CdHelperService,
        private notificationService: NotificationService
    ) {
        super();
    }


    // region nodesSelector
    private nodeIds: number[] = [];

    onSelectNodesClick(multiselect = true, cb?: (nodeIds: number[]) => any): void {
        this.globalModalsService.openDialog('NodesSelectorComponent', {
            multiselect,
            showSearchInput: true,
            prevSelectedNodeIds: this.nodeIds,
            onSelectedFn: nodeIds => {
                if (cb) {
                    cb(nodeIds);
                } else {
                    this.nodeIds = nodeIds;
                    this.notificationService.success(
                        $localize`Выбранные группы` + `: ${nodeIds}`,
                        APP_SETTINGS.QUICK_NOTIFICATION_LIFETIME
                    );
                }
            }
        });
    }
    // endregion nodesSelector


    // region classifierSelector
    private classifierItemId?: number;

    async onSelectClassifierNodeClick() {
        const classifierId = await this.cdHelperService.getAnyClassifierId();
        this.globalModalsService.openDialog('ClassifierSelectorComponent', {
            classifierId,
            onSelectedFn: ([classifierItemId]) => {
                this.notificationService.success($localize`Выбранна группа` + ' ' + classifierItemId);
                this.classifierItemId = classifierItemId;
            },
            prevSelectedEntityId: this.classifierItemId
        });
    }
    // endregion classifierSelector


    // region formulaEditor
    async onOpenFormulaEditorClick() {
        const config = new FormulaEditorConfig(true);
        config.formulaType = FormulaRuleType.PropertyValue;
        config.nodeIds = [await this.cdHelperService.getAnyNodeId()];
        this.globalModalsService.openDialog('FormulaEditorComponent', config);
    }
    // endregion formulaEditor


    // region nodeRelations
    async onOpenNodeRelationsClick() {
        const nodeId = await this.cdHelperService.getNodeIdWithRelations();
        this.globalModalsService.openDialog('NodeRelationsDialogComponent', {
            nodeId
        });
    }

    onOpenNodeRelationsForSpecificNodeClick() {
        this.onSelectNodesClick(false, nodeIds => {
            this.globalModalsService.openDialog('NodeRelationsDialogComponent', {
                nodeId: nodeIds[0]
            });
        });
    }
    // endregion nodeRelations


    // region itemGenerator
    onOpenItemGeneratorClick() {
        this.onSelectNodesClick(false, nodeIds => {
            this.globalModalsService.openDialog('ItemGeneratorComponent', {
                nodeId: nodeIds[0],
                baseTablePresentationId: 1 // is required, pass fake for demo
            });
        });
    }
    // endregion itemGenerator


    // region itemVersions
    async onOpenItemVersionsClick() {
        const itemId = await this.cdHelperService.getAnyItemId();
        this.globalModalsService.openDialog('ItemVersionsDialogComponent', {
            itemId
        });
    }
    // endregion itemVersions


    // region applicationEditor
    async onOpenApplicationEditorClick() {
        const application = await this.cdHelperService.getAnyApplication();
        this.globalModalsService.openDialog('ApplicationEditorComponent', {
            application
        });
    }
    // endregion applicationEditor


    // region applicationGroupEditor
    onOpenApplicationGroupClick() {
        this.cdHelperService.getNextApplicationGroupUid()
            .subscribe(groupUid => {
                this.globalModalsService.openDialog('ApplicationsGroupEditorComponent', {
                    groupUid
                });
            });
    }
    // endregion applicationGroupEditor


    // region fileSelector
    onSelectSingleFileClick() {
        this.globalModalsService.selectFiles('single', f => {
            this.notificationService.success(`Выбран файл "${f.name}"`);
        });
    }

    onSelectSingleFileBase64Click() {
        this.globalModalsService.selectFiles('singleBase64', f => {
            this.notificationService.success(`Выбранный файл преобразован в Base64-кодированную строку (см. консоль)`);
            console.log(f);
        });
    }

    onSelectMultipleFilesClick() {
        this.globalModalsService.selectFiles('multiple', files => {
            this.notificationService.success(`Выбраны файлы ${files.map(f => '"' + f.name + '"').join(', ')}`);
        });
    }
    // endregion fileSelector

}
