import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { MatSnackBar } from '@angular/material/snack-bar';
import * as reserved_words from 'src/app/components/resources/json/reserved_words.json';

@Component({
  selector: 'app-getbfo-textarea',
  templateUrl: './getbfo-textarea.component.html',
  styleUrls: ['./getbfo-textarea.component.css']
})
export class GETBFOTEXTAREAComponent implements OnInit {

  public formGroup: FormGroup;
  private heightStand: number = 10;

  public num_cols: number;
  public arr_columns: Array<string>;

  constructor(
    public dialogRef: MatDialogRef<GETBFOTEXTAREAComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any
  ) {
    this.formGroup = this.createFormGroup();
    this.num_cols = data.num_cols;
    this.arr_columns = data.column;
  }

  ngOnInit(): void {
  }

  private createFormGroup() {
    return new FormGroup({
      tag: new FormControl('textarea', Validators.required),
      column: new FormControl('', Validators.required),
      name: new FormControl('', Validators.required),
      id: new FormControl('', [Validators.required, this.applyFilterControl]),
      type: new FormControl('text', Validators.required),
      placeholder: new FormControl(''),
      label: new FormControl('', Validators.required),
      max: new FormControl(45, Validators.required),
      value: new FormControl(''),
      pattern: new FormControl(''),
      msgerror: new FormControl({ value: '', disabled: true}),
      icon: new FormControl(''),
      disabled_reg: new FormControl(false),
      disabled_mod: new FormControl(false),
      required_reg: new FormControl(false),
      required_mod: new FormControl(false),
      readonly_reg: new FormControl(false),
      readonly_mod: new FormControl(false),
      num_col: new FormControl(1, Validators.required),
      num_row: new FormControl(1, Validators.required),
      height: new FormControl(this.heightStand, Validators.required),
      deleted: new FormControl(true),
    });
  }

  private applyFilterControl(control: any) {
    const filterValue = control.value;
    let arrWords = reserved_words;
    let filter = arrWords.reserved_words.filter((element: string) => element == filterValue.trim().toUpperCase());
    if (filter.length > 0) {
      return {
        validWords: {
          word: filterValue
        }
      }
    }
    return null;
  }

  public numColumn(type: string) {
    if (type == 'add') {
      if (this.num_col?.value + 1 <= this.num_cols) {
        this.num_col?.setValue(this.num_col?.value + 1);
      }
    } else {
      if (this.num_col?.value - 1 >= 1) {
        this.num_col?.setValue(this.num_col?.value - 1);
      }
    }
  }

  public numRows(type: string) {
    if (type == 'add') {
      if (this.num_row?.value + 1 <= 3) {
        this.height?.setValue(this.height.value + 70);

        this.num_row?.setValue(this.num_row?.value + 1);
      }
    } else {
      if (this.num_row?.value - 1 >= 1) {
        this.height?.setValue(this.height.value - 70);

        this.num_row?.setValue(this.num_row?.value - 1);
      }
    }
  }

  public changeColumn() {
    let column = this.column?.value;
    this.id?.setValue(column);
    this.name?.setValue(column);
  }

  public validPattern(textPattern:string) {
    if (textPattern === '') {
      this.msgerror?.disable();
      this.msgerror?.clearValidators();
    }else{
      this.msgerror?.enable();
      this.msgerror?.setValidators(Validators.required);
      this.msgerror?.setValue('');
    }
  }

  get tag() {
    return this.formGroup.get('tag');
  }
  get name() {
    return this.formGroup.get('name');
  }
  get column() {
    return this.formGroup.get('column');
  }
  get id() {
    return this.formGroup.get('id');
  }
  get type() {
    return this.formGroup.get('type');
  }
  get placeholder() {
    return this.formGroup.get('placeholder');
  }
  get label() {
    return this.formGroup.get('label');
  }
  get max() {
    return this.formGroup.get('max');
  }
  get value() {
    return this.formGroup.get('value');
  }
  get rows() {
    return this.formGroup.get('rows');
  }
  get pattern() {
    return this.formGroup.get('pattern');
  }
  get msgerror() {
    return this.formGroup.get('msgerror');
  }
  get icon() {
    return this.formGroup.get('icon');
  }
  get disabled_reg() {
    return this.formGroup.get('disabled_reg');
  }
  get disabled_mod() {
    return this.formGroup.get('disabled_mod');
  }
  get required_reg() {
    return this.formGroup.get('required_reg');
  }
  get required_mod() {
    return this.formGroup.get('required_mod');
  }
  get readonly_reg() {
    return this.formGroup.get('readonly_reg');
  }
  get readonly_mod() {
    return this.formGroup.get('readonly_mod');
  }
  get num_col() {
    return this.formGroup.get('num_col');
  }
  get num_row() {
    return this.formGroup.get('num_row');
  }
  get height() {
    return this.formGroup.get('height');
  }

}
