/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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.facebook.litho.intellij.actions;

import com.facebook.litho.annotations.LayoutSpec;
import com.facebook.litho.annotations.MountSpec;
import com.facebook.litho.intellij.LithoPluginUtils;
import com.facebook.litho.intellij.extensions.EventLogger;
import com.facebook.litho.intellij.logging.LithoLoggerProvider;
import com.facebook.litho.intellij.services.ComponentGenerateService;
import com.google.common.annotations.VisibleForTesting;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiJavaFile;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Updates Component file for the given Spec file. Update logic is defined by the {@link
 * ComponentGenerateService}. Works with {@link LayoutSpec} and {@link MountSpec}.
 */
public class GenerateComponentAction extends AnAction {

  @Override
  public void update(AnActionEvent e) {
    super.update(e);
    Optional<PsiClass> specCls = getValidSpec(e);
    e.getPresentation().setEnabledAndVisible(specCls.isPresent());
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    Optional<PsiClass> spec = getValidSpec(e);
    spec.ifPresent(cls -> ComponentGenerateService.getInstance().updateComponentAsync(cls));
    Map<String, String> eventMetadata = new HashMap<>();
    PsiJavaFile file = (PsiJavaFile) e.getData(CommonDataKeys.PSI_FILE);
    // Since GenerateComponentAction only appears on valid specs the file should not be null
    if (file != null) {
      eventMetadata.put(EventLogger.KEY_FILE, file.getPackageName() + "." + file.getName());
    }
    eventMetadata.put(EventLogger.KEY_RESULT, spec.isPresent() ? "success" : "fail");
    LithoLoggerProvider.getEventLogger().log(EventLogger.EVENT_GENERATE_COMPONENT, eventMetadata);
  }

  /**
   * @return {@link Optional} with {@link PsiClass} from {@link AnActionEvent} if it's a valid class
   *     for which GenerateComponent action is enabled. Or empty {@link Optional} otherwise.
   */
  @VisibleForTesting
  static Optional<PsiClass> getValidSpec(AnActionEvent e) {
    return Optional.of(e)
        .map(event -> event.getData(CommonDataKeys.PSI_FILE))
        .flatMap(
            psiFile ->
                LithoPluginUtils.getFirstClass(
                    psiFile,
                    cls ->
                        LithoPluginUtils.isLayoutSpec(cls) || LithoPluginUtils.isMountSpec(cls)));
  }
}

@@ -1917,29 +1917,107 @@
     
-spec.ifPresent(cls -%3E
+Map%3CString, String%3E eventMetadata = new HashMap%3C%3E();%0A    spec.ifPresent(%0A        cls -%3E %7B%0A         
  Com
@@ -2081,68 +2081,16 @@
 cls)
-)
 ;%0A    
-Map%3CString, String%3E eventMetadata = new HashMap%3C%3E();%0A
+  
     
@@ -2126,161 +2126,37 @@
 le) 
-e
+cls
 .get
-Data(CommonDataKeys.PSI_FILE);%0A    // Since GenerateComponentAction only appears on valid specs the file should not be null%0A    if (file != null) %7B%0A
+ContainingFile();%0A    
     
@@ -2252,93 +2252,13 @@
     
-%7D%0A
     
-eventMetadata.put(EventLogger.KEY_RESULT, spec.isPresent() ? %22success%22 : %22fail%22
+%7D
 );%0A 

Refactor logging for GenerateComponentAction