package com.huatek.busi.ws.util;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.util.IOUtils;
import com.huatek.busi.ws.protocol.ActionVo;
import com.huatek.busi.ws.protocol.CommandVo;
import com.huatek.busi.ws.protocol.ReponseVO;

/**
 * 工具类
 *
 * @author anker cheng
 *
 * @Date 2019年6月24日
 */
public final class CacUtil {
    private static final Logger logger = LoggerFactory.getLogger(CacUtil.class);

    private CacUtil() {
    }

    public static String buildResult() {
        ReponseVO vo = createVo();
        String result = createXml(vo);
        return result;
    }

    private static ReponseVO createVo() {
        ReponseVO vo = new ReponseVO();
        vo.setResult("0");
        CommandVo cvo1 = new CommandVo();
        cvo1.setObjid("26M00090990000987");
        cvo1.setType("SETCONFIG");

        ActionVo avo1 = new ActionVo();
        avo1.setName("CAGIP");
        avo1.setValue("10.144.98.101");
        cvo1.getActionlist().add(avo1);

        ActionVo avo2 = new ActionVo();
        avo2.setName("CURTIME");
        avo2.setValue("2011-04-10 22:10:11");
        cvo1.getActionlist().add(avo2);

        CommandVo cvo2 = new CommandVo();
        cvo2.setObjid("26M00090990000986");
        cvo2.setType("SETCONFIG");

        ActionVo avo3 = new ActionVo();
        avo3.setName("MAINTIME");
        avo3.setValue("20");
        cvo2.getActionlist().add(avo3);

        ActionVo avo4 = new ActionVo();
        avo4.setName("SAMPLECOUNT");
        avo4.setValue("15");
        cvo2.getActionlist().add(avo4);

        vo.getCommandlist().add(cvo1);
        vo.getCommandlist().add(cvo2);

        return vo;
    }

    private static String createXml(ReponseVO vo) {
        StringWriter sw = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setNewLineAfterDeclaration(false);
        format.setEncoding("utf-8");

        Document doc = DocumentHelper.createDocument();
        Element rootEle = doc.addElement("response");
        Element resultEle = rootEle.addElement("result");
        resultEle.addAttribute("code", vo.getResult());

        Element commandsEle = resultEle.addElement("commands");

        List<CommandVo> commandlist = vo.getCommandlist();
        for (CommandVo commandVo : commandlist) {
            Element commandEle = commandsEle.addElement("command");
            commandEle.addAttribute("objid", commandVo.getObjid());
            commandEle.addAttribute("type", commandVo.getType());
            List<ActionVo> actionlist = commandVo.getActionlist();
            for (ActionVo actionVo : actionlist) {
                Element actionEle = commandEle.addElement("action");
                actionEle.addAttribute("name", actionVo.getName());
                actionEle.addAttribute("value", actionVo.getValue());
            }
        }
        try {
            XMLWriter xmlWriter = new XMLWriter(sw, format);
            xmlWriter.write(doc);
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
            IOUtils.close(sw);
        }
        return sw.toString();
    }
}
