import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { NgxChartsModule } from '@swimlane/ngx-charts';
import { single } from './data';
import { single2 } from './data2';
import { HealthProvider } from '../../providers/health/health';
import { HumanizePipe  } from 'angular2-humanize';
import { NativeStorage } from '@ionic-native/native-storage';
import { InAppBrowser } from '@ionic-native/in-app-browser';
/**
 * Generated class for the EventPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-event',
    templateUrl: 'event.html',
})
export class EventPage {
    allRunners;
    single: any[];
    data: any[];
    multi: any[];
    tops: any[];
    todayTops: any[];
    acts: any[];
    view: any[] = [700, 400];

    // options
    showXAxis = true;
    showYAxis = true;
    gradient = false;
    showLegend = true;
    showXAxisLabel = true;
    xAxisLabel = 'Country';
    showYAxisLabel = true;
    yAxisLabel = 'Population';
    po;
    eventSeg: string = "contributors";

    colorScheme = {
        domain: ['#5AA454', '#A10A28', '#C7B42C', '#AAAAAA']
    };
    constructor(public navCtrl: NavController, public navParams: NavParams,
        private hp: HealthProvider,
        private ns: NativeStorage,
   private ab: InAppBrowser ) {
        //Object.assign(this, { single })
        this.single = single;
        this.data = single2;
        this.hp.topRunners()
            .subscribe( data => {
                console.log("TOP Runners");
                console.log(JSON.stringify(data));
                this.tops = data.output;
            }, error => {
                console.log(error);
            } );

        this.hp.getAllRunners()
            .subscribe( data => {
                this.allRunners = data.output;

            } );
        this.hp.todayTopRunners()
            .subscribe( data => {
                this.todayTops = data.output;
            }, error => {
                console.log(error);
            } );
        this.hp.recentActs()
            .subscribe( data => {
                this.acts = data.output;
            }, error => {
                console.log(error);
            } );
        this.ns.getItem("profileObject")
            .then( data => { 
                this.po = data
            }, error => this.po = null);
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad EventPage');
    }

    onSelect(event) {
        console.log(event);
    }
    format(data) {
        let r = /,/gi
        console.log(data.replace(r, ''));
        let n = parseInt(data.replace(r, ''));
        function abbreviateNumber(value) {
            var newValue = value;
            if (value >= 1000) {
                var suffixes = ["", "k", "m", "b","t"];
                var suffixNum = Math.floor( (""+value).length/3 );
                var shortValue;
                for (var precision = 2; precision >= 1; precision--) {
                    shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
                    var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
                    if (dotLessShortValue.length <= 2) { break; }
                }
                var shortNum;
                if (shortValue % 1 != 0)  shortNum = shortValue.toFixed(1);
                newValue = shortValue+suffixes[suffixNum];
            }
            return newValue;
        }
        let v = abbreviateNumber(n);	
        //return (  n / 1000 ) + "k";
        return v;

    }
    format2(data) {
        let r = /,/gi
        console.log(data.replace(r, ''));
        let n = Math.round(parseInt(data.replace(r, '')) / 1000);
        function abbreviateNumber(value) {
            var newValue = value;
            if (value >= 1000) {
                var suffixes = ["", "k", "m", "b","t"];
                var suffixNum = Math.floor( (""+value).length/3 );
                var shortValue;
                for (var precision = 2; precision >= 1; precision--) {
                    shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
                    var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
                    if (dotLessShortValue.length <= 2) { break; }
                }
                var shortNum;
                if (shortValue % 1 != 0)  shortNum = shortValue.toFixed(1);
                newValue = shortValue+suffixes[suffixNum];
            }
            return newValue;
        }
        let v = abbreviateNumber(n);	
        //return (  n / 1000 ) + "k";
        return v;

    }
    refreshEvent(refresher) {
        this.hp.topRunners()
            .subscribe( data => {
                console.log("TOP Runners");
                console.log(JSON.stringify(data));
                this.tops = data.output;
                refresher.complete();
            }, error => {
                console.log(error);
            } );
        this.hp.getAllRunners()
            .subscribe( data => {
                this.allRunners = data.output;

            } );
        this.hp.recentActs()
            .subscribe( data => {
                this.acts = data.output;
            }, error => {
                console.log(error);
            } );
        this.ns.getItem("profileObject")
            .then( data => { 
                this.po = data
            }, error => this.po = null);
        this.hp.todayTopRunners()
            .subscribe( data => {
                this.todayTops = data.output;
            }, error => {
                console.log(error);
            } );
    }
    openLink(link: string) {
         this.ab.create(link, '_blank');
    }
}
