import {Component, OnInit, TemplateRef} from '@angular/core';
import {ChangelogModel} from '../models/changelog.model';
import {ApiService} from '../services/api.service';
import {ModalService} from '@inst-iot/bosch-angular-ui-components';

@Component({
  selector: 'app-changelog',
  templateUrl: './changelog.component.html',
  styleUrls: ['./changelog.component.scss']
})
export class ChangelogComponent implements OnInit {

  timestamp = new Date();
  pageSize = 25;
  changelog: ChangelogModel[] = [];
  modalDetail = 0;

  constructor(
    private api: ApiService,
    private modal: ModalService
  ) { }

  ngOnInit(): void {
    this.loadChangelog();
  }

  loadChangelog(page = 0) {
    this.api.get<ChangelogModel[]>(`/changelog/${
      new Date(new Date(this.timestamp).getTime() - new Date(this.timestamp).getTimezoneOffset() * 60000).toISOString()
    }/${page}/${this.pageSize}`, data => {
      this.changelog = data.map(e => new ChangelogModel().deserialize(e));
      if (page) {
        this.timestamp = new Date(this.changelog[0].date);
      }
    });
  }

  showDetails(i: number, modal: TemplateRef<any>) {
    this.modalDetail = i;
    this.modal.open(modal).then(() => {});
  }

}
