import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input } from '@angular/core';
import { FormArray, FormGroup } from '@angular/forms';
import { BaseComponent } from '@common/base/base.component';
import { takeUntil } from 'rxjs/operators';
import { ToCEntry } from '../../dataset-description.component';
import { VisibilityRulesService } from '../../visibility-rules/visibility-rules.service';

@Component({
	selector: 'app-form-composite-field',
	templateUrl: './form-composite-field.component.html',
	styleUrls: ['./form-composite-field.component.scss'],
	changeDetection: ChangeDetectionStrategy.OnPush
})
export class FormCompositeFieldComponent extends BaseComponent {

	@Input() datasetProfileId: String;
	@Input() form: FormGroup;
	@Input() isChild: Boolean = false;
	@Input() showDelete: Boolean = false;
	@Input() tocentry: ToCEntry;

	constructor(
		public visibilityRulesService: VisibilityRulesService,
		private changeDetector: ChangeDetectorRef
		//private markForConsiderationService: MarkForConsiderationService,
	) {
		super();
		this.visibilityRulesService.getElementVisibilityMapObservable().pipe(takeUntil(this._destroyed)).subscribe(x => {
			this.changeDetector.markForCheck();
		});
	}

	ngOnInit() {

		if (this.tocentry) {
			this.form = this.tocentry.form as FormGroup;
		}
	}

	// addMultipleField(fieldIndex: number) {
	// 	const compositeFieldToBeCloned = (this.form.get('compositeFields').get('' + fieldIndex) as FormGroup).getRawValue();
	// 	const compositeField: DatasetDescriptionCompositeFieldEditorModel = new DatasetDescriptionCompositeFieldEditorModel().cloneForMultiplicity(compositeFieldToBeCloned);
	// 	(<FormArray>(this.form.get('compositeFields').get('' + fieldIndex).get('multiplicityItems'))).push(compositeField.buildForm());
	// }

	// markForConsideration() {
	// 	this.markForConsiderationService.markForConsideration(this.compositeField);
	// }

	deleteCompositeField() {
		if (this.isChild) {
			this.deleteMultipeFieldFromCompositeFormGroup();
		} else {
			this.deleteCompositeFieldFormGroup();
		}
	}

	deleteCompositeFieldFormGroup() {

		const compositeFieldId = ((this.form.get('multiplicityItems') as FormArray).get('' + 0) as FormGroup).getRawValue().id;
		const fieldIds = (this.form.get('fields') as FormArray).controls.map(control => control.get('id').value) as string[];

		const numberOfItems = this.form.get('multiplicityItems').get('' + 0).get('fields').value.length;
		for (let i = 0; i < numberOfItems; i++) {
			const multiplicityItem = this.form.get('multiplicityItems').get('' + 0).get('fields').get('' + i).value;
			this.form.get('fields').get('' + i).patchValue(multiplicityItem);
		}
		(<FormArray>(this.form.get('multiplicityItems'))).removeAt(0);


		this.visibilityRulesService.removeAllIdReferences(compositeFieldId);
		fieldIds.forEach(x => this.visibilityRulesService.removeAllIdReferences(x));
	}

	deleteMultipeFieldFromCompositeFormGroup() {
		const parent = this.form.parent;
		const index = (parent as FormArray).controls.indexOf(this.form);

		const currentId = this.form.get('id').value;
		const fieldIds = (this.form.get('fields') as FormArray).controls.map(control => control.get('id').value) as string[];


		this.visibilityRulesService.removeAllIdReferences(currentId);
		fieldIds.forEach(x => this.visibilityRulesService.removeAllIdReferences(x));

		(parent as FormArray).removeAt(index);
		(parent as FormArray).controls.forEach((control, i) => {
			try {
				control.get('ordinal').setValue(i);
			} catch {
				throw 'Could not find ordinal';
			}
		});
		// (<FormArray>(this.form as AbstractControl)).removeAt(fildIndex);
	}
}
