/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 {PipelineService} from "../../../platform-services/apis/pipeline.service";
import {DialogRef} from "../../../core-ui/dialog/base-dialog/dialog-ref";
import {forkJoin} from "rxjs";
import {Pipeline} from "../../../core-model/gen/streampipes-model";

@Component({
    selector: 'import-pipeline-dialog',
    templateUrl: './import-pipeline-dialog.component.html',
    styleUrls: ['./import-pipeline-dialog.component.scss']
})
export class ImportPipelineDialogComponent {

    isInProgress: any = false;
    currentStatus: any;
    page = "upload-pipelines";

    availablePipelines: Pipeline[] = [];
    selectedPipelines: Pipeline[] = [];

    importing: boolean = false;

    pages = [{
        type: "upload-pipelines",
        title: "Upload",
        description: "Upload a json file containing the pipelines to import"
    }, {
        type: "select-pipelines",
        title: "Select pipelines",
        description: "Select the pipelines to import"
    }, {
        type: "import-pipelines",
        title: "Import",
        description: ""
    }];

    constructor(private PipelineService: PipelineService,
                private DialogRef: DialogRef<ImportPipelineDialogComponent>) {
    }

    handleFileInput(files: any) {
        let file = files[0];
        var aReader = new FileReader();
        aReader.readAsText(file, "UTF-8");
        aReader.onload = evt => {
            this.availablePipelines = JSON.parse(aReader.result as string);
            console.log(this.availablePipelines);
            this.page = "select-pipelines";
        }
    }

    close(refreshPipelines: boolean) {
        this.DialogRef.close(refreshPipelines);
    };

    back() {
        if (this.page == 'select-pipelines') {
            this.page = 'upload-pipelines';
        } else if (this.page == 'import-pipelines') {
            this.page = 'select-pipelines';
        }
    }

    toggleSelectedPipeline(pipeline: Pipeline) {
        console.log(pipeline);
        if (this.selectedPipelines.some(p => p._id = pipeline._id)) {
            this.selectedPipelines.splice(this.selectedPipelines.findIndex(sp => sp._id === pipeline._id), 1);
        } else {
            this.selectedPipelines.push(pipeline);
        }
    }

    storePipelines() {
        console.log(this.selectedPipelines);
         var promises = [];
         this.selectedPipelines.forEach(pipeline => {
             pipeline._rev = undefined;
             pipeline._id = undefined;
             promises.push(this.PipelineService.storePipeline(pipeline));
         });

         forkJoin(promises).subscribe(results => {
             this.importing = false;
             this.close(true);
         })
    }

    startImport() {
        this.page = 'import-pipelines';
        this.importing = true;
        this.storePipelines();
    }

}