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

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

  constructor(public http: Http, private crypto: CryptoService) { }
  async GetAllReferences(token) {
    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 = environment.BASE_URL + environment.REFERENCE_ALL
      this.http.post(url, {}, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res["_body"] }])
        },
        (err) => {
          resolve([{ status: err.status, json: err["_body"] }])
        }
      );
    });
  }
  async AddReferences(certi_id, d_id, sd_id, topic_id, ref_type, book_id, page_no, title, web_link, token) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        "certi_id": certi_id,
        "d_id": d_id,
        "sd_id": sd_id,
        "topic_id": topic_id,
        "ref_type": ref_type,
        "book_id": book_id,
        "page_no": page_no,
        "title": title,
        "web_link": web_link
      }))
    }
    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 = environment.BASE_URL + environment.REFERENCE_ADD
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res["_body"] }])
        },
        (err) => {
          resolve([{ status: err.status, json: err["_body"] }])
        }
      );
    });
  }
  async UpdateReferences(id, certi_id, d_id, sd_id, topic_id, ref_type, book_id, page_no, title, web_link, token) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        "id": id,
        "certi_id": certi_id,
        "d_id": d_id,
        "sd_id": sd_id,
        "topic_id": topic_id,
        "ref_type": ref_type,
        "book_id": book_id,
        "page_no": page_no,
        "title": title,
        "web_link": web_link
      }))
    }
    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 = environment.BASE_URL + environment.REFERENCE_UPDATE
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res["_body"] }])
        },
        (err) => {
          resolve([{ status: err.status, json: err["_body"] }])
        }
      );
    });
  }
  async DeleteReference(id, token) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        "id": 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 = environment.BASE_URL + environment.REFERENCE_DELETE
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res["_body"] }])
        },
        (err) => {
          resolve([{ status: err.status, json: err["_body"] }])
        }
      );
    });
  }
  async getReferenceByBook(book_id, token) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        "book_id": book_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 = environment.BASE_URL + environment.REFERENCE_BY_BOOK
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res["_body"] }])
        },
        (err) => {
          resolve([{ status: err.status, json: err["_body"] }])
        }
      );
    });
  }
}
