import { Component, Input } from '@angular/core';
import { NavController, NavParams, ModalController, ToastController, PopoverController } from 'ionic-angular';
import { ProfilePage } from '../../pages/profile/profile';
import { ThreadPage } from '../../pages/thread/thread';
import { LoginPage } from '../../pages/login/login';
import { NewPostModal } from '../../pages/stream/new-post';
import { PostMenu } from '../../pages/stream/post-menu';

import * as pnut from 'pnut-butter';

/**
 * Generated class for the PostComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'post',
  templateUrl: 'post.html'
})
export class PostComponent {

  @Input() public post: Object;
  @Input() public hideImg: boolean;
  @Input() public ccOnReply: boolean;
  @Input() public myUsername: string;

  constructor(public navCtrl: NavController, public navParams: NavParams,
    public modalCtrl: ModalController, public toastCtrl: ToastController,
    public popoverCtrl: PopoverController) {}

  fetchThread(threadid) {
    pnut.thread(threadid, {include_deleted: 0, include_raw: 1, count: 140}).then(res => {
      if (res.meta.code === 401) {
        // this.storage.clear();
        this.navCtrl.setRoot(LoginPage);
      } else {
        this.navCtrl.push(ThreadPage, {posts: res.data, me: this.myUsername});
      }
    });
  }

  showImage(url) {
    window.open(url, '_system');
  }

  showProfile(user) {
    this.navCtrl.push(ProfilePage, {user: user, me: this.myUsername});
  }

  showReplyPost(postData, repType) {
    let newPostModal = this.modalCtrl.create(NewPostModal, {
      type: repType,
      post: postData,
      me: this.myUsername,
      cc: this.ccOnReply});
    newPostModal.present();
  }

  repost(postid, reposted) {
    if (reposted) {
      pnut.deleteRepost(postid).then(res => {
        this.updatePost(res.data.id);
        this.presentToast("Repost updated.");
      }).catch(err => {
        console.log(err);
      });
    } else {
      pnut.repost(postid).then(res => {
        this.updatePost(res.data.id);
        this.presentToast("Repost updated.");
      }).catch(err => {
        console.log(err);
      });
    }
  }

  bookmark(postid, bookmarked) {
    if (bookmarked) {
      pnut.deleteBookmark(postid).then(res => {
        this.updatePost(res.data.id);
        this.presentToast("Bookmark updated.");
      }).catch(err => {
        console.log(err);
      });
    } else {
      pnut.bookmark(postid).then(res => {
        this.updatePost(res.data.id);
        this.presentToast("Bookmark updated.");
      }).catch(err => {
        console.log(err);
      });
    }
  }

  updatePost(postid) {
    pnut.post(postid, {include_raw: 1}).then(res => {
      this.post = res.data;
    }).catch(err => {
      console.log(err);
    });
  }

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

  presentPostMenu(myEvent, postData) {
    let popover = this.popoverCtrl.create(PostMenu, {
      post: postData,
      me: this.myUsername});
    popover.present({ev: myEvent});
  }

}
