import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';


//import 'rxjs/add/operator/of';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Rx';

@Injectable({
  providedIn: 'root'
})
export class UserService {

  //constructor() { }
  apiKey = '99dfe35fcb7de1ee';
  url;
 

  constructor(private http: HttpClient) {
    console.log('Hello UserService Provider');
    this.url = 'https://oameye.svrjuba.works.jubabox.com/JUBA/user/';
 
  }

  
  connectUser(){
    alert('1202020');

  }

  sendGetRequest(grp: string, endPoint: string, sendData: any) {
    // grp - will be used to change URL
    var endpoint_string ="";
    console.log("GRP->"+grp);
    switch(grp){
      case '1':
      endpoint_string =this.url;
      break;

      case '2':
      endpoint_string=this.url;
      break;
    }
    let httpParams = new HttpParams();
    if (sendData) {
      for(let k in sendData) {
        httpParams.set(k, sendData[k]);
      }
    }
    return Observable.fromPromise(new Promise(resolve => {
      this.http.get(endpoint_string + endPoint, { params: httpParams }).subscribe(data => {
      //  console.log(data);
      //  console.log("*************************************************");
     /*   var payload = this.decrypt(
          this.encryptionIV,
          data["payload"],
          this.encryptionKey);*/
        console.log(payload);
     var payload = data['payload'];
        resolve(JSON.parse(payload));
      }, error => {
        console.log("Error");
      }, () => {
        console.log("OK!");        
      });
    }));
  }

  sendPostRequest(grp: string, endPoint: string, sendData: any) {
    // grp - will be used to change URL
    var endpoint_string ="";
    console.log("GRP->"+grp);
    switch(grp){
      case '1':
      endpoint_string =this.url;
      break;

      case '2':
      endpoint_string=this.url;
      break;
    }
    //return this.http.post(this.url + endPoint, sendData);
    return Observable.fromPromise(new Promise(resolve => {
      this.http.post(endpoint_string + endPoint, sendData).subscribe(data => {
        console.log(data);
        console.log("*************************************************");
     /*   var payload = this.decrypt(
          this.encryptionIV,
          data["payload"],
          this.encryptionKey);
          */
         var payload = data['payload'];
        console.log(payload);
        resolve(JSON.parse(payload));
      }, error => {
        console.log("Error");
      }, () => {
        console.log("OK!");
      });
    }));
  }

}
