/*
 * Copyright (C) 2022 Robert Bosch GmbH Copyright (C) 2022 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */
import { AfterContentInit, Component, ElementRef, Input, OnChanges, Renderer2, ViewChild } from '@angular/core';

/**
 * @deprecated
 * Due to the deprecation of the BciFormFieldNotificationComponent.
 * No replacement
 */

export type NotificationType = 'info' | 'warning' | 'success' | 'error';
/**
 * @deprecated
 * 
 * This component is deprecated and won't be replaced with another component.
 * 
 * To have info, success, warning and error styled messages use mat-hint and mat-error
 * mat-hint replaces info messages
 * mat-hint with success property replaces success messages
 * mat-error with warning property replaces warning messages
 * mat-error replaces error messages
 */
@Component({
  selector: 'bci-form-field-notification',
  templateUrl: './bci-form-field-notification.html',
  styleUrls: ['./bci-form-field-notification.scss'],
})
export class BciFormFieldNotificationComponent implements AfterContentInit, OnChanges {
  /** The type of the callout */
  @Input() messageType: NotificationType = 'info';
  @Input() show = true;
  @ViewChild('bciFormField', {static: true}) bciFormFieldElement: ElementRef;

  constructor(private renderer: Renderer2) {}

  ngOnChanges(): void {
    this.renderer.setStyle(this.bciFormFieldElement.nativeElement, 'display', this.show ? '': 'none');    
  }

  // Changes to remove the previous notification wrapper
  ngAfterContentInit(): void {  
    if((this.bciFormFieldElement.nativeElement.parentNode.parentNode !== null) && !(this.bciFormFieldElement.nativeElement.parentNode.parentNode.childNodes[0].localName === 'bci-chip-list-autocomplete' || 
          this.bciFormFieldElement.nativeElement.parentNode.parentNode.classList.contains('bci-select'))) {
      this.setSuffixOrPrefixStyling();
    }
  }

  // Changes to add the new notification wrapper , add class bci-notification to the subscript wrapper
setSuffixOrPrefixStyling() {
    const formFieldNodes = this.bciFormFieldElement.nativeElement.parentNode.offsetParent.offsetParent.parentNode.childNodes;
    const subscriptwrapper = formFieldNodes[formFieldNodes.length-1]; 
    this.renderer.addClass(subscriptwrapper,'bci-notification');
    this.renderer.setStyle(this.bciFormFieldElement.nativeElement, 'display', this.show ? '': 'none');
    this.renderer.appendChild(subscriptwrapper, this.bciFormFieldElement.nativeElement);
  }
}
