package com.yutou.nas.utils;

import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class IdeaTools {
    public static String getIdea(String name) {
        if(StringUtils.isEmpty(name)){
            return "";
        }
        File file = new File("tmp" + File.separator + "idea.zip");
        try {
            ZipFile zipFile = new ZipFile(file, Charset.forName("gbk"));
            String data = "";
            if (file.exists()) {
                data = StreamTools.streamReadLine(zipFile.getInputStream(zipFile.getEntry(name)));
            }
            zipFile.close();
            return data;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static List<String> getIdeaList() {
        File file = HttpTools.syncDownload("http://idea.medeming.com/a/jihuoma.zip", "idea.zip");
        List<String> list = new ArrayList<>();
        try {
            ZipFile zip = new ZipFile(file, Charset.forName("gbk"));
            Enumeration<? extends ZipEntry> ez = zip.entries();
            while (ez.hasMoreElements()) {
                ZipEntry entry = ez.nextElement();
                if (entry.getName().endsWith(".txt")) {
                    list.add(entry.getName());
                }
            }
            zip.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }
}
