/**
 *
 * (c) Copyright Ascensio System SIA 2021
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

const config = require("config");
const configServer = config.get("server");
var urlModule = require("url");
var urllib = require("urllib");
const xmlParser = require("fast-xml-parser");
const he = require("he");

var cache = null;

// get the wopi discovery information
function getDiscoveryInfo(siteUrl) {
    let actions = [];

    if (cache) return cache;

    try {
        urllib.request(urlModule.parse(siteUrl + configServer.get("wopi.discovery")), {method: "GET"}, (err, data) => {
            let discovery = xmlParser.parse(data.toString(), {  // create the discovery XML file with the parameters from the response
                attributeNamePrefix: "",
                ignoreAttributes: false,
                parseAttributeValue: true,
                attrValueProcessor: (val, attrName) => he.decode(val, { isAttributeValue: true })
            });
            for (let app of discovery["wopi-discovery"]["net-zone"].app) {
                if (!Array.isArray(app.action)) { app.action = [app.action]; }
                for (let action of app.action) {
                    actions.push({  // write all the parameters to the actions element
                        app: app.name,
                        favIconUrl: app.favIconUrl,
                        checkLicense: app.checkLicense == 'true',
                        name: action.name,
                        ext: action.ext || "",
                        progid: action.progid || "",
                        isDefault: action.default ? true : false,
                        urlsrc: action.urlsrc,
                        requires: action.requires || ""
                    });
                }
            }
        });
    } catch (e) {
        return actions;
    }

    cache = actions;
    setTimeout(() => cache = null, 1000 * 60 * 60); // 1 hour

    return actions;
}

// get actions of the specified extension
function getActions(ext) {
    let actions = getDiscoveryInfo();  // get the wopi discovery information
    let filtered = [];

    for (let action of actions) {  // and filter it by the specified extention
        if (action.ext == ext) {
            filtered.push(action);
        }
    }

    return filtered;
}

// get an action for the specified extension and name
function getAction(ext, name) {
    let actions = getDiscoveryInfo();

    for (let action of actions) {
        if (action.ext == ext && action.name == name) {
            return action;
        }
    }

    return null;
}

// get the default action for the specified extension
function getDefaultAction(ext) {
    let actions = getDiscoveryInfo();

    for (let action of actions) {
        if (action.ext == ext && action.isDefault) {
            return action;
        }
    }

    return null;
}

// get the action url
function getActionUrl(host, userAddress, action, filename) {
    return action.urlsrc.replace(/<.*&>/g, "") + "WOPISrc=" + host + "/wopi/files/" + filename + "@" + userAddress;
}

exports.getDiscoveryInfo = getDiscoveryInfo;
exports.getAction = getAction;
exports.getActions = getActions;
exports.getActionUrl = getActionUrl;
exports.getDefaultAction = getDefaultAction;
@@ -1099,16 +1099,44 @@
 a) =%3E %7B%0A
+            if (data) %7B%0A    
         
@@ -1274,24 +1274,28 @@
             
+    
 attributeNam
@@ -1303,24 +1303,28 @@
 Prefix: %22%22,%0A
+    
             
@@ -1368,16 +1368,20 @@
         
+    
 parseAtt
@@ -1411,24 +1411,28 @@
             
+   
+ 
 attrValuePro
@@ -1474,17 +1474,16 @@
 e(val, %7B
- 
 isAttrib
@@ -1496,17 +1496,16 @@
 ue: true
- 
 %7D)%0A     
@@ -1503,36 +1503,44 @@
 e%7D)%0A            
+   
+ 
 %7D);%0A
+    
             for 
@@ -1610,24 +1610,28 @@
             
+    
 if (!Array.i
@@ -1651,16 +1651,40 @@
 tion)) %7B
+%0A                       
  app.act
@@ -1702,19 +1702,43 @@
 action%5D;
- %7D%0A
+%0A                    %7D%0A    
         
@@ -1798,16 +1798,20 @@
         
+  
+  
 actions.
@@ -1893,16 +1893,20 @@
         
+    
 app: app
@@ -1908,24 +1908,28 @@
 : app.name,%0A
+    
             
@@ -1992,16 +1992,20 @@
         
+ 
+   
 checkLic
@@ -2062,16 +2062,20 @@
         
+    
 name: ac
@@ -2109,16 +2109,20 @@
         
+   
+ 
 ext: act
@@ -2128,32 +2128,36 @@
 tion.ext %7C%7C %22%22,%0A
+    
                 
@@ -2217,16 +2217,20 @@
         
+    
 isDefaul
@@ -2287,16 +2287,20 @@
         
+  
+  
 urlsrc: 
@@ -2314,16 +2314,20 @@
 urlsrc,%0A
+    
         
@@ -2386,35 +2386,61 @@
                 
-%7D);
+    %7D);%0A                    %7D
 %0A               

check callback data