import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { Accounts } from 'meteor/accounts-base';
import { HTTP } from 'meteor/http';

// Accounts.onLogin(function(user) {
//     debugger;
//     console.log(",", user.user._id)
// });

import Worklist from '../collections/worklist.js';

import workListItemSchema from '../collections/worklistItemSchema.js';

Meteor.methods({
    'Fetchworklist' (devicemap) {
        debugger
        check(devicemap, String);
        this.unblock();
        if (this.userId) {
            try {
                const result = HTTP.call('GET', `${Meteor.settings.worklistUrl}/HISIntegration.svc/rest/Fetchworklist/${devicemap}`);
                let worklistHttp = JSON.parse(result.content);
                Worklist.remove({ _userid: this.userId, _devicemap: devicemap });
                worklistHttp.WorkListItems.forEach((patient) => {
                    Worklist.insert({
                        ...patient,
                        _userid: this.userId,
                        _devicemap: devicemap
                    });
                })
                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                return false;
            }
        }
    },
    "EditworklistItem" (item) {
        // debugger;
        // const item = {
        //     "Accession": "",
        //     "AdmissionID": "1501",
        //     "EnglishFirstName": "SHOOSHAN",
        //     "EnglishFirstNameParts": [
        //         "SHOOSHAN"
        //     ],
        //     "EnglishLastName": "????? KHAZER ABADI",
        //     "EnglishLastNameParts": [
        //         "KHASHILO",
        //         "KHAZER",
        //         "ABADI"
        //     ],
        //     "FarsiFirstName": "شوشان",
        //     "FarsiFirstNameParts": [
        //         "شوشان"
        //     ],
        //     "FarsiLastName": "خشيلو خزر ابادي",
        //     "FarsiLastNameParts": [
        //         "خشيلو",
        //         "خزر",
        //         "ابادي"
        //     ],
        //     "FarsiName": "شوشان^خشيلو خزر ابادي",
        //     "HISID": null,
        //     "InstitutionAddress": null,
        //     "InstitutionName": null,
        //     "Modality": "",
        //     "PPSStatus": 0,
        //     "PatientAge": "67",
        //     "PatientBirthDate": "1990/01/01",
        //     "PatientID": "132748",
        //     "PatientWeight": 0,
        //     "PatinetSex": "0",
        //     "PatinetSize": 0,
        //     "PregnancyStatus": 4,
        //     "ProcedureID": null,
        //     "ProcedureSteps": [],
        //     "ReferringPhysicianName": null,
        //     "RequestedProcedureID": "1501",
        //     "ScheduledStartDate": null,
        //     "Smoking": 2,
        //     "StudyInstanceUID": "1.3.12.2.1107.5.2.30.26719.6.8122118808"
        // }
        // try {
        if (this.userId) {
            delete item._id;
            workListItemSchema.validate(item);
            var options = {
                data: item,
                headers: {
                    'content-type': 'application/json',
                    'Accept': 'application/json'
                }
            }
            const result = HTTP.call('POST', `${Meteor.settings.worklistUrl}/HISIntegration.svc/rest/EditworklistItem`, options);
            let result1 = JSON.parse(result.content);
            delete result1._id;
            let update = Worklist.update({ AdmissionID: item.AdmissionID }, { $set: result1 });
            return true;
        }

    }
});