import { Component, Input, OnInit } from '@angular/core';
import { FormArray, FormControl, FormGroup } from '@angular/forms';
import { DatasetProfileFieldViewStyle } from '../../../../../../core/common/enum/dataset-profile-field-view-style';
import { DatasetProfileComboBoxType } from '../../../../../../core/common/enum/dataset-profile-combo-box-type';
import { ToCEntryType } from '../../../table-of-contents/table-of-contents-entry';

@Component({
	selector: 'app-dataset-profile-editor-rule-component',
	templateUrl: './dataset-profile-editor-rule.component.html',
	styleUrls: ['./dataset-profile-editor-rule.component.scss']
})

export class DatasetProfileEditorRuleComponent implements OnInit{
	
	@Input() form: FormArray;
	
	@Input() viewStyleForCheck: DatasetProfileFieldViewStyle;
	@Input() formControlForCheck: FormControl;
	@Input() formArrayOptionsForCheck: FormArray;
	@Input() comboBoxTypeForCheck: DatasetProfileComboBoxType;
	@Input() viewOnly: boolean;
	
	targetValidation() {
		//TODO
	}
	
	deleteRule(index) {
		this.form.removeAt(index);
	}

	ngOnInit(): void {
		this.options = this.getOptions();
		// TODO TO SEE IF THE COMPONENT IS NOT DESTROYED AND THE FORM UPDATES

		this.sectionOptions = [];
		this.fieldOptions = [];
		this.fieldSetOptions = [];
		this.options.forEach(option=>{
			switch (option.type) {
				case ToCEntryType.Field:
					this.fieldOptions.push(option);
					break;
				case ToCEntryType.FieldSet:
					this.fieldSetOptions.push(option);
					break;
				case ToCEntryType.Section:
					this.sectionOptions.push(option);
					break;
				default:
					break;
			}
		});

	}
	options: OptionItem[];
	sectionOptions: OptionItem[];
	fieldSetOptions: OptionItem[];
	fieldOptions: OptionItem[];


	getOptions():OptionItem[]{
		const rootForm = this.form.root;
		if(rootForm){
			// const parentSections = rootForm.get('sections') as FormArray;

			const result:OptionItem[] =[];

			const sections = rootForm.get('sections') as FormArray;

			if(sections){
				sections.controls.forEach(section=>{
					const subResult = this.buildOptions(section as FormGroup, ToCEntryType.Section,[]);
					result.push(...subResult);
				});
			}

			//return options
			return result;

		}
		//nothing found
		return [];
	}

	private buildOptions(form: FormGroup, type: ToCEntryType, parentIds:string[]):OptionItem[]{
		
		const sections = form.get('sections') as FormArray;
		const fieldSets =  form.get('fieldSets') as FormArray;
		const fields = form.get('fields') as FormArray;

		const result:OptionItem[] = []; 

		// parentIds.push(form.get('id').value);
		const currentOptionItem:OptionItem = {
			id: form.get('id').value,
			type: type,
			label: type ===ToCEntryType.Field? form.get('data').get('label').value :form.get('title').value,
			// parentsIds: [form.get('id').value]
			parentsIds: [...parentIds, form.get('id').value]
		}
		result.push(currentOptionItem);

		if(sections){
			sections.controls.forEach(section=>{
				result.push( ...this.buildOptions(section as FormGroup, ToCEntryType.Section, currentOptionItem.parentsIds) );
			});
		}
		if(fieldSets){
			fieldSets.controls.forEach(fieldset=>{
				result.push( ...this.buildOptions(fieldset as FormGroup, ToCEntryType.FieldSet, currentOptionItem.parentsIds) );
			});
		}
		if(fields){
			fields.controls.forEach(field=>{
				result.push( ...this.buildOptions(field as FormGroup, ToCEntryType.Field, currentOptionItem.parentsIds) ); //TODO NA TO DOUME
			});
		}

		return result;
	}

	get parentIds(): string[]{

		if(!this.formControlForCheck.get('id')) return [];

		const current = this.options.find(opt=> opt.id === this.formControlForCheck.get('id').value);

		if(current){
			return current.parentsIds;
		}
		return [];
	}

}


interface OptionItem{
	id: string,
	label: string,
	type: ToCEntryType,
	parentsIds: string[]
}