import { Component, OnInit } from '@angular/core';
import { Health } from '@awesome-cordova-plugins/health/ngx';
import { Storage } from '@ionic/storage-angular';

@Component({
    selector: 'app-login',
    templateUrl: './login.page.html',
    styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {

    constructor(private health: Health, private storage: Storage) { }

    ngOnInit() {
    }
    async ionViewDidEnter() {
        this.health.isAvailable()
            .then((available:boolean) => {
                console.log(available);
                this.health.requestAuthorization([
                    'distance', 'nutrition', 'height', 'weight'  //read and write permissions
                    /*
                    {
                        read: ['steps'],       //read only permission
                        write: ['height', 'weight']  //write only permission
                    }*/
                ])
                    .then(res => console.log(res))
                    .catch(e => console.log(e));
            })
            .catch(e => console.log(e));

        await this.storage.set('name', "Tum")



    }
    async queryStorage() {
        let name = await this.storage.get('name')
        console.log(name)
    }
    readHeight() {
        console.log("read height")
        this.health.query({  startDate: new Date(new Date().getTime() - 3 * 24 * 60 * 60 * 1000),
            // three days ago
            endDate: new Date(), // now
            dataType: 'height',
            limit: 1000
        })
            .then((data) => {
                console.log(data);

            }).catch( e => console.log(e) );
    }

    readSteps() {
        console.log("Read steps")
        this.health.queryAggregated({
          startDate: new Date(new Date().getTime() - 3 * 24 * 60 * 60 * 1000), // three days ago
          endDate: new Date(), // now
          dataType: 'steps',
          bucket: 'day'
        })
            .then(data => {
                console.log(data)
            })
        .catch( e => console.log(e) )
    }
    readNutrition() {

        this.health.query({
          startDate: new Date(new Date().getTime() - 3 * 24 * 60 * 60 * 1000), // three days ago
          endDate: new Date(), // now
          dataType: 'nutrition',
        })
            .then(data => {
                console.log("nutrition")
                console.log(data)
                for(var i of  data) {
                    console.log(i)
                }
            })
        .catch( e => console.log(e) )
    }

    addFood() {
        let food  = { item: "cheese", meal_type: "lunch", brand_name: "McDonald's", nutrients: { 'nutrition.fat.saturated': 11.5, 'nutrition.calories': 233.1 } };
        this.health.store({
          startDate:  new Date(new Date().getTime() - 60 * 60 * 1000), // three minutes ago
          endDate: new Date(),
          dataType: 'nutrition',
          value: food, sourceName: "Tum", sourceBundleId: "xxx" 
        })
            .then(data => {
                console.log(data)
            }).catch(e => console.log(e))
    }

    

}
