//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package kinosearch.webapp.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kinosearch.core.Kino;
import kinosearch.core.Tools;
import kinosearch.core.warez.KinoWarez;
import kinosearch.webapp.KinoGroup;
import kinosearch.webapp.WebApp;
import kinosearch.webapp.template.TemplateProcessor;

public class IndexServlet extends HttpServlet {
    private static final long serialVersionUID = 3242992839104315456L;
    private TemplateProcessor template;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.template = WebApp.getTemplateProcessor();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, Object> model = WebApp.getDefaultModel(this.getServletContext());
        if (request.getParameter("search") != null && !request.getParameter("search").trim().isEmpty()) {
            boolean strong = false;
            if (request.getParameter("strong") != null && request.getParameter("strong").equals("1")) {
                strong = true;
            }

            this.search(request.getParameter("search"), model, strong);
        }

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        try {
            this.template.process("index.html", model, response.getWriter());
        } catch (IOException e) {
            this.log("Error process template", e);
            response.sendError(500);
        }

    }

    private void search(String search, Map<String, Object> model, boolean strong) {
        List<Kino> list = Collections.synchronizedList(new LinkedList());
        Set<KinoWarez> kinoWarezSet = Tools.getKinoWarezSet();
        ThreadGroup threadGroup = new ThreadGroup("");

        for(KinoWarez kinoWarez : kinoWarezSet) {
            (new Thread(threadGroup, () -> {
                List<Kino> outList = kinoWarez.search(search, strong);

                for(Kino kino : outList) {
                    kino.setName("[" + kinoWarez.getName() + "] " + kino.getName());
                }

                list.addAll(outList);
            })).start();
        }

        for(int i = 0; i < 15 && threadGroup.activeCount() > 0; ++i) {
            Tools.SafeSleep(1000L);
        }

        model.put("searchtext", search);
        model.put("resultsearch", this.groupKino(list));
        model.put("strong", strong);
    }

    private List<Kino> groupKino(List<Kino> list) {
        Map<String, KinoGroup> hashGroup = new HashMap();
        List<Kino> grouppedList = new ArrayList();
        Iterator<Kino> itr1 = list.iterator();
        int skip = 1;

        while(itr1.hasNext()) {
            Kino kino1 = (Kino)itr1.next();
            String s1 = Tools.cleanString(kino1.getName().replaceAll("^\\[.+?\\] ", ""));
            if (hashGroup.containsKey(s1)) {
                ++skip;
            } else {
                KinoGroup group = new KinoGroup(s1, (String)null, (KinoWarez)null);
                Iterator<Kino> itr2 = list.iterator();
                int val = 0;

                while(itr2.hasNext()) {
                    if (val < skip) {
                        ++val;
                        itr2.next();
                    } else {
                        Kino kino2 = (Kino)itr2.next();
                        String s2 = Tools.cleanString(kino2.getName().replaceAll("^\\[.+?\\] ", ""));
                        int res = s1.compareTo(s2);
                        if (res == 0 && !group.contains(kino2)) {
                            group.add(kino2);
                        }
                    }
                }

                if (group.getKinolist().size() > 0) {
                    group.add(kino1);
                    hashGroup.put(s1, group);
                } else {
                    grouppedList.add(kino1);
                }

                ++skip;
            }
        }

        grouppedList.addAll(0, hashGroup.values());
        return grouppedList;
    }

    public void log(String message, Throwable t) {
        super.log(message, t);
    }
}
