var Q = require('q');
var fs = require('fs');
var utils = require('../lib/utils');

function MockClinicalLibrary(delay){

    var useRecordFilter = true;

    var getOrg1Data = function (url, overrideSettings){

        switch (url){
            case "/Patient/813":
                return "Demographics_813.json";
                break;
            case "/Patient/34?_include=*":
                if (useRecordFilter)
                    return "PatientRecord_34.filtered.json";
                else
                    return "PatientRecord_34.json";
                break;
            case "/Patient/3058?_include=*":
                if ((overrideSettings) && (overrideSettings.fhir.toLowerCase() === 'stu3'))
                    return "Patient3058_STU3.json";
                else
                    return "Patient3058.json";
                break;                
            case "/Patient/233?_include=*":
                return "PatientRecord_233.json";
                break;
            case "/Patient/563?_include=*":
                return "PatientRecord_563.json";
                break;
            case "/Patient/743?_include=*":
                return "PatientRecord_743.json";
                break;
            case "/Patient/1520?_include=*":
                return "PatientRecord_1520.json";
                break;
            case "/Patient/1646?_include=*":
                return "PatientRecord_1646.json";
                break;
            case "/Patient/2614?_include=*":
                return "PatientRecord_2614.json";
                break;
            case "/Patient/500112?_include=*":
                return "PatientRecord_500112.json";
                break;
            case "/Patient/502799?_include=*":
                return "PatientRecord_502799.json";
                break;
            case "/Patient?identifier=NHS|4002520226":
                return "Search_4002520226.json";
                break;
            case "/Patient?birthdate=1937-05-20":
                return "Search_4002520226.json";
                break;
            case "/Patient?identifier=NHS|4499261864":
                return "Search_4499261864.json";
                break;
            case "/Patient?identifier=NHS|4577390825":
                return "Search_4577390825.json";
                break;
            case "/Patient?identifier=NHS|4633746065":
                return"Search_4633746065.json";
                break;
            case "/Patient?identifier=NHS|7071948666":
                return "Search_7071948666.json";
                break;
            case "/Patient?identifier=NHS|7543520265":
                return "Search_7543520265.json";
                break;
            case "/Patient?identifier=NHS|7919720621":
                return "Search_7919720621.json";
                break;
            case "/Patient?identifier=NHS|8413693624":
                return "Search_8413693624.json";
                break;
            case "/Patient?identifier=NHS|0123456789":
                return "Search_0123456789.json";
                break;
            case "/Patient?birthdate=2008-05-15":
                return "Search_0123456789.json";
                break;
            case "/Patient?name.family=Duck&name.given=D":
                return "Search_0123456789.json";
                break;
            case "/Schedule?date=2015-09-29":
                return "Schedule_20150929.json";
                break;
            case "/Schedule?date=2015-09-30":
                return "Schedule_20150930.json";
                break;
            case "/Slot?date=2015-09-29&schedule=6222":
                return "Slot_6222_20150929.json";
                break;
            case "/Slot?schedule=6222&date=2015-09-29":
                return "Slot_6222_20150929.json";
                break;
            case "/Appointment?date=2015-09-29&schedule=6222":
                return "Appointment_6222_20150929.json";
                break;
            case "/Appointment?schedule=6222&date=2015-09-29":
                return "Appointment_6222_20150929.json";
                break;
        }
    };

    var getOrg2Data = function (url){
        switch (url){
            case "/Patient/5284655303?_include=*":
                return "PatientRecord_5284655303.json";
                break;
            case "/Patient/6483222572?_include=*":
                return "PatientRecord_6483222572.json";
                break;
            case "/Patient?identifier=NHS|5284655303":
                return"Search_5284655303.json";
                break;
            case "/Patient?identifier=NHS|6483222572":
                return "Search_6483222572.json";
                break;
        }
    };

    var isOrg1 = function(connectionString){
        return (connectionString.indexOf("organisation=50002") != -1);
    };

    function parseHeaders(headers){
        if (!headers)
            return;

        var list = JSON.parse(headers);
        if (!Array.isArray(list))
            return;
        
        settings = list[0];
        if (settings.key !== 'bps-override-settings')
            return;
        
        return JSON.parse(settings.value);
    }
    
    this.get = function(url, connectionString, queue, headers){

        var overrideSettings = parseHeaders(headers);

        var filename =  (isOrg1(connectionString))
            ? getOrg1Data(url, overrideSettings)
            : getOrg2Data(url, overrideSettings);

        if (filename == undefined) {
            if (utils.urlIsSearch(url)) {
                if (url.indexOf('/Slot')  == 0)
                    filename = "Slot_empty.json";
                else if (url.indexOf('/Appointment') == 0)
                    filename = "Appointment_empty.json";
                else
                    filename = "Search_bad.json";
            } else
                return Q.reject(new Error("API Error"));
        }

        var data = fs.readFileSync(require('path').join(__dirname, "../data/pyrusConnect/" + filename), "utf8");
        return Q(data);
    };

    this.post = function(url, data, connectionString){

        var validPatient;
        switch (url) {
            case "/Patient/34":
            case "/Patient/233":
            case "/Patient/563":
            case "/Patient/654":
            case "/Patient/743":
            case "/Patient/1520":
            case "/Patient/1646":
            case "/Patient/2614":
            case "/Patient/500112":
                validPatient = (isOrg1(connectionString));
                break;
            case "/Patient/5284655303":
            case "/Patient/6483222572":
                validPatient = (!isOrg1(connectionString));
                break;
            case "/Appointment":
                validPatient = (isOrg1(connectionString));
                break;
            case "/Slot":
                validPatient = (isOrg1(connectionString));
                break;
            default:
                validPatient = false;
                break;
        }

        if (validPatient)
            return Q("");
        else
            return Q.reject(new Error("API Error"));
    };

    this.bypassPatientRecordFilter = function () {
        useRecordFilter = false;
    };

    this.config = function(){
        return Q("");
    };
}

module.exports = new MockClinicalLibrary();
