import { HttpErrorResponse, HttpHeaderResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { ReportBuilderService } from 'src/app/services/api/report-builder.service';

@Component({
  selector: 'app-queryadd',
  templateUrl: './queryadd.component.html',
  styleUrls: ['./queryadd.component.scss']
})
export class QueryaddComponent implements OnInit {
  public entryForm: FormGroup;
  selectedFile: File[] = [];
  type = ["MYSQL", "postgresql", "mysqllite", "oracle", "Snowflake", "BigQuery", "RedShift", "microsoft sql server", "redis", "maria_db", "MongoDB", "firebase", "dynamodb", "ibm DB2", "couchbase", "ElasticSearch", "Casandra", "OrientDB", "Neo4j", "FireBird"];
  constructor(private _fb: FormBuilder, private router: Router, private toastr: ToastrService,
    private route: ActivatedRoute, private reportBuilderService: ReportBuilderService,) { }

  ngOnInit(): void {
    this.entryForm = this._fb.group({
      name: [null],
      type: [null],
      description: [null],
      active: [null],
      driver_class_name: [null],
      conn_string: [null],
      username: [null],
      password: [null],
    });
  }
  public onFileChanged(event) {
    //Select File
    console.log(event);
    this.selectedFile = event.target.files[0];
    //for (var i = 0; i < event.target.files.length; i++) {
    // var name = event.target.files[i].name;
    // var type = event.target.files[i].type;
    // var size = event.target.files[i].size;
    // var modifiedDate = event.target.files[i].lastModifiedDate;
    // this.selectedFile.push(event.target.files[i]);
    //}
  }
  toastrShown: boolean = false;
  onSubmit() {
    console.log(this.entryForm.value);
    this.reportBuilderService.createdb(this.entryForm.value).subscribe((data) => {
      console.log(data);
      if (data) {
        if (!this.toastrShown) {
          this.toastrShown = true; // Set the flag to indicate that the toastr message has been shown
          this.toastr.success("Added Successfully");
          setTimeout(() => {
            // this.router.navigate(["../query"], { relativeTo: this.route });
            this.router.navigate([".."], { relativeTo: this.route });

          }, 500);
        }
      }
    }, (error: HttpErrorResponse) => {
      console.log(error);
      if (error.status === 202) {
        this.toastr.success("Added Succesfully");
      }
      if (error.status === 404) {
        this.toastr.error("Not Added");
      }
      if (error.status === 400) {
        this.toastr.error("Not Added");
      }
    });
    setTimeout(() => {
      // this.router.navigate(["../query"], { relativeTo: this.route });
      this.router.navigate([".."], { relativeTo: this.route });

    }, 500);
  }
  goback() {
    this.router.navigate(["../query"], { relativeTo: this.route });
  }

  // testConnection(){
  //   this.dataservice.testConnection(this.entryForm.value.data_source_type, this.entryForm.value.user_name, this.entryForm.value.password, this.entryForm.value.portnumber, this.entryForm.value.db_host_name).subscribe((data)=> {
  //     console.log(data);
  //     this.toastr.success('Test Connection successfully');
  //   },(err) => {
  //     console.log(err);
  //     this.toastr.error('Test Connection Failed');
  //   });
  // }

}
