import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpHeaders } from '@angular/common/http';
import { Device } from '@ionic-native/device';

/*
  Generated class for the HealthProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
 */
//const  HOSTBASE = "http://localhost:8000";
const  HOSTBASE = "https://hc.simplico.net";
const COREAPI = HOSTBASE + "/coredata/api";
const PRODUCT_API = HOSTBASE + "/coredata/api/products";
const IMAGE_URL = HOSTBASE + "/media/";
const QR_URL = HOSTBASE + "/static/";
const REDEEM_API = HOSTBASE + "/coredata/api/redeem";
const SV_API = HOSTBASE + "/strava/api";
const WP = "https://whitesports.net/wp-json/wp/v2/";

@Injectable()
export class HealthProvider {
    imageURL = IMAGE_URL;
    public qrURL = QR_URL;
    public hostBase = HOSTBASE;
    httpOptions = {
        headers: new HttpHeaders({
            'Content-Type':  'application/json',
            'Authorization': 'JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoicHVibGljIiwicm9sZXMiOlsicHVibGljIl0sImV4cCI6MTczNDE2NTcxMCwiaWF0IjoxNDc0OTY1NzEwLCJuYmYiOjE0NzQ5NjU3MTAsImlkZW50aXR5IjoiNTdlOWVmMjZjMjU0ZmQ4N2Q3ZmQ4MzM2In0.-9fcm5s8qYbdqBDUX7cZJ5J3AX91fe6VrCLs_S-_eBU'
        })
    };
    constructor(public http: HttpClient, private dev: Device) {
        console.log('Hello HealthProvider Provider');
        console.log("construct device = ", this.dev.model, this.dev.serial, this.dev.isVirtual);
    }
    getProducts(): Observable<any> {
        console.log("PRODUCT_API ", PRODUCT_API);
        console.log("get products = ", this.dev.model, this.dev.isVirtual);
        return this.http.post(PRODUCT_API, {action: "LIST"});
    }
    getProductsByCat(catId: number): Observable<any> {
        return this.http.post(PRODUCT_API, {action: "LIST", query: { category_id: catId } });
    }
    getProduct(id: string): Observable<any> {
        return this.http.get(PRODUCT_API + "/" + id);
    }
    myActivities(token: string): Observable<any> {
        console.log(SV_API);
        return this.http.post(SV_API, {token: token, action: "MYACTIVITIES"});
    }
    getMyDetail(token: string): Observable<any> {
        return this.http.post(SV_API, {token: token, action: "MYDETAIL"});
    }
    getHistory(token: string): Observable<any> {
        return this.http.post(COREAPI, {token: token, action: "HISTORY"});
    }

    totalCal(): Observable<any> {
        return this.http.post(COREAPI, {action: "TOTALCAL"});
    }
    topRunners(): Observable<any> {
        return this.http.post(COREAPI, {action: "TOPRUNNERS"});
    }
    getAllRunners(): Observable<any> {
        return this.http.post(COREAPI, {action: "ALLRUNNERS"});
    }
    todayTopRunners(): Observable<any> {
        return this.http.post(COREAPI, {action: "TODAYTOP"});
    }

    redeem(token, act): Observable<any> {
        return this.http.post(COREAPI, { token: token, action: "REDEEM", data: act });
    }
    getPoint(token:string): Observable<any> {
        return this.http.post(COREAPI, {token :token, action: "GETPOINT"});
    }
    useCoupon(token, tid): Observable<any> {
        return this.http.post(COREAPI, {token: token, action: "USECOUPON",
            data: { id: tid }});
    }
    cutPoint(token:string, productId:string): Observable<any> {
        return this.http.post(COREAPI, {token: token, action: "CUTPOINT",
            data: {pid: productId}});
    }
    getPosts(page): Observable<any> {
        //coment
        return this.http.post(COREAPI, {action: "LIST", collection: "coredata_post", query: {active: true}, page: page});
    }
    getWPPostCat(cat_no): Observable<any> {
        
        return this.http.get(WP + "posts?categories="+cat_no);
    }
    recentActs(): Observable<any> {
        //coment
        return this.http.post(COREAPI, {action: "LIST", collection: "strava_stravaactivity", query: {}, page: 0});
    }

    galaxyNews(querys) {
        return this.http.post("http://www.sportgalaxy.xyz/api/v2.0/posts", {action: "LIST", model: { query: querys }  }, this.httpOptions);
    }
    getGalaxyNews(id) {
        return this.http.post("http://www.sportgalaxy.xyz/api/v2.0/posts", {action: "GET", model: { id: id }  }, this.httpOptions);
    }
    getAthlete(athId) {
        console.log("get ath ", athId);
        return this.http.post(COREAPI, {action: "GETRUNNER", athId: athId});
    }
    /*
  listNews(token, act): Observable<any> {
    return this.http.post("http://mcotn-api.simplico.net/api/v2.0/posts", {  action: "LIST", model: {} }, this.httpOptions);
  }*/

}
