import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import {
  Http,
  Headers,
  RequestOptions,
} from '@angular/http';
import { CryptoService } from '../crypto/crypto.service';

@Injectable({
  providedIn: 'root',
})
export class GamificationserviceService {
  baseurl = environment.baseurl;
  constructor(public http: Http, private crypto: CryptoService) { }
  async getLeaderborad(user_id, token) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id
      }))
    }
    return new Promise((resolve) => {
      let headers = new Headers();
      headers.append('Accept', 'application/json');
      headers.append('Authorization', 'Bearer ' + token);
      let options = new RequestOptions({ headers: headers });
      var url = this.baseurl + environment.POINTS_LEADER_BOARD;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve(err);
        }
      );
    });
  }
  async scoreImprove(user_id, token, cource_id) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        cource_id: cource_id
      }))
    };
    return new Promise((resolve) => {
      let headers = new Headers();
      headers.append('Accept', 'application/json');
      headers.append('Authorization', 'Bearer ' + token);
      let options = new RequestOptions({ headers: headers });
      var url = this.baseurl + environment.POINTS_LBWITH_DOMAIN;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
}
