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 MindmapService {
  baseurl = environment.baseurl;

  constructor(public http: Http, private crypto: CryptoService) { }
  async MindMapList(token, ids) {
    var certi_id = localStorage.getItem('CisspCourseId');
    var userId = localStorage.getItem('CisspUserId');
    var cohort_id = localStorage.getItem('cohort_id');
    var url = this.baseurl + environment.MIND_MAP;
    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 data = {
        data: this.crypto.encrypt(JSON.stringify({
          certi_id: certi_id,
          user_id: userId,
          cohort_id: cohort_id,
          ids: ids
        }))
      };


      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async VideoUpdate(token, VideoUpdate) {
    var userIdset = localStorage.getItem('CisspUserId');
    var cohort_id = localStorage.getItem('cohort_id');
    var data = {
      data: this.crypto.encrypt(JSON.stringify(VideoUpdate))
    }
    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.MIND_UPDATE;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve(err);

        }
      );
    });
  }
}
