import { Component, Input, ChangeDetectorRef } from "@angular/core";
import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
import { NavController } from "ionic-angular";
import { WriteTweetPage } from "../../pages/write-tweet/write-tweet";

/**
 * Generated class for the TweetActionsComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: "tweet-actions",
  templateUrl: "tweet-actions.html"
})
export class TweetActionsComponent {
  @Input()
  data: any[];

  constructor(
    private twitter: TwitterApiProvider,
    private ref: ChangeDetectorRef,
    private navCtrl: NavController
  ) {}

  get favoriteCount() {
    if (this.data["retweeted_status"]) {
      return this.data["retweeted_status"]["favorite_count"];
    } else {
      return this.data["favorite_count"];
    }
  }

  get id() {
    if (this.data["retweeted_status"]) {
      return this.data["retweeted_status"]["id_str"];
    } else {
      return this.data["id_str"];
    }
  }

  like(id: string): void {
    if (!this.data["private_tweet"]) {
      this.twitter.likeTweet(id).then(() => {
        this.data["favorited"] = true;
        this.data["favorite_count"]++;
        this.ref.detectChanges();
      });
    }
  }

  removeLike(id: string): void {
    if (!this.data["private_tweet"]) {
      this.twitter.unlikeTweet(id).then(() => {
        this.data["favorited"] = false;
        this.data["favorite_count"]--;
        this.ref.detectChanges();
      });
    }
  }

  retweetStatus(id: string): void {
    this.navCtrl.push(WriteTweetPage, { tweetId: id });
  }
}
