import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ApiService as ApiService } from '../../services/login-api';
import { RaidData, Raid, Signup } from '../../../../../../backend/Types/Types';
import { NbWindowService, NbToastrService, NbDialogService } from '@nebular/theme';
import { FrontcraftCharacerpickerComponent } from './characterpicker.component';
import { getClassColor } from '../../../../../../backend/Types/PlayerSpecs';
import { FrontcraftBuyTokenComponent } from '../shop/buytoken.component';

@Component({
  selector: 'raid',
  templateUrl: './raid.component.html',
})
export class FrontcraftRaidComponent implements OnInit{

    canSignup = false
    isSignedup = false
    manageRaid
    mySignup

    raid: RaidData = <any>{
      participants:{
          Druid: [],
          Hunter: [],
          Mage: [],
          Paladin: [],
          Priest: [],
          Rogue: [],
          Shaman: [],
          Warlock: [],
          Warrior: [],
      },
      tokens:{}
    }
    
    constructor(
      private api: ApiService,
      private route: ActivatedRoute,
      private router: Router,
      private dialogService : NbDialogService,
      private toast: NbToastrService
    ){
      window['r'] = this
    }

    async ngOnInit(){
      this.manageRaid = this.api.get('manageRaid')
      
      const signupFeature = this.api.get('signup')
      if(signupFeature){
        this.canSignup = true
      }
      this.refresh()
    }

    itemSelect = async(item) => {
      this.dialogService.open(FrontcraftBuyTokenComponent, {
        context: {
          item: item,
          signup: this.mySignup
        }
      }).onClose.subscribe(() => this.refresh())
    }

    signup = async () => {
      const signupFeature = this.api.get('signup')
      if(!signupFeature) return

      this.dialogService.open(FrontcraftCharacerpickerComponent, { 
        closeOnBackdropClick: true,
        closeOnEsc: true,
        context: {
          raid: this.raid,
        } 
      }).onClose.subscribe(()=>{
        this.refresh()
      });
    }

    async archiveRaid(raid:Raid){
      await this.manageRaid!.archiveRaid(raid)
      this.toast.show('Raid archived', 'Success', { status: 'success' })
      this.router.navigateByUrl('/frontcraft/archive/'+raid.id)
    }

    async startRaid(raid:Raid){
      await this.manageRaid!.startRaid(raid)
      this.toast.show('Raid started', 'Success', { status: 'success' })
      this.router.navigateByUrl('/frontcraft/archive/'+raid.id)
    }

    unsign = async () => {
      const signupFeature = this.api.get('signup')
      if(!signupFeature) return

      await signupFeature.unsign(this.api.getAuth().token.value, <any>{id: this.mySignup.characterid, userid: this.mySignup.userid}, this.raid)
      this.toast.show('Success', 'Unsigned', { status: 'success' })
      this.refresh()
    }

    refresh = async () => {
      const param = this.route.snapshot.paramMap.get('id');

      const raidManager = this.api.get('RaidManager')
      const raiddata = await raidManager.getRaidData(<any>{
        id: param
      })
      this.raid = raiddata

      Object.values(raiddata.participants).flat().forEach(p => {
        p['color'] = getClassColor(p.class)
      })
      const user = this.api.getCurrentUser()
      const matchingSignup = Object.values(raiddata.participants).flat().find(char => user && char.userid === user.id!)
      if(matchingSignup){       

        this.isSignedup = true
        this.mySignup = matchingSignup
        this.mySignup.status = matchingSignup['benched']?'Bench':matchingSignup['late']?'Late':'Attending'
      }else{
        this.isSignedup = false
        this.mySignup = null
      }
    }

    setBench(signup:Signup){
      this.manageRaid.setBenched({
        characterid: signup.characterid,
        raidid: signup.raidid,
        late: false,
        benched: !signup.benched
      }).then(_ => this.refresh())
    }
}