
/**
 * Check if socket.io-client still connected
 * @param {Socket} socket
 * @returns true if connected
 */
function socketvalid(socket){
    return (socket!=null && socket.connected);
}

function gettablenames(socket, callback){
    if (socketvalid(socket)){
        socket.emit("gettablenames", (reply)=>{
            callback?.(reply?.data);
        });
    } else callback?.("no socket");
}

/**
 * Create table
 * @param {Socket} socket Socketio connection 
 * @param {String} tablename in format yyyy-MM-dd
 * @param {Function} callback Callback function to handle result with result in string : "success" | "failed" | "invalid data" | "no event handler"
 */
function createtable(socket, tablename,callback){
    if (socketvalid(socket)){
        socket.emit("createtable", tablename, (reply)=>{
            callback?.(reply?.result);
        })

    }  else callback?.("no socket");
}

/**
 * Drop table
 * @param {Socket} socket Socketio connection
 * @param {String} tablename in format yyyy-MM-dd
 * @param {Function} callback Callback function to handle result with result in string : "success" | "failed" | "invalid data" | "no event handler"
 */
function droptable(socket, tablename, callback){
    if (socketvalid(socket)){
        socket.emit("droptable",tablename,(reply)=>{
            callback?.(reply?.result);
        })

    } else callback?.("no socket");
}

/**
 * Delete row in a table
 * 
 * The request data is {
 *   tablename: tablename,
 *   rowid: rowid
 * }
 * 
 * The reply format is {
 * command: "deleterow", 
 * value: {
 *   tablename: tablename,
 *   rowid: rowid,
 *   }
 * result: "success" | "failed" | "invalid data" | "no event handler" 
 * }
 * @param {Socket} socket Socketio connection
 * @param {String} tablename Table name in format yyyy-MM-dd
 * @param {number} rowid Row Index to delete
 * @param {Function} callback Callback function to handle result with result in string : "success" | "failed" | "invalid data" | "no event handler"
 */
function deleterow(socket, tablename, rowid, callback){
    if (socketvalid){
        let value = {
            tablename: tablename,
            rowid: rowid
        }
        socket.emit("deleterow",value,(reply)=>{
            callback?.(reply?.result);
        })
    } else callback?.("no socket");
}

/**
 * Insert fisdata object to database
 * 
 * The request data is {
 *   tablename: tablename,
 *   fisdata: fisdata
 *   }
 * 
 * The reply format is {
 *   command: "insertrow",
 *   value: {
 *     tablename: tablename,
 *     fisdata: fisdata
 *   }
 *   result: "success" | "failed" | "invalid data" | "no event handler"
 * }
 * @param {Socket} socket Socketio connection
 * @param {String|null} tablename tablename in format yyyy-MM-dd
 * @param {FisData} fisdata FisData object to insert
 * @param {Function} callback Callback function to handle result with result in string : "success" | "failed" | "invalid data" | "no event handler"
 */
function insertrow(socket, tablename, fisdata, callback){
    if (socketvalid){
        let value = {
            tablename: tablename,
            fisdata: fisdata
        }
        socket.emit("insertrow",value,(reply)=>{
            callback?.(reply?.result);
        })           
    } else callback?.("no socket");
}

/**
 * Get Data from database
 * 
 * The request data is {
 *   tablename: tablename,
 *   filter: {
 *     airlinecode: filter_airline,
 *     flightnumber: filter_flightnumber,
 *     DA: filter_da,
 *     DI: filter_di
 *   }
 * 
 * The reply format is {
 *   command: "getdata",
 *   value: {
 *     tablename: tablename,
 *     filter: {
 *       airlinecode: filter_airline,
 *       flightnumber: filter_flightnumber,
 *       DA: filter_da,
 *       DI: filter_di
 *     }
 *   result: "success" | "failed" | "invalid data" | "no event handler"
 *   data: [FisData]
 * }
 * 
 * return of this function is object {
 *   result: "success" | "failed" | "invalid data" | "no event handler",
 *  data: [FisData]
 * }
 * 
 * @param {Socket} socket socketio connection 
 * @param {String} tablename tablename in format yyyy-MM-dd
 * @param {String|null} filter_airline use to filter airline code
 * @param {String|null} filter_flightnumber use to filter flight number 
 * @param {String|null} filter_da use to filter Departure or Arrival
 * @param {String|null} filter_di use to filter Domestic or International
 * @param {Function} callback Callback function to handle result with result in string : "success" | "failed" | "invalid data" | "no event handler"
 */
