package org.zanata.webtrans.shared.rpc;

import org.zanata.common.ContentState;
import org.zanata.webtrans.shared.model.TransUnitId;
import org.zanata.webtrans.shared.model.TransUnitUpdateRequest;

import java.util.ArrayList;
import java.util.List;


public class UpdateTransUnit extends AbstractWorkspaceAction<UpdateTransUnitResult>
{
   private static final long serialVersionUID = 1L;

   private List<TransUnitUpdateRequest> updateRequests;

   private boolean isRedo = false;

   public boolean isRedo()
   {
      return isRedo;
   }

   public void setRedo(boolean isRedo)
   {
      this.isRedo = isRedo;
   }

   protected UpdateTransUnit()
   {
      updateRequests = new ArrayList<TransUnitUpdateRequest>();
   }

   public UpdateTransUnit(TransUnitUpdateRequest updateRequest)
   {
      this();
      updateRequests.add(updateRequest);
   }

   public void addTransUnit(TransUnitId transUnitId, ArrayList<String> contents, ContentState contentState, int verNum)
   {
      this.updateRequests.add(new TransUnitUpdateRequest(transUnitId, contents, contentState, verNum));
   }

   public List<TransUnitUpdateRequest> getUpdateRequests()
   {
      return updateRequests;
   }

   // FIXME replace all these getters with an update request getter
   public TransUnitId getSingleTransUnitId()
   {
      checkForSingleUpdateRequest();
      return updateRequests.get(0).getTransUnitId();
   }

   public List<String> getSingleContents()
   {
      checkForSingleUpdateRequest();
      return updateRequests.get(0).getNewContents();
   }

   public ContentState getSingleContentState()
   {
      checkForSingleUpdateRequest();
      return updateRequests.get(0).getNewContentState();
   }

   public Integer getSingleVerNum()
   {
      checkForSingleUpdateRequest();
      return updateRequests.get(0).getBaseTranslationVersion();
   }

   private void checkForSingleUpdateRequest()
   {
      if (updateRequests.size() != 1)
      {
         throw new IllegalStateException("this method can only be used when updating a single TransUnit");
      }
   }
}

@@ -841,32 +841,104 @@
 Request);%0A   %7D%0A%0A
+   // FIXME pass a TransUnitUpdateRequest instead of multiple arguments%0A
    public void a
@@ -1375,32 +1375,34 @@
 %0A   %7B%0A      
-checkFor
+return get
 SingleUpdate
@@ -1414,44 +1414,8 @@
 st()
-;%0A      return updateRequests.get(0)
 .get
@@ -1485,32 +1485,34 @@
 %0A   %7B%0A      
-checkFor
+return get
 SingleUpdate
@@ -1524,44 +1524,8 @@
 st()
-;%0A      return updateRequests.get(0)
 .get
@@ -1599,32 +1599,34 @@
 %0A   %7B%0A      
-checkFor
+return get
 SingleUpdate
@@ -1638,44 +1638,8 @@
 st()
-;%0A      return updateRequests.get(0)
 .get
@@ -1710,24 +1710,26 @@
 %7B%0A      
-checkFor
+return get
 SingleUp
@@ -1745,44 +1745,8 @@
 st()
-;%0A      return updateRequests.get(0)
 .get
@@ -1785,28 +1785,40 @@
    p
-rivate void checkFor
+ublic TransUnitUpdateRequest get
 Sing
@@ -2001,15 +2001,51 @@
       %7D%0A
+      return updateRequests.get(0);%0A
    %7D%0A%7D%0A

refactor UpdateTransUnit to reduce duplication