import { Component } from '@angular/core';
import { NavController, ModalController } from 'ionic-angular';
import { HealthProvider } from '../../providers/health/health';
import { ProductCatDetailPage } from '../product-cat-detail/product-cat-detail';
import { NativeStorage } from '@ionic-native/native-storage';

//import { Health } from '@ionic-native/health';
import { Device } from '@ionic-native/device';
import { DetailPage } from '../detail/detail';

@Component({
  selector: 'page-about',
  templateUrl: 'about.html'
})
export class AboutPage {
  token: string;
  jo;
  jo2;
  catPage = ProductCatDetailPage;
  products;
  imageURL;

  constructor(public navCtrl: NavController, private nativeStorage: NativeStorage, private hp: HealthProvider, private device: Device, private vc: ModalController) {
    this.imageURL = this.hp.imageURL;
  }
  ionViewDidEnter() {
    console.log("Enter enter ");
    console.log(this.device.uuid, this.device.isVirtual);
    this.nativeStorage.getItem('token')
      .then(
        data => { 
          this.token = data;
          this.hp.myActivities(this.token)
            .subscribe(acts => console.dir(acts.output.firstname, acts.output.lastname));
        },
        error => console.error(error)
      );

    this.hp.getProducts()
      .subscribe(data => { 
        this.products = data.output;
        console.log("proudcts = ", this.products);
      }, 
        error => { console.log("error ", JSON.stringify(error));  } );
    /*
		this.health.isAvailable()
			.then((available:boolean) => {
				console.log(available);
				this.health.requestAuthorization([
          //'distance', 'nutrition','height', 'weight', 'activity', 'workouts' //read and write permissions
          {
						read: ['steps', 'workouts', 'heart_rate', 'activity'],       //read only permission
						write: ['height', 'weight']  //write only permission
          }
				])
          .then(res => { 
            console.log(JSON.stringify(res));
            this.health.query({
                startDate: new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000), // three days ago
                endDate: new Date(), // now
              dataType: 'steps',
              limit: 100
            })
            .then(
              q => { 
                //console.log(JSON.stringify(q));
                this.jo = q;
              })
            .catch( e => console.log("error", e));
            this.health.queryAggregated({
                startDate: new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000), // three days ago
                endDate: new Date(), // now
              dataType: 'workouts',
              bucket: 'day'
            })
            .then(
              q => { 
                //console.log(JSON.stringify(q));
                console.log("hr ", JSON.stringify(q));
                this.jo2 = q;
              })
            .catch( e => console.log("error", e));
          })
					.catch(e => console.log(e));
			})
      .catch(e => console.log(e));*/
  }
 
  openNavDetailsPage(item) {
    let modal = this.vc.create(DetailPage, {id: item});
    modal.present();
    //this.navCtrl.push(DetailPage, {  id: item });
  }
    refreshPromo(refresher) {
        this.hp.getProducts()
            .subscribe(data => { 
                this.products = data.output;
                console.log("proudcts = ", this.products);
                refresher.complete();
            }, 
                error => { console.log("error ", JSON.stringify(error));  } );
    }
}
