import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { Events } from 'ionic-angular';

/**
 * Generated class for the SettingsPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@Component({
  selector: 'page-settings',
  templateUrl: 'settings.html',
})
export class SettingsPage {

  private set_unified: boolean;
  private set_cc: boolean;
  private set_default: string;

  constructor(public navCtrl: NavController, private storage: Storage, public navParams: NavParams,
    public events: Events) {
  }

  ngAfterViewInit() {
    this.storage.get('timeline').then((val) => {
      this.set_default = val;
    }).catch(err => {
      console.log('ERROR: ' +err);
    });

    this.storage.get('unified').then((val) => {
      this.set_unified = val;
    }).catch(err => {
      console.log('ERROR: ' + err);
    });

    this.storage.get('cc').then((val) => {
      this.set_cc = val;
    }).catch(err => {
      console.log('ERROR: ' + err);
    });
  }

  ionViewDidLeave() {
    this.events.publish('stream:reload', {});
  }

  updateUnified() {
    this.storage.set('unified', this.set_unified);
  }

  updateCc() {
    this.storage.set('cc', this.set_cc);
  }

  updateDefault() {
    this.storage.set('timeline', this.set_default);
  }

}
