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',
})
@Injectable({
  providedIn: 'root',
})
export class PracticeserviceService {
  baseurl = environment.baseurl;

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

  async getuserreportpracticetestAns(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 + 'practiceans/getuserreportpracticetestAns';
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async practicecomplate(user_id, pt_id, token, cohort_id) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        pt_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.PRACTICEANS_PRE_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 ResetTest(user_id, pt_id, token, cohort_id) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        pt_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.RESET_PRACTICETEST;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async IdPracticeTest(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 = this.baseurl + environment.PRECTICETEST;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async getPracticeTestQuestions(
    user_id: number,
    test_id: number,
    cohort_id,
    token,
    RowNumber = 0,
  ) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        test_id: test_id,
        cohort_id: cohort_id,
        RowNumber: RowNumber
      }))
    };
    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 + "meeting/filter_practice_question";
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async PracticeTestFiltertestques(
    user_id: number,
    test_id: number,
    cohort_id,
    token,
    RowNumber = 0,
    pageNumber,
    pageSize
  ) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        test_id: test_id,
        cohort_id: cohort_id,
        RowNumber: RowNumber,
        pageNumber: pageNumber,
        pageSize: pageSize
      }))
    };
    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.PRECTICEANS_FILTERS_QUEST;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }

  async PracticeidWise(id: number, 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 = this.baseurl + environment.PRACTICE_ID;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async addpracticetestans(user_id: number, test_id: number, PTQ_id: number, ans, token, correct, d_id, sd_id, topic_id, flag, cohort_id, time) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        PTQ_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,
        flag_review: flag.toString(),
        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.PRECTICEANS_ADD_PRECTICE;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
  async DomainFiltertestques(
    user_id: number,
    test_id: number,
    Course_id,
    token
  ) {
    var cohort_id = localStorage.getItem('cohort_id');
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        test_id: test_id,
        course_id: Course_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.PRECTICEANS_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'] }]);
        }
      );
    });
  }
  savenotes(user_id: number, test_id: number, q_id, test_type, notes, token) {
    var cohort_id = localStorage.getItem('cohort_id');
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        test_id: test_id,
        q_id: q_id,
        cohort_id: cohort_id,
        test_type: test_type,
        notes: notes
      }))
    };
    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.SAVENOTES;
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }

  async getResetDataUser(user_id: number, cohort_id, test_id, token) {
    var data = {
      data: this.crypto.encrypt(JSON.stringify({
        user_id: user_id,
        cohort_id: cohort_id,
        test_id: test_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 + 'testlogs/previous_result';
      this.http.post(url, data, options).subscribe(
        (res) => {
          resolve([{ status: res.status, json: res['_body'] }]);
        },
        (err) => {
          resolve([{ status: err.status, json: err['_body'] }]);
        }
      );
    });
  }
}
