import { Component, OnInit, ViewChild } from "@angular/core";
import { RouterLink, RouterModule } from "@angular/router";
import {
  MatPaginator,
  MatPaginatorModule,
  PageEvent,
} from "@angular/material/paginator";
import { MatTableDataSource, MatTableModule } from "@angular/material/table";
import { MatDialog } from "@angular/material/dialog";
import { TranslateModule, TranslateService } from "@ngx-translate/core";
import { CommonModule } from "@angular/common";
import { MatIconModule } from "@angular/material/icon";
import { MatProgressSpinnerModule } from "@angular/material/progress-spinner";
import { MatProgressBarModule } from "@angular/material/progress-bar";
import { MatButtonModule } from "@angular/material/button";
import { MatInputModule } from "@angular/material/input";
import { ReactiveFormsModule } from "@angular/forms";
import { MatFormFieldModule } from "@angular/material/form-field";

import { AddAction } from "../../models/add-action.enum";
import { Jump } from "../../models/jump";
import { JumpService } from "../../services/jump.service";
import { ServiceComm } from "../../services/service-comm.service";
import { JumpInfosComponent } from "../jump-infos/jump-infos.component";
import { StatsService } from "../../services/stats.service";

@Component({
  selector: "app-list-of-jumps",
  templateUrl: "./list-of-jumps.component.html",
  styleUrls: ["./list-of-jumps.component.css"],
  imports: [
    TranslateModule,
    CommonModule,
    RouterLink,
    RouterModule,
    MatIconModule,
    MatPaginatorModule,
    MatProgressSpinnerModule,
    MatProgressBarModule,
    MatTableModule,
    MatFormFieldModule,
    ReactiveFormsModule,
    MatInputModule,
    MatButtonModule,
  ],
})
export class ListOfJumpsComponent implements OnInit {
  public displayedColumns: Array<string> = [
    "infos",
    "id",
    "jumpDate",
    "jumpType",
    "aircraft",
    "dropZone",
    "actions",
  ];
  public dataSourceTable: MatTableDataSource<Jump> = new MatTableDataSource();
  @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  public isLoading: boolean = false;

  constructor(
    private serviceApi: JumpService,
    private serviceComm: ServiceComm,
    public dialog: MatDialog,
    private translateService: TranslateService,
    private statsService: StatsService
  ) {}

  ngAferViewInit(): void {
    this.dataSourceTable.paginator = this.paginator;
  }

  ngOnInit() {
    this.serviceComm.refreshRequest.subscribe((action) => {
      if (action === AddAction.Jump) {
        this.dialog.closeAll();
        this.getListOfJumps();
      }
    });
    this.serviceComm.forceTranslateTitle.subscribe((data) => {
      if (data === true) {
        this.updateTitle();
      }
    });

    this.updateTitle();
    this.getListOfJumps();
  }

  getListOfJumps(pageSize: number = 20, pageIndex: number = 0) {
    this.isLoading = true;

    this.serviceApi
      .getJumps(pageIndex * pageSize, pageIndex * pageSize + pageSize)
      .subscribe((data) => {
        this.dataSourceTable.data = data.rows;
        setTimeout(() => {
          this.paginator.length = data.count;
          this.paginator.pageIndex = pageIndex;
          this.isLoading = false;
        }, 500);
      });
  }

  openDialog(item: Jump, editMode: boolean) {
    this.dialog.open(JumpInfosComponent, {
      data: { jump: item, editMode: editMode },
      maxHeight: "400px",
      minWidth: "350px",
    });
  }

  delete(item: Jump) {
    let data: Array<Jump> = this.dataSourceTable.data;
    data = data.filter((d) => d.id !== item.id);

    this.dataSourceTable.data = data;

    this.serviceApi.deleteJump(item);
    this.statsService.resetStats();
  }

  pageChanged(event: PageEvent) {
    this.getListOfJumps(event.pageSize, event.pageIndex);
  }

  private updateTitle() {
    this.translateService.get("ListJumps_Title").subscribe((data) => {
      this.serviceComm.updatedComponentTitle(data);
    });
  }
}
