/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * 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 com.intellij.ide.actions;

import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.settingsSummary.ProblemType;
import com.intellij.util.io.ZipUtil;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipOutputStream;

public class CollectZippedLogsAction extends AnAction implements DumbAware {

  private static final ExtensionPointName<ProblemType> EP_SETTINGS = ExtensionPointName.create("com.intellij.settingsSummaryFactory");
  private static final String CONFIRMATION_DIALOG = "zipped.logs.action.show.confirmation.dialog";

  @Override
  public void actionPerformed(@NotNull final AnActionEvent e) {
    final Project project = e.getProject();

    final boolean doNotShowDialog = PropertiesComponent.getInstance().getBoolean(CONFIRMATION_DIALOG);

    int response = Messages.OK;
    if (!doNotShowDialog) {
      response = Messages.showOkCancelDialog(
        project, "Included log and settings may contain sensitive data.", "Sensitive Data",
        Messages.OK_BUTTON, Messages.CANCEL_BUTTON, Messages.getWarningIcon(),
        new DialogWrapper.DoNotAskOption.Adapter() {
          @Override
          public void rememberChoice(final boolean selected, final int exitCode) {
            PropertiesComponent.getInstance().setValue(CONFIRMATION_DIALOG, selected);
          }
        }
      );
    }
    try {
      if (response == Messages.OK) {
        final File zippedLogsFile = createZip(project);
        if (ShowFilePathAction.isSupported()) {
          ShowFilePathAction.openFile(zippedLogsFile);
        }
        else {
          Messages.showInfoMessage(zippedLogsFile.getAbsolutePath(), "Log File");
        }
      }
    }
    catch (final IOException exception) {
      Messages.showErrorDialog("Can't create zip file with logs: " + exception.getLocalizedMessage(), "Can't Create File");
    }
  }

  @NotNull
  private static File createZip(final Project project) throws IOException {
    File settingsTempFile = null;
    final File zippedLogsFile = FileUtil.createTempFile("logs-" + getDate(), ".zip");
    try (ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zippedLogsFile)))) {
      ZipUtil.addFileOrDirRecursively(zipOutputStream, null, new File(PathManager.getLogPath()), "", null, null);
      settingsTempFile = dumpSettingsToFile(project);
      ZipUtil.addFileToZip(zipOutputStream, settingsTempFile, "settings.txt", null, null);
    }
    catch (final IOException exception) {
      //noinspection ResultOfMethodCallIgnored
      zippedLogsFile.delete();
      throw exception;
    }
    finally {
      if (settingsTempFile != null) {
        //noinspection ResultOfMethodCallIgnored
        settingsTempFile.delete();
      }
    }
    return zippedLogsFile;
  }

  @NotNull
  private static File dumpSettingsToFile(final Project project) throws IOException {
    final File settingsTempFile = FileUtil.createTempFile("settings" + getDate(), ".txt");
    for (final ProblemType problemType : EP_SETTINGS.getExtensions()) {
      final String settingString = problemType.collectInfo(project);
      FileUtil.appendToFile(settingsTempFile, settingString + '\n');
    }
    return settingsTempFile;
  }

  @NotNull
  private static String getDate() {
    return new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
  }

  @Override
  public void update(AnActionEvent e) {
    Presentation presentation = e.getPresentation();
    presentation.setText(getActionName());
  }

  @NotNull
  private static String getActionName() {
    return "Compress Logs and Show in " + ShowFilePathAction.getFileManagerName();
  }
}

@@ -2069,36 +2069,70 @@
     
-int response = Messages.OK;%0A
+try %7B%0A      final File zippedLogsFile = createZip(project);%0A  
     
@@ -2161,26 +2161,17 @@
 %7B%0A      
-response =
+ 
  Message
@@ -2180,24 +2180,27 @@
 show
-OkCancel
+IdeaMessage
 Dialog(%0A
     
@@ -2195,16 +2195,18 @@
 Dialog(%0A
+  
         
@@ -2289,32 +2289,47 @@
  Data%22,%0A        
+  new String%5B%5D%7B
 Messages.OK_BUTT
@@ -2334,32 +2334,12 @@
 TTON
+%7D
 , 
-Messages.CANCEL_BUTTON
+1
 , Me
@@ -2352,23 +2352,27 @@
 .get
-Warning
+Information
 Icon(),%0A
     
@@ -2367,16 +2367,18 @@
 Icon(),%0A
+  
         
@@ -2432,16 +2432,18 @@
         
+  
 @Overrid
@@ -2454,16 +2454,18 @@
         
+ 
+ 
 public v
@@ -2525,24 +2525,26 @@
 exitCode) %7B%0A
+  
             
@@ -2629,29 +2629,21 @@
     
-   %7D%0A
      
-   
 %7D%0A      
 );%0A 
@@ -2638,19 +2638,16 @@
 %7D%0A      
-);%0A
     %7D%0A  
@@ -2652,109 +2652,23 @@
     
-try %7B%0A      if (response == Messages.OK) %7B%0A        final File zippedLogsFile = createZip(project);%0A  
+    );%0A      %7D%0A
     
@@ -2705,26 +2705,24 @@
 ported()) %7B%0A
-  
         Show
@@ -2764,30 +2764,26 @@
 ile);%0A      
-  %7D%0A  
+%7D%0A
       else %7B
@@ -2783,18 +2783,16 @@
  else %7B%0A
-  
         
@@ -2863,26 +2863,16 @@
 File%22);%0A
-        %7D%0A
       %7D%0A

Revert "Don't collect logs in case canceled is pressed"