import { Component, OnInit } from '@angular/core';
import { ApiService } from '../../services/login-api';
import { Raid, RaidData } from '../../../../../../backend/Types/Types';
import { NbWindowRef, NbDialogRef } from '@nebular/theme';

const ONE_MINUTE = 60000
const ONE_HOUR = 60 * ONE_MINUTE

@Component({
  selector: 'createraid',
  templateUrl: 'createraid.component.html',
})
export class FrontcraftCreateRaidsComponent implements OnInit {

  templates :RaidData[]
  template :RaidData

  title = ""
  startdate = new Date()
  hour = 20
  minute = 0
  size = 40
  description = ""

  constructor(
    protected dialogRef: NbDialogRef<FrontcraftCreateRaidsComponent>,
    private api: ApiService
  ) {}

  loadNext(cardData) {
  }

  onTemplateSelect(){
    const templateDate = new Date(this.template.start)
    this.title = this.template.title
    this.hour = templateDate.getHours()
    this.minute = templateDate.getMinutes()
    this.description = this.template.description
    this.startdate = new Date(parseInt(this.template.start) - templateDate.getHours()*ONE_HOUR - templateDate.getMinutes()*ONE_MINUTE)
  }

  ngOnInit(){

  }

  async submit(){
    const raid = <Raid>{
      description: this.description,
      size: this.size,
      start: ""+new Date(this.startdate.getTime() + this.hour*ONE_HOUR + this.minute*ONE_MINUTE).getTime(),
      title: this.title,
    }
    const manage = this.api.get('manageRaid')
    if(!manage) return

    await manage.createRaid(raid)
    this.dialogRef.close()
  }
}
