/**
 * This file is part of Anis Client.
 *
 * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

import { Component, Input, Output, EventEmitter, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

import { Instance, Dataset, Survey, DatasetFamily, FileInfo, Attribute } from 'src/app/metamodel/models';

@Component({
    selector: 'app-dataset-form',
    templateUrl: 'dataset-form.component.html'
})
export class DatasetFormComponent implements OnInit, OnChanges {
    @Input() instance: Instance;
    @Input() dataset: Dataset;
    @Input() surveyList: Survey[];
    @Input() tableListIsLoading: boolean;
    @Input() tableListIsLoaded: boolean;
    @Input() tableList: string[];
    @Input() datasetFamilyList: DatasetFamily[];
    @Input() idDatasetFamily: number;
    @Input() rootDirectory: FileInfo[];
    @Input() rootDirectoryIsLoading: boolean;
    @Input() rootDirectoryIsLoaded: boolean;
    @Input() attributeList: Attribute[];
    @Output() changeSurvey: EventEmitter<number> = new EventEmitter();
    @Output() loadRootDirectory: EventEmitter<string> = new EventEmitter();
    @Output() onSubmit: EventEmitter<Dataset> = new EventEmitter();

    public isNewDataset = true;

    public imageFormGroup = new FormGroup({

    });

    public coneSearchFormGroup = new FormGroup({
        cone_search_enabled: new FormControl(false),
        cone_search_opened: new FormControl({value: false, disabled: true}),
        cone_search_column_ra: new FormControl({value: false, disabled: true}),
        cone_search_column_dec: new FormControl({value: false, disabled: true}),
        cone_search_plot_enabled: new FormControl({value: false, disabled: true}),
        cone_search_sdss_enabled: new FormControl({value: false, disabled: true}),
        cone_search_sdss_display: new FormControl({value: false, disabled: true}),
        cone_search_background: new FormControl({value: [], disabled: true})
    });

    public downloadFormGroup = new FormGroup({
        download_enabled: new FormControl(true),
        download_opened: new FormControl(false),
        download_csv: new FormControl(true),
        download_ascii: new FormControl(true),
        download_vo: new FormControl(false),
        download_archive: new FormControl(false)
    });

    public summaryFormGroup = new FormGroup({
        summary_enabled: new FormControl(false),
        summary_opened: new FormControl({value: false, disabled: true})
    });

    public serverlinkFormGroup = new FormGroup({
        server_link_enabled: new FormControl(false),
        server_link_opened: new FormControl({value: false, disabled: true})
    });

    public sampFormGroup = new FormGroup({
        samp_enabled: new FormControl(false),
        samp_opened: new FormControl({value: false, disabled: true})
    });

    public datatableFormGroup = new FormGroup({
        datatable_enabled: new FormControl(true),
        datatable_opened: new FormControl(true),
        datatable_selectable_rows: new FormControl(false)
    });

    public form = new FormGroup({
        name: new FormControl('', [Validators.required]),
        label: new FormControl('', [Validators.required]),
        survey_name: new FormControl('', [Validators.required]),
        table_ref: new FormControl('', [Validators.required]),
        id_dataset_family: new FormControl('', [Validators.required]),
        description: new FormControl('', [Validators.required]),
        data_path: new FormControl(''),
        display: new FormControl('', [Validators.required]),
        public: new FormControl('', [Validators.required]),
        config: new FormGroup({
            cone_search: this.coneSearchFormGroup,
            download: this.downloadFormGroup,
            summary: this.summaryFormGroup,
            server_link: this.serverlinkFormGroup,
            samp: this.sampFormGroup,
            datatable: this.datatableFormGroup
        })
    });

    ngOnInit() {
        if (this.dataset) {
            this.isNewDataset = false;
            this.form.patchValue(this.dataset);
        }
        this.form.controls.table_ref.disable();
        if (this.idDatasetFamily) {
            this.form.controls.id_dataset_family.setValue(this.idDatasetFamily);
        }
    }

    ngOnChanges(changes: SimpleChanges) {
        if (changes.tableListIsLoaded && changes.tableListIsLoaded.currentValue) {
            this.form.controls.table_ref.enable();
        } else {
            this.form.controls.table_ref.disable();
        }
    }

    submit() {
        if (this.dataset) {
            this.onSubmit.emit({
                ...this.dataset,
                ...this.form.getRawValue()
            });
        } else {
            this.onSubmit.emit(this.form.getRawValue());
        }
    }
    
    onChangeSurvey() {
        const surveyName = this.form.controls.survey_name.value;
        if (!surveyName) {
            this.form.controls.table_ref.disable();
        } else {
            this.changeSurvey.emit(this.surveyList.find(survey => survey.name === surveyName).id_database);
        }
    }

    onChangeDataPath(path: string) {
        this.loadRootDirectory.emit(this.instance.data_path + path);
        this.form.markAsDirty();
    }
}