import { Component, OnInit } from '@angular/core';
import { DefaultApi } from '../../rest-api';
import { QueryService, MessageService, UserService } from '../../api';

import gql from 'graphql-tag';

const Query = gql`
query Mage($id:Int!) {
  Mage(id:$id) {
    id
    guild {
      id
      enemies {
        mage {
          id
          name
          color
          rank
          land
          netPower
          status
          buildings {
            fortress {
              amount
            }
          }
          guild {
            id
            name
          }
        }
        comment
      }
    }
  }
}
`;

@Component({
  selector: 'app-guild-enemy-list',
  templateUrl: './guild-enemy-list.component.html',
  styleUrls: ['./guild-enemy-list.component.css']
})
export class GuildEnemyListComponent implements OnInit {
  canEdit = true;

  enemies: any;

  constructor(
    private api: DefaultApi,
    private queryService: QueryService,
    private messageService: MessageService,
    private userService: UserService) {
    userService.guildPermissions$.subscribe(permissions => {
      this.canEdit = permissions.includes('EDIT_MAGE_LIST');
    });
  }

  ngOnInit() {
    this.executeQuery();
  }

  executeQuery() {
    this.queryService.queryAndUpdateMage(Query).subscribe((mage) => {
      if (mage.guild && mage.guild.enemies) 

    });
  }

  setComment(mageId: number, comment: string) {
    const parameter = { mageId: mageId, comment: comment };
    this.api.updateGuildMageComment(this.userService.mageId, parameter).subscribe(message => {
      this.messageService.success(message);
      this.executeQuery();
    });
  }
}

{
        this.enemies = mage.guild.enemies;
      }