import { Component } from '@angular/core';
import { ViewController, NavParams,  ToastController } from 'ionic-angular';
import { Events } from 'ionic-angular';

import * as pnut from 'pnut-butter';

@Component({
  template: `
<ion-list>
  <button ion-item (click)="browse()">Open in browser</button>
  <button ion-item [disabled]="myUsername == username">Block</button>
  <button ion-item [disabled]="myUsername == username">Mute</button>
</ion-list>
`
})
export class ProfileMenu {

  private userid: string;
  private username: string;
  private you_muted: boolean;
  private you_blocked: boolean;
  private myUsername: string;

  constructor(public navParams: NavParams, public viewCtrl: ViewController,
    public toastCtrl: ToastController, public events: Events) {
      this.userid = this.navParams.data.userid;
      this.username = this.navParams.data.username;
      this.you_muted = this.navParams.data.you_muted;
      this.you_blocked = this.navParams.data.you_blocked;
      this.myUsername = this.navParams.data.me;
  }

  browse() {
    window.open('https://pnut.io/@' + this.username, '_system');
    this.close();
  }

  mute() {
    if (this.you_muted) {
      pnut.unmute(this.userid).then(res => {
        this.presentToast('User unmuted');
      });
    } else {
      pnut.mute(this.userid).then(res => {
        this.presentToast('User muted');
      });
    }
    this.close();
  }

  block() {
    if (this.you_blocked) {
      pnut.unblock(this.userid).then(res => {
        this.presentToast('User unblocked');
      });
    } else {
      pnut.block(this.userid).then(res => {
        this.presentToast('User blocked');
      });
    }
    this.close();
  }

  presentToast(text) {
    let toast = this.toastCtrl.create({
      position: 'top',
      message: text,
      duration: 2000
    });
    toast.present();
  }

  close() {
    this.viewCtrl.dismiss();
  }
}
