import { Injectable } from '@angular/core';
import {BaseService} from '../../base/base-service';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {environment} from '../../../../environments/environment';
import {catchError, concatMap, filter, map, tap} from 'rxjs/operators';
import {SweetalertService} from '../sweetalert/sweetalert';
import {throwError} from 'rxjs';

@Injectable({
    providedIn: 'root'
})
export class ExamNotificationService extends BaseService {
    requestUrl;
    constructor(
        public http: HttpClient,
        private swSV: SweetalertService,
    ) {
        super('/v1/information/testing_results_count_waitting', http);
    }

    getByUid(testUid){
        return this.http.get(`${this.fullUrl}?testing_information_uid=${testUid}`);
    }

    getSendUpdateNotiDate(data) {
        return this.http.put(`${this.prefix}/v1/information/exam_notification/${data.testing_information_uid}/${data.subject_uid}/${data.testing_datetime}/${data.period_id}`, null);
    }

    sendMail(data) {
        // return this.http.post(`${environment.storeApi}/api/sentmail/sentmail/${email}/${body}/${topic}`, null);
        return this.http.post(`${environment.storeApi}/api/sentmail/sentmail`, data);
    }

    getInstructorEmail(subject_uid){
        return this.http.get(`${this.prefix}/information/instructor_email/${subject_uid}`)
    }

    sendNotfi() {
        return this.http.get(`https://uat.ru.ac.th/etest/sendmail.jsp`);
    }

    sendNotfiGet() {
        console.log('get')
        // const httpHeaders: HttpHeaders = new HttpHeaders({
        //     'Access-Control-Allow-Headers': 'Content-Type',
        //     'Access-Control-Allow-Methods': 'POST',
        //     'Connection': 'keep-alive',
        //     'Content-Type': 'text/html;charset=UTF-8',
        // });
        const httpHeaders = new HttpHeaders()
            .set('Content-Type', 'text/html;charset=UTF-8')
            .set('mode', 'no-cors');
        return this.http.get<any>(`https://uat.ru.ac.th/etest/sendmail.jsp` ,{headers: httpHeaders});


    }

    sendNotiTest(){
        this.swSV.confirmText('ยืนยันการอัพเดทการแจ้งเตือน').pipe(
            filter(x => x.isConfirmed),
            concatMap(x => {
                return fetch(`https://uat.ru.ac.th/etest/sendmail.jsp`, {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'text/html;charset=UTF-8',
                    },
                    mode: 'no-cors'
                })
                    .then(response => response.text())
                    .then(response => this.swSV.updateSuccess())
                    .then(result => console.log(result))
                    .catch(error => console.log('error', error));
            })
        ).subscribe();
    }

    sendNotfiPost() {
        return this.http.post(`https://uat.ru.ac.th/etest/sendmail.jsp`, null);
    }
}
