/**
 * @typedef {object} FlightFilter
 * @property {number} id
 * @property {string} description
 * @property {string} airlineCode
 * @property {string} flightNumber
 * @property {string} departureZone
 * @property {string} arrivalZone
 */


flightfilterdata = {
    /**
     * @type {FlightFilter[]}
     */
    aas1: [],
    /**
     * @type {FlightFilter[]}
     */
    aas2: [],
    /**
     * @type {FlightFilter[]}
     */
    aas3: [],
}

/**
 * Check if flight ID is valid
 * @param {number} id number from 1 to 3
 * @returns true if valid, false otherwise
 */
function validFlightID(id) {
    return (typeof id === 'number' && id >= 1 && id <= 3);
}

/**
 * Clear flight filter data for given ID
 * @param {number} id from 1 to 3 
 */
function clearflightfilterdata(id) {
    if (validFlightID(id)) {
        switch (id) {
            case 1:
                flightfilterdata.aas1 = [];
                break;
            case 2:
                flightfilterdata.aas2 = [];
                break;
            case 3:
                flightfilterdata.aas3 = [];
                break;
        }
    }
}

/**
 * Fetch flight filter data for given ID
 * @param {number} id from 1 to 3 
 * @param {function} cbOK Callback when done
 * @param {function} cbErr Callback on error contain error message
 */
function getflightfilterdata(id, cbOK, cbErr) {
    if (validFlightID(id) === false) return;
    
    clearflightfilterdata(id);
    // use POST function from script.js with URL "FlightFilter" with command="get" and aas_id=id
    Post("FlightFilter", { command: "get", aas_id: id }, function (data) {
        if (data && Array.isArray(data.filters)) {
            switch (id) {
                case 1:
                    flightfilterdata.aas1 = data.filters;
                    break;
                case 2:
                    flightfilterdata.aas2 = data.filters;
                    break;
                case 3:
                    flightfilterdata.aas3 = data.filters;
                    break;
            }
            if (cbOK) cbOK();
        } else if (cbErr) cbErr("Invalid data format received from server.");
    }, function (errMsg) {
        if (cbErr) cbErr(errMsg);
    }); 
}

/**
 * Clear flight filter data for given ID on server
 * @param {number} id from 1 to 3 
 * @param {function} cbOK Callback when done
 * @param {function} cbErr Callback on error contain error message
 */
function clearflightfilterdata(id, cbOK, cbErr) {
    if (validFlightID(id) === false) return;

    // use POST function from script.js with URL "FlightFilter" with command="clear" and aas_id=id
    Post("FlightFilter", { command: "clear", aas_id: id }, function (data) {
        switch (id) {
            case 1:
                flightfilterdata.aas1 = [];
                break;
            case 2:
                flightfilterdata.aas2 = [];
                break;
            case 3:
                flightfilterdata.aas3 = [];
                break;
        }
        if (cbOK) cbOK();
    }, function (errMsg) {
        if (cbErr) cbErr(errMsg);
    });
}

/**
 * Add flight filter data for given ID on server
 * @param {number} id from 1 to 3 
 * @param {FlightFilter} data 
 * @param {function} cbOK Callback when done
 * @param {function} cbErr Callback on error contain error message
 * @returns 
 */
function addFlightFilter(id, data, cbOK, cbErr) {
    if (validFlightID(id) === false) return;
    // use POST function from script.js with URL "FlightFilter" with command="add", aas_id=id and data=data in FlightFilter format
    Post("FlightFilter", { command: "add", aas_id: id, filter: data }, function (respData) {
        if (cbOK) cbOK();
    }, function (errMsg) {
        if (cbErr) cbErr(errMsg);
    });
}

/**
 * delete flight filter data for given ID on server
 * @param {number} id from 1 to 3 
 * @param {number} filterId row ID to delete
 * @param {function} cbOK Callback when done
 * @param {function} cbErr Callback on error contain error message
 * @returns 
 */
function deleteflightfilter(id, filterId, cbOK, cbErr) {
    if (validFlightID(id) === false) return;
    // use POST function from script.js with URL "FlightFilter" with command="delete", aas_id=id and filter_id=filterId
    Post("FlightFilter", { command: "delete", aas_id: id, filter_id: filterId }, function (respData) {
        if (cbOK) cbOK();
    }, function (errMsg) {
        if (cbErr) cbErr(errMsg);
    });
}

/**
 * Patch flight filter data for given ID on server
 * @param {number} id from 1 to 3
 * @param {FlightFilter} data data to patch
 * @param {function} cbOK Callback when done
 * @param {function} cbErr Callback on error contain error message
 * @returns 
 */
function patchflightfilter(id, data, cbOK, cbErr) {
    if (validFlightID(id) === false) return;
    // use POST function from script.js with URL "FlightFilter" with command="patch", aas_id=id and data=data in FlightFilter format
    Post("FlightFilter", { command: "patch", aas_id: id, data: data }, function (respData) {
        if (cbOK) cbOK();
    }, function (errMsg) {
        if (cbErr) cbErr(errMsg);
    });
}

$(document).ready(function () {
    console.log("Flight filter script loaded.");

    getflightfilterdata(1);
    getflightfilterdata(2);
    getflightfilterdata(3);
    
    $("#accordion").on("shown.bs.collapse", function (e) {
        const $panel = $(e.target);


        const $table = $panel.find('.FlightFilterTable');
        const $clear = $panel.find('.clearButton');
        const $add = $panel.find('.addButton');
        const $del = $panel.find('.deleteButton');
        const $edit = $panel.find('.editButton');


    });
});