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

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

  constructor(public http: Http, private Toaster: ToastrService, private crypto: CryptoService) {

  }

  async Addacronyms(short_name, full_name, certi_id, token) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        short_name: short_name,
        full_name: full_name,
        certi_id: certi_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.ACRONYMS_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 Updateacronyms(id, short_name, full_name, certi_id, token) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        id: id,
        short_name: short_name,
        full_name: full_name,
        certi_id: certi_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.ACRONYMS_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 Deleteacronyms(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.ACRONYMS_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 Listacronyms(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.ACRONYMS_ALL
      this.http.post(url, options, { headers: headers }).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res["_body"] }])
        },
        (err) => {
          console.log(err)
          resolve([{ status: err.status, json: err["_body"] }])
        }
      );
    });
  }
  async Idacronyms(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.ACRONYMS_BY_ID
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res["_body"] }])
        },
        (err) => {
          resolve([{ status: err.status, json: err["_body"] }])
        }
      );
    });
  }

}
