/*
 * Copyright (c) 2015 Ziver
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package wa.server.plugin.hwstatus;

import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.cmd.Shell;
import wa.server.plugin.WAStatus;
import zutil.io.file.FileUtil;
import zutil.parser.DataNode;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Ziver on 2015-04-17.
 */
public class HDDStatus implements WAStatus {
    private int nextId;
    private HashMap<String,Integer> idMap = new HashMap<String,Integer>();

    @Override
    public String getName() {
        return "Harddrives";
    }

    @Override
    public String html() {
        try {
            return FileUtil.getContent(FileUtil.findURL("wa/server/plugin/hwstatus/HddStatus.tmpl"));
        } catch (IOException e) {
            return e.getMessage();
        }
    }

    @Override
    public void jsonUpdate(Map<String, String> request, DataNode root) {
        if (request.containsKey("hdd")) {
            DataNode hdd_root = new DataNode(DataNode.DataType.List);
            root.set("hdd", hdd_root);
            Sigar sigar = new Shell().getSigar();
            try {
                FileSystem[] hdds = sigar.getFileSystemList();
                for (FileSystem hdd : hdds) {
                    if (hdd.getType() != FileSystem.TYPE_LOCAL_DISK)
                        continue;
                    DataNode node = new DataNode(DataNode.DataType.Map);

                    String device = new String(hdd.getDevName().getBytes(), "UTF-8");
                    node.set("device", device);
                    node.set("filesystem", hdd.getSysTypeName());
                    node.set("mount", hdd.getDirName());

                    if (idMap.containsKey(device))
                        node.set("id", idMap.get(device));
                    else {
                        idMap.put(device, nextId);
                        node.set("id", nextId++);
                    }

                    FileSystemUsage hdd_use = sigar.getFileSystemUsage(hdd.getDirName());
                    node.set("size_total", hdd_use.getTotal() * 1000);
                    node.set("size_used", (hdd_use.getTotal() - hdd_use.getFree()) * 1000);
                    node.set("size_free", hdd_use.getFree() * 1000);

                    hdd_root.add(node);
                }
            } catch (SigarException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }
}
