/*
 * Copyright 2003-2015 Yusuke Yamamoto
 *
 * 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 samurai.swing;

import com.sun.tools.attach.AttachNotSupportedException;
import samurai.remotedump.ProcessUtil;
import samurai.remotedump.ThreadDumpUtil;
import samurai.remotedump.VM;
import samurai.util.Configuration;
import sun.jvmstat.monitor.MonitorException;

import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

public class LocalProcesses {
    private final FileHistory fileHistory;
    private final JMenu localProcessesMenu;

    public LocalProcesses(Configuration config, FileHistory fileHistory) {
        config.apply(this);
        this.fileHistory = fileHistory;
        localProcessesMenu = new JMenu();
        localProcessesMenu.addMenuListener(new MenuListener() {
            @Override
            public void menuSelected(MenuEvent e) {
                updateChildMenuItems();
            }

            @Override
            public void menuDeselected(MenuEvent e) {

            }

            @Override
            public void menuCanceled(MenuEvent e) {

            }
        });
//        updateChildMenuItems();
    }

    public JMenu getLocalProcessesMenu() {
        return this.localProcessesMenu;
    }

    private void updateChildMenuItems() {
        try {
            List<VM> currentVms = ProcessUtil.getVMs("localhost");

            for (int i = 0; i < localProcessesMenu.getItemCount(); i++) {
                LocalProcessMenuItem item = (LocalProcessMenuItem) localProcessesMenu.getItem(i);
                boolean found = false;
                for (VM vm : currentVms) {
                    if (item.getVm().getPid() == vm.getPid()) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    localProcessesMenu.remove(item);
                }

            }

            for (VM vm : currentVms) {
                boolean found = false;
                for (int i = 0; i < localProcessesMenu.getItemCount(); i++) {
                    LocalProcessMenuItem item = (LocalProcessMenuItem) localProcessesMenu.getItem(i);
                    if (item.getVm().getPid() == vm.getPid()) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    JMenuItem localProcess = new LocalProcessMenuItem(vm);
                    localProcess.setToolTipText(vm.getFullCommandLine());
                    localProcessesMenu.add(localProcess);
                }
            }
        } catch (URISyntaxException | MonitorException e) {
            e.printStackTrace();
        }
    }

    class LocalProcessMenuItem extends JMenuItem {
        final VM vm;

        public LocalProcessMenuItem(VM vm) {
            super(String.format("%s %s", vm.getPid(), vm.getFqcn()));
            this.vm = vm;
            addActionListener(e -> {
                try {
                    try {
                        for (int i = 0; i < 3; i++) {
                            Path path = Paths.get(String.valueOf(vm.getPid()));
                            Files.write(path, ThreadDumpUtil.getThreadDump(vm.getPid()), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
                            fileHistory.open(path.toFile());
                            Thread.sleep(1000);
                        }

                    } catch (InterruptedException ignore) {

                    }
                } catch (AttachNotSupportedException | IOException e1) {
                    e1.printStackTrace();
                }
            });
        }

        public VM getVm() {
            return vm;
        }

    }
}

@@ -3683,38 +3683,8 @@
 y %7B%0A
-                    try %7B%0A    
     
@@ -3753,20 +3753,16 @@
         
-    
 Path pat
@@ -3779,41 +3779,52 @@
 get(
-String.valueOf(vm.getPid()));%0A   
+vm.getFqcn() + %22-%22 + vm.getPid() + %22.txt%22);%0A
     
@@ -3843,17 +3843,16 @@
         
- 
 Files.wr
@@ -3983,20 +3983,16 @@
         
-    
 fileHist
@@ -4040,20 +4040,16 @@
         
-    
 Thread.s
@@ -4076,35 +4076,26 @@
             
- 
-   %7D%0A%0A    
+%7D%0A
             
@@ -4132,66 +4132,10 @@
 ion 
-ignore) %7B%0A%0A                    %7D%0A                %7D catch (
+%7C 
 Atta

change thread dump file name from [pid] to [fqcn-pid.txt]