package com.crowdin.action;

import com.crowdin.client.Crowdin;
import com.crowdin.client.CrowdinProperties;
import com.crowdin.client.CrowdinPropertiesLoader;
import com.crowdin.util.FileUtil;
import com.crowdin.util.GitUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;

import java.util.List;
import java.util.stream.Collectors;

import static com.crowdin.Constants.STANDARD_TRANSLATION_PATTERN;

/**
 * Created by ihor on 1/27/17.
 */
public class UploadFromContextAction extends BackgroundAction {
    @Override
    public void performInBackground(AnActionEvent anActionEvent) {

        final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(anActionEvent.getDataContext());
        Project project = anActionEvent.getProject();
        CrowdinProperties properties = CrowdinPropertiesLoader.load(project);
        Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
        String branch = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);
        crowdin.uploadFile(file, STANDARD_TRANSLATION_PATTERN, branch);
    }

    @Override
    public void update(AnActionEvent e) {
        Project project = e.getProject();
        CrowdinProperties properties = CrowdinPropertiesLoader.load(project);
        List<VirtualFile> files = properties.getSourcesWithPatterns().keySet()
            .stream()
            .flatMap(s -> FileUtil.getSourceFilesRec(project.getBaseDir(), s).stream())
            .collect(Collectors.toList());
        final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
        boolean isSourceFile = files.contains(file);
        e.getPresentation().setEnabled(isSourceFile);
        e.getPresentation().setVisible(isSourceFile);
    }

    @Override
    String loadingText(AnActionEvent e) {
        return "Uploading " + CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext()).getName();
    }
}

@@ -216,24 +216,66 @@
 il.GitUtil;%0A
+import com.crowdin.util.NotificationUtil;%0A
 import com.i
@@ -777,17 +777,16 @@
 vent) %7B%0A
-%0A
         
@@ -925,32 +925,50 @@
 t.getProject();%0A
+        try %7B%0A    
         CrowdinP
@@ -1021,32 +1021,36 @@
 .load(project);%0A
+    
         Crowdin 
@@ -1163,24 +1163,28 @@
 ));%0A        
+  
+  
 String branc
@@ -1269,16 +1269,20 @@
         
+    
 crowdin.
@@ -1337,16 +1337,130 @@
 ranch);%0A
+        %7D catch (Exception e) %7B%0A            NotificationUtil.showErrorMessage(project, e.getMessage());%0A        %7D%0A
     %7D%0A%0A 
@@ -1574,32 +1574,70 @@
 owdinProperties 
+properties;%0A        try %7B%0A            
 properties = Cro
@@ -1664,32 +1664,102 @@
 .load(project);%0A
+        %7D catch (Exception exception) %7B%0A            return;%0A        %7D%0A
         List%3CVir

Handle exceptions for CrowdinPropertiesLoader.load