function getdata(socket, tablename, filter_airline, filter_flightnumber, filter_da, filter_di, callback){
    if (socketvalid){
        let value = {
            tablename: tablename,
            filter: {
                airlinecode: filter_airline,
                flightnumber: filter_flightnumber,
                DA: filter_da,
                DI: filter_di
            }
        }
        socket.emit("getdata",value,(reply)=>{
            callback?.({result:reply?.result, data:reply?.data});
        })
    } else callback?.({result:"no socket", data:[]});
}

/**
 * Edit Row in database
 * 
 * The request data is {
 *   tablename: tablename,
 *   rowid: rowid,
 *   newdata: fisdata
 *   }
 * 
 * The reply format is {
 *   command: "editrow",
 *   value: {
 *     tablename: tablename,
 *     rowid: rowid,
 *     newdata: fisdata
 *     }
 *   result: "success" | "failed" | "invalid data" | "no event handler"
 *   }
 * @param {Socket} socket Socketio connection
 * @param {String} tablename tablename in format yyyy-MM-dd 
 * @param {number} rowid row index to edit 
 * @param {FisData} newdata data to replace at row index
 * @param {Function} callback Callback function to handle result with result in string : "success" | "failed" | "invalid data" | "no event handler"
 * @returns status in string : "success" | "failed" | "invalid data" | "no event handler"
 */
function editrow(socket, tablename, rowid, newdata, callback){
    if (socketvalid){
        let value = {
            tablename: tablename,
            rowid: rowid,
            newdata: newdata
        }
        socket.emit("editrow",value,(reply)=>{
            callback?.(reply?.result);
        })           
    } else callback?.("no socket");
}

/**
 * Import data from xls file
 *
 * The request data is {
 *  tablename: tablename,
 *  filename: filename,
 *  size: size,
 *  filedata: ArrayBuffer
 *  }
 *
 *  size and filedata is read from filename by FileReader
 *
 *  The reply format is {
 *      command: "importfromxls",
 *      tablename: tablename,
 *      filename: filename,
 *      size: size,
 *      result: "success" | "failed" | "invalid data" | "no event handler"
 *      }
 *
 * @param {Socket} socket
 * @param {String} tablename
 * @param {String} filename
 * @param {Blob} file
 * @param {Function} callback
 */
function importfromxls(socket, tablename, filename, file, callback){
    if (socketvalid){
        let value = {
            tablename: tablename,
            filename: filename,
            size: 0,
            filedata: null
        }

        const reader = new FileReader();
        reader.onload = (e)=>{
            console.log("File read complete, size: "+e.target.result.byteLength);
            value.size = e.target.result.byteLength;
            value.filedata = new Uint8Array(e.target.result) ;
            socket.emit("importxlsx",value,(reply)=>{
                console.log("Result from importfromxls: "+JSON.stringify(reply));
                callback?.(reply?.result);
            })
        }

        reader.readAsArrayBuffer(file);


    } else callback?.("no socket");
}

/**
 * Download data to xls file
 * <br/>
 * The request data is {<br/>
 *  tablename: tablename <br/>
 *  }<br/>
 *<br/>
 *  The reply format is {<br/>
 *      command: "exporttoxls",<br/>
 *      tablename: tablename,<br/>
 *      result: "success" | "failed" | "invalid data" | "no event handler"<br/>
 *      size: size,<br/>
 *      filename: exports/{filename in xlsx}<br/>
 *      }<br/>
 * <br/>
 *      Use the filename to download file from server
 *
 *      callback value is {
 *          result: "success" | "failed" | "invalid data" | "no event handler",
 *          filename: filename
 *          size: size
 *      }
 * @param socket
 * @param tablename
 * @param callback
 */
function exporttoxls(socket, tablename, callback){
    if (socketvalid){
        let value = {
            tablename: tablename
        }
        let vv = {
            result: "",
            filename: "",
            size: 0
        }
        socket.emit("exportxlsx",value,(reply)=>{
            vv.result = reply?.result;
            vv.filename = reply?.filename;
            vv.size = reply?.size;
            callback?.(vv);
        })
    } else {
        vv.result = "no socket";
        callback?.(vv);
    }
}



/**
 * Login to server
 * The request data is {
 *  username: username,
 *  password: password
 *  }
 *
 *  The reply format is {
 *      command: "login",
 *      value: {
 *          username: username,
 *          password: password
 *          }
 *      result: "welcome ..." | "failed" | "invalid data" | "no event handler"
 *      }
 *      Use the result to check if login success
 * @param socket
 * @param username
 * @param password
 * @param callback
 */
function login(socket, username, password, callback){
    if (socketvalid){
        let value = {
            username: username,
            password: password
        }
        socket.emit("login",value,(reply)=>{
            callback?.(reply.result);
        })
    } else callback?.("no socket");
}
