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 CatexamService {
  baseurl = environment.baseurl;
  constructor(public http: Http, private crypto: CryptoService) { }

  async cateexamreportpracticetestAns(user_id: number, cohort_id, token) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        cohort_id: cohort_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.KETEXAM_ANS_GETUSERREPORT;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async CateExamFiltertestques(
    user_id: number,
    test_id: number,
    cohort_id,
    token
  ) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        test_id: test_id,
        cohort_id: cohort_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.KETEXAM_ANS_FILTER;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async addcateexamtestans(
    user_id: number,
    test_id: number,
    PTQ_id: number,
    ans,
    token,
    correct,
    d_id,
    sd_id,
    topic_id,
    cohort_id,
    time
  ) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        KEQ_id: PTQ_id,
        ans: ans,
        test_id: test_id,
        user_id: user_id,
        correct: correct,
        d_id: d_id,
        sd_id: sd_id,
        topic_id: topic_id,
        cohort_id: cohort_id,
        ans_time: time,
      }))
    };
    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.KETEXAM_ADD_KETEXAMTEST_ANS;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async IdCateExamTest(id, 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 = this.baseurl + environment.KETEXAM_TEST + this.crypto.enc(id);;
      this.http.get(url, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async catexamcomplate(user_id, pt_id, token, cohort_id) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        ct_id: pt_id,
        cohort_id: cohort_id,
      }))
    };
    let headers = new Headers();
    headers.append('Accept', 'application/json');
    headers.append('Authorization', 'Bearer ' + token);

    let options = new RequestOptions({ headers: headers });

    return new Promise((resolve) => {
      var url = this.baseurl + environment.KETEXAMANS_CATEX_COMPLATE;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async DomainFilterCATEXAMA_ANS(
    user_id: number,
    test_id: number,
    c_id,
    cohort_id,
    token
  ) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        test_id: test_id,
        cource_id: c_id,
        cohort_id: cohort_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.KETEXAM_ANS_GETDOMAIN_REPORT;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
}
