import { Injectable } from '@angular/core';
import { BehaviorSubject, Subject, Observable } from 'rxjs';
import { environments } from '../../../../environments/environments';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class EnviarInfoService {

  private URL: string = environments.baseUrl;

  constructor(private _http: HttpClient) { }

  private getHeaders(): HttpHeaders {
    const token = localStorage.getItem('token') || '';
    return new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    });
  }

  private mensajeBienvenida = new BehaviorSubject<string>('');
  private nombreColegio = new BehaviorSubject<string>('');
  private color = new BehaviorSubject<string>('');
  private textColor = new BehaviorSubject<string>('');
  private link = new BehaviorSubject<string>('');

  currentMensaje = this.mensajeBienvenida.asObservable();
  currentColor = this.color.asObservable();
  currentTextColor = this.textColor.asObservable();
  currentLink = this.link.asObservable();
  currentColegio = this.nombreColegio.asObservable();

  private tablaSource = new Subject<void>();
  tabla$ = this.tablaSource.asObservable();

  notifyCambioTabla() {
    this.tablaSource.next();
  }

  changeNombreColegio(name: string) {
    this.nombreColegio.next(name);
  }

  changeMessage(mensaje: string) {
    this.mensajeBienvenida.next(mensaje);
  }

  changeColor(color: string) {
    this.color.next(color);
  }

  changeTextColor(textColor: string) {
    this.textColor.next(textColor);
  }

  changeColorLinks(links: string) {
    this.link.next(links);
  }

  getNombreColegio(): Observable<any> {
    return this._http.get(`${this.URL}/personalizacion/nombre-colegio`, {
      headers: this.getHeaders()
    });
  }

   getEsloganColegio(): Observable<any> {
    return this._http.get(`${this.URL}/personalizacion/eslogan-colegio`, {
      headers: this.getHeaders()
    });
  }

}
