import { Injectable }   from '@angular/core'
import { Http }         from '@angular/http'

import 'rxjs/add/operator/toPromise';

import { HelperService } from './svc.helper'

@Injectable()
export class StorelocatorService {
    
    constructor(
        private http: Http,
        private _helper: HelperService
    ) { }

    api = this._helper.server+'feed/get/'
    key = this._helper.credentials

    getCountries(): Promise<any> {
        return this.http.get(this.api + 'store-countries' + this.key + '&usFirst').toPromise().then(response => {
            return response.json().data
        })
    }

    findNearest(lat: Number, lon: Number) {
        return this.http.get(this.api + 'store-nearest'+this.key + '&lat=' + lat + '&lng=' + lon).toPromise().then(response => {
            return response.json().data
        })
    }

    findStoreByUrlkey(urlkey): Promise<any> {
        return this.http.get(this.api + 'store' + this.key + '&urlkey=' + urlkey).toPromise().then(response => {
            return response.json().data
        })
    }

    getNearbyStores(id: any): Promise<any> {
        return this.http.get(this.api + 'store-nearby' + this.key + '&id=' + id).toPromise().then(response => {
            return response.json().data
        })
    }

    findStoresByCountry(country: string): Promise<any> {
        return this.http.get(this.api + 'store' + this.key + '&country=' + country).toPromise().then(response => {
            return response.json().data
        })
    }

    findStoresByState(state: string): Promise<any> {
        return this.http.get(this.api + 'store' + this.key + '&state=' + state).toPromise().then(response => {
            return response.json().data
        })
    }
    getTypes(): Promise<any> {
        return this.http.get(this.api + 'store-types' + this.key + '&reverse').toPromise().then(response => {
            return response.json().data.data.types
        })
    }
}
