import { Component } from "@angular/core";
import {
  IonicPage,
  NavController,
  MenuController,
  InfiniteScroll,
  Refresher,
  LoadingController
} from "ionic-angular";
import { Storage } from "@ionic/storage";
import { WriteTweetPage } from "../write-tweet/write-tweet";
import { FeedProvider } from "../../providers/feed/feed";
import { MockProvider } from "../../providers/mock-provider/mock-provider";

@IonicPage()
@Component({
  selector: "page-home",
  templateUrl: "home.html"
})
export class HomePage {
  menuController: MenuController;
  tweets;
  mock;

  constructor(
    public navCtrl: NavController,
    private menuCtrl: MenuController,
    private feed: FeedProvider,
    private mockProv: MockProvider,
    private storage: Storage,
    private loadingCtrl: LoadingController
  ) {}

  ionViewDidLoad() {
    this.menuCtrl.enable(true, "sideNav");
  }

  async ionViewDidEnter() {
    const loading = this.loadingCtrl.create();
    loading.present();
    this.mock = await this.storage.get("mockup");
    console.log('mockup?', this.mock);

    if (this.mock) {
      this.mockProv
        .loadHomeTimeline()
        .then(tweets => {
          console.log("HOME tweets are: ", tweets);
          this.tweets = tweets;
          loading.dismiss();
        })
        .catch(err => console.error('error in timeline', err));
      // console.log('tweets from htl are:',this.tweets);

    } else {
      console.log('entered ionic view');
      this.feed
        .loadHomeTimeline()
        .then(tweets => {
          this.tweets = tweets;
          loading.dismiss();
        })
        .catch(err => console.error(err));

    }
    // loading.dismiss();
  }

  doRefresh(refresher: Refresher) {
    if (this.mock) {
      console.log('mock refresher called',refresher);
      this.mockProv.loadHomeTimeline().then(tweets => {
        this.tweets = tweets;
        refresher.complete();
      });
    } else {
      this.feed.loadHomeTimeline().then(tweets => {
        this.tweets = tweets;
        refresher.complete();
      });
    }
  }

  loadMore(infiniteScroll: InfiniteScroll) {
    this.feed
      .loadHomeTimeline(this.oldestPublicTweet, this.oldestPrivateTweet)
      .then(tweets => {
        this.tweets = this.tweets.concat(tweets);
        infiniteScroll.complete();
      });
  }

  writeTweet() {
    console.log('Writing the tweet');
    this.navCtrl.push(WriteTweetPage);
  }

  get publicTweets() {
    return this.tweets.filter(tweet => !tweet.private_tweet);
  }

  get privateTweets() {
    return this.tweets.filter(tweet => tweet.private_tweet);
    // return null;
  }

  get oldestPublicTweet() {
    // return null;

    if (this.publicTweets.length > 0) {
      return this.publicTweets.reduce((acc, cur) =>
        acc.id < cur.id ? acc : cur
      );
    } else {
      return undefined;
    }
  }

  get oldestPrivateTweet() {
    if (this.privateTweets.length > 0) {
      return this.privateTweets.reduce((acc, cur) =>
        new Date(acc.created_at) < new Date(cur.created_at) ? acc : cur
      );
    } else {
      return undefined;
    }
  }
}
