import { Component, OnInit } from '@angular/core';
import { Raid, RaidData } from '../../../../../../backend/Types/Types';
import { NbDialogRef, DARK_THEME } from '@nebular/theme';
import { Tiers, _Tiers } from '../../../../../../backend/Types/Items';
import { IApiService } from '../../services/ApiService/ApiService';

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 = ""
  tier: Tiers = null 
  tiers = _Tiers

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

  loadNext() {
  }

  onTierSelect(){
  }

  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)
    this.tier = this.template.tier
  }

  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,
      tier: this.tier
    }
    const manage = this.api.get('manageRaid')
    if(!manage) return

    const dbraid = await manage.createRaid(raid)
    this.dialogRef.close()
  }
}
