import { FormGroup } from '@angular/forms';
import { DatasetProfileComboBoxType } from '../../../../../core/common/enum/dataset-profile-combo-box-type';
import { AutoCompleteFieldData } from '../../../../../core/model/dataset-profile-definition/field-data/field-data';
import { FieldDataEditorModel } from './field-data-editor-model';
import { FieldDataOptionEditorModel } from './field-data-option-editor-model';

export class AutoCompleteFieldDataEditorModel extends FieldDataEditorModel<AutoCompleteFieldDataEditorModel> {

	public type: DatasetProfileComboBoxType = DatasetProfileComboBoxType.Autocomplete;
	public url: string;
	public optionsRoot: string;
	public multiAutoComplete: boolean;
	public autoCompleteOptions: FieldDataOptionEditorModel = new FieldDataOptionEditorModel();
	//public multiAutoCompleteOptions: FieldDataOptionEditorModel = new FieldDataOptionEditorModel();

	buildForm(disabled: boolean = false, skipDisable: Array<String> = []): FormGroup {
		const formGroup = this.formBuilder.group({
			label: [{ value: this.label, disabled: (disabled && !skipDisable.includes('AutoCompleteFieldDataEditorModel.label')) }],
			type: [{ value: this.type, disabled: (disabled && !skipDisable.includes('AutoCompleteFieldDataEditorModel.type')) }],
			url: [{ value: this.url, disabled: (disabled && !skipDisable.includes('AutoCompleteFieldDataEditorModel.url')) }],
			optionsRoot: [{ value: this.optionsRoot, disabled: (disabled && !skipDisable.includes('AutoCompleteFieldDataEditorModel.optionsRoot')) }],
			multiAutoComplete: [{ value: this.multiAutoComplete, disabled: (disabled && !skipDisable.includes('AutoCompleteFieldDataEditorModel.multiAutoComplete')) }]
		});
		formGroup.addControl('autoCompleteOptions', this.autoCompleteOptions.buildForm(disabled, skipDisable));

		return formGroup;
	}

	fromModel(item: AutoCompleteFieldData): AutoCompleteFieldDataEditorModel {
		this.type = item.type;
		this.url = item.url;
		this.label = item.label;
		this.optionsRoot = item.optionsRoot;
		this.multiAutoComplete = item.multiAutoComplete;
		this.autoCompleteOptions = new FieldDataOptionEditorModel().fromModel(item.autoCompleteOptions);
		return this;
	}
}
