/*
 * Copyright 2015-2018 Jeeva Kandasamy (jkandasa@gmail.com)
 * and other contributors as indicated by the @author tags.
 *
 * 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.
 */
package org.mycontroller.standalone.scripts.api;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.apache.commons.mail.EmailException;
import org.mycontroller.standalone.AppProperties;
import org.mycontroller.standalone.api.jaxrs.model.McHeatMap;
import org.mycontroller.standalone.api.jaxrs.model.Query;
import org.mycontroller.standalone.db.tables.GatewayTable;
import org.mycontroller.standalone.db.tables.OperationTable;
import org.mycontroller.standalone.db.tables.RuleDefinitionTable;
import org.mycontroller.standalone.email.EmailUtils;
import org.mycontroller.standalone.gateway.GatewayUtils;
import org.mycontroller.standalone.gateway.config.GatewayConfig;
import org.mycontroller.standalone.operation.OperationUtils;
import org.mycontroller.standalone.operation.model.Operation;
import org.mycontroller.standalone.rule.RuleUtils;
import org.mycontroller.standalone.rule.model.RuleDefinitionAbstract;
import org.mycontroller.standalone.settings.LocationSettings;
import org.mycontroller.standalone.utils.McUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Jeeva Kandasamy (jkandasa)
 * @since 0.0.3
 */
@Slf4j
public class UtilsApi {

    public Query getQuery() {
        return Query.builder()
                .order(Query.ORDER_ASC)
                .orderBy("id")
                .filters(new HashMap<String, Object>())
                .pageLimit(Query.MAX_ITEMS_PER_PAGE)
                .page(1L)
                .build();
    }

    public List<Integer> asList(Integer... ids) {
        return Arrays.asList(ids);
    }

    public GatewayConfig getGateway(GatewayTable gatewayTable) {
        return GatewayUtils.getGateway(gatewayTable);
    }

    public Operation getOperation(OperationTable operationTable) {
        return OperationUtils.getOperation(operationTable);
    }

    public RuleDefinitionAbstract getRuleDefinition(RuleDefinitionTable ruleDefinitionTable) {
        return RuleUtils.getRuleDefinition(ruleDefinitionTable);
    }

    public McHeatMap getHeatMap() {
        return McHeatMap.builder().build();
    }

    public String getUUID() {
        return UUID.randomUUID().toString();
    }

    public String friendlyTime(Long timestamp) {
        if (timestamp == null) {
            return "Never";
        }
        return McUtils.getFriendlyTime(System.currentTimeMillis() - timestamp, true);
    }

    public String formatTime(String pattern) {
        return formatTime(pattern, null);
    }

    public String formatTime(String pattern, Long timestamp) {
        Date date = null;
        if (timestamp != null) {
            date = new Date(timestamp);
        } else {
            date = new Date();
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    public LocationSettings getServerLocationSettings() {
        return AppProperties.getInstance().getLocationSettings();
    }

    public void sendEmail(String toAddresses, String subject, String message) {
        try {
            EmailUtils.sendSimpleEmail(toAddresses, subject, message);
        } catch (EmailException ex) {
            _logger.error("Unable to send an email:[toAddress:{}, subject:{}, message:{}], Exception:{}", toAddresses,
                    subject, message, ex.toString(), ex);
        }
    }
}

@@ -727,16 +727,44 @@
 s.api;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -911,16 +911,319 @@
 .UUID;%0A%0A
+import javax.xml.parsers.DocumentBuilder;%0Aimport javax.xml.parsers.DocumentBuilderFactory;%0Aimport javax.xml.parsers.ParserConfigurationException;%0Aimport javax.xml.xpath.XPath;%0Aimport javax.xml.xpath.XPathExpression;%0Aimport javax.xml.xpath.XPathExpressionException;%0Aimport javax.xml.xpath.XPathFactory;%0A%0A
 import o
@@ -2147,16 +2147,78 @@
 McUtils;
+%0Aimport org.w3c.dom.Document;%0Aimport org.xml.sax.SAXException;
 %0A%0Aimport
@@ -4449,11 +4449,577 @@
  %7D%0A    %7D
+%0A%0A    public String readXmlByXpath(String uri, String xpath) throws ParserConfigurationException, SAXException,%0A            IOException, XPathExpressionException %7B%0A        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();%0A        DocumentBuilder db = dbf.newDocumentBuilder();%0A        Document document = db.parse(uri);%0A%0A        XPathFactory xPathfactory = XPathFactory.newInstance();%0A        XPath xpathObj = xPathfactory.newXPath();%0A        XPathExpression expression = xpathObj.compile(xpath);%0A%0A        return expression.evaluate(document);%0A    %7D
 %0A%7D%0A

fix #449, add support to read xml file content