
import { Component, Input, OnInit } from '@angular/core';
import { FormArray, FormControl, FormGroup } from '@angular/forms';
import { DatasetProfileFieldViewStyle } from '@app/core/common/enum/dataset-profile-field-view-style';
import { ValidationType } from '@app/core/common/enum/validation-type';
import { DatasetProfileService } from '@app/core/services/dataset-profile/dataset-profile.service';
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
import { BooleanDecisionFieldDataEditorModel } from '@app/ui/admin/dataset-profile/admin/field-data/boolean-decision-field-data-editor-model';
import { CheckBoxFieldDataEditorModel } from '@app/ui/admin/dataset-profile/admin/field-data/check-box-field-data-editor-model';
import { DatePickerDataEditorModel } from '@app/ui/admin/dataset-profile/admin/field-data/date-picker-data-editor-models';
import { FreeTextFieldDataEditorModel } from '@app/ui/admin/dataset-profile/admin/field-data/free-text-field-data-editor-model';
import { RadioBoxFieldDataEditorModel } from '@app/ui/admin/dataset-profile/admin/field-data/radio-box-field-data-editor-model';
import { ResearchersAutoCompleteFieldDataEditorModel } from '@app/ui/admin/dataset-profile/admin/field-data/researchers-auto-complete-field-data-editor-model';
import { TextAreaFieldDataEditorModel } from '@app/ui/admin/dataset-profile/admin/field-data/text-area-field-data-editor-model';
import { WordListFieldDataEditorModel } from '@app/ui/admin/dataset-profile/admin/field-data/word-list-field-data-editor-model';
import { RuleEditorModel } from '@app/ui/admin/dataset-profile/admin/rule-editor-model';
import { BaseComponent } from '@common/base/base.component';
import { takeUntil } from 'rxjs/operators';

@Component({
	selector: 'app-dataset-profile-editor-field-component',
	templateUrl: './dataset-profile-editor-field.component.html',
	styleUrls: ['./dataset-profile-editor-field.component.scss']
})
export class DatasetProfileEditorFieldComponent extends BaseComponent implements OnInit {
	@Input() viewOnly: boolean;
	@Input() form: FormGroup;
	@Input() showOrdinal = true;
	@Input() indexPath: string;
	validationTypeEnum = ValidationType;
	viewStyleEnum = DatasetProfileFieldViewStyle;
	isFieldMultiplicityEnabled = false;


	constructor(
		public enumUtils: EnumUtils,
		public datasetProfileService: DatasetProfileService
	) { super(); }

	ngOnInit() {
		if (this.form.get('multiplicity')) {
			if (this.form.get('multiplicity').value.min > 1 && this.form.get('multiplicity').value.max > 1) {
				this.isFieldMultiplicityEnabled = true;
			}
		}
		// this.addNewRule();

		this.form.get('viewStyle').get('renderStyle').valueChanges
			.pipe(takeUntil(this._destroyed))
			.subscribe(x => {
				if (this.form.get('data')) {
					this.form.removeControl('data');

					switch (x) {
						case DatasetProfileFieldViewStyle.BooleanDecision:
							this.form.addControl('data', new BooleanDecisionFieldDataEditorModel().buildForm());
							break;
						case DatasetProfileFieldViewStyle.CheckBox:
							this.form.addControl('data', new CheckBoxFieldDataEditorModel().buildForm());
							break;
						case DatasetProfileFieldViewStyle.ComboBox:
							this.form.addControl('data', new WordListFieldDataEditorModel().buildForm());
							break;
						case DatasetProfileFieldViewStyle.InternalDmpEntities:
							this.form.addControl('data', new ResearchersAutoCompleteFieldDataEditorModel().buildForm());
							break;
						case DatasetProfileFieldViewStyle.FreeText:
							this.form.addControl('data', new FreeTextFieldDataEditorModel().buildForm());
							break;
						case DatasetProfileFieldViewStyle.RadioBox:
							this.form.addControl('data', new RadioBoxFieldDataEditorModel().buildForm());
							break;
						case DatasetProfileFieldViewStyle.TextArea:
							this.form.addControl('data', new TextAreaFieldDataEditorModel().buildForm());
							break;
						case DatasetProfileFieldViewStyle.DatePicker:
							this.form.addControl('data', new DatePickerDataEditorModel().buildForm());
							break;
					}
				}
			});
	}

	defaulValueRequired(viewStile: DatasetProfileFieldViewStyle): boolean {
		switch (viewStile) {
			case DatasetProfileFieldViewStyle.CheckBox:
				return true;
			case DatasetProfileFieldViewStyle.RadioBox:
			case DatasetProfileFieldViewStyle.TextArea:
			case DatasetProfileFieldViewStyle.FreeText:
			case DatasetProfileFieldViewStyle.ComboBox:
			case DatasetProfileFieldViewStyle.InternalDmpEntities:
			case DatasetProfileFieldViewStyle.BooleanDecision:
			case DatasetProfileFieldViewStyle.DatePicker:
				return false;
			default:
				return false;
		}
	}



	onIsFieldMultiplicityEnabledChange(isFieldMultiplicityEnabled: boolean) {
		if (!isFieldMultiplicityEnabled) {
			(<FormControl>this.form.get('multiplicity').get('min')).setValue(0);
			(<FormControl>this.form.get('multiplicity').get('max')).setValue(0);
		}
	}

	addNewRule() {
		const rule: RuleEditorModel = new RuleEditorModel();
		(<FormArray>this.form.get('visible').get('rules')).push(rule.buildForm());
	}
}
