import { Injectable, OnInit } from '@angular/core';
import { stringify } from 'querystring';
import { Tab1Page } from './tab1.page'
import { Discoteca } from '../discoteca'
import { Observable, of } from 'rxjs';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { NumericValueAccessor } from '@ionic/angular';
import { DiscotecaI } from '../interfaces/discoteca-i';
import { ApiService } from '../services/api.service';
import { Router } from '@angular/router';
import { Eventoi } from '../interfaces/eventoi';


@Injectable({
  providedIn: 'root'
})
export class Tab1Service implements OnInit{

  discoteca: Discoteca;
  discotecaI: DiscotecaI;
  galeria: string[];
  eventos: Eventoi[];
  eventoForms: FormGroup[];
  eventoIndex: number;
  editarEvento: boolean;

  constructor(private apiService: ApiService, private router: Router) { 
    
  }

  ngOnInit(){

  }

  initValues(): void{
    this.discoteca = new Discoteca();
    this.discoteca.setId(this.discotecaI.discotecaID);
    this.discoteca.setNombre(this.discotecaI.nombre);
    this.discoteca.setTelefono(this.discotecaI.telefono);
    this.discoteca.setLocalizacion(this.discotecaI.localizacion);
    this.initEventos();
    this.galeria = [];
    this.editarEvento = false;
  }

  getNombre(): string{
    return this.discoteca.getNombre();
  }

  getTelefono(): number{
    return this.discoteca.getTelefono();
  }

  getLocalizacion(): string{
    return this.discoteca.getLocalizacion();
  }

  getEventos(): Eventoi[]{
    return this.eventos;
  }

  getDescripcion(): string{
    return this.discoteca.getDescripcion();
  }

  initEventos(): void{
    this.eventos = [];
    this.apiService.getEventosDiscoteca(this.discoteca.getId())
    .subscribe(eventos => {
      this.eventos = eventos;
      this.router.navigate(['/tabs/tab1/perfil-discoteca']);
    })
    
    
  }

  initEventoForms(): void{
    this.eventoForms = [];
    if (this.eventos){
      this.eventos.forEach(evento => {
        let thisForm = new FormGroup({
          nombre: new FormControl(null, Validators.required),
          fecha: new FormControl(null, Validators.required),
          hora: new FormControl(null, Validators.required),
          precio1: new FormControl(null, Validators.required),
          precio2: new FormControl(null, Validators.required),
          descripcion: new FormControl(null, Validators.required),
      });
      thisForm.patchValue(evento);
      this.eventoForms.push(thisForm);
      })
    }
  }

  getEventobyIndex(eventoIndex: number){
    return this.eventos[eventoIndex];
  }

getDiscoteca(discotecaId: number){
    if (discotecaId != 0){
    this.apiService.getUserDiscoteca(discotecaId)
    .subscribe(discoteca => {
      this.discotecaI = discoteca[0];
      console.log(this.discotecaI);
      this.initValues();
    })
  }
 
  }

  postEvento(evento: Eventoi){
    this.apiService.postEvento(evento)
    .subscribe(evento => {
      this.initEventos();
    })

  }

  updateEvento(evento: Eventoi){
    
  }



}